繁体   English   中英

如何打印我两个数组具有的相同整数的数量? [C]

[英]How to print the amount of same integers that my 2 arrays have? [C]

我需要编写的代码有问题。 我必须从命令行中获取14个参数,并使用它们来生成彩票号码,中奖号码,然后将这两个参数相互比较。

例如,使用以下参数: ./a.out 2 30 17 8 6 19 24 7 6 1 2 3 5 4

应该做这样的事情:

Winning numbers: 2 30 17 8 6 19 24
Lottonumbers: 7 6 1 2 3 5 4

2 are the same: 6 2

我的代码几乎可以按预期工作,但是我似乎无法正确打印此代码: 2相同 它总是这样循环: 1 are the same: 6 2 are the same: 2

数字2是比较2个数组时发现的相同数字的数量。 我的问题是我该如何打印它,以使它不会重复文本且字数正确? 即使很简单,我的头似乎也无法工作:/

#include <stdio.h>
#include <stdlib.h>

int main(int args, char **argv)
{    
    int i;
    int winningNumbers[7];
    int lottoNumbers[7];
    int j;
    int a;
    int b;
    int winningNumber;
    int lottoNumber;
    int count = 0;

    printf("Winning numbers: ");

    for (i=0;i<7; i++) {    
        winningNumber = atoi(argv[i+1]);    
        winningNumbers[i] = winningNumber;    
        printf("%d ", winningNumber);    
    }

    printf("\n");    
    printf("Lotto numbers:: ");

    for (j= 8; j < args; j++) {

        lottoNumber = atoi(argv[j]);    
        lottoNumbers[j-8] = lottoNumber;    
        printf("%d ", lottoNumber);

    }
    printf("\n");

    for(a = 0; a < 7; a++) {

        for(b=0; b < 7; b++) {
            if (lottoNumbers[a] == winningNumbers[b]) {
                count = count + 1;
                printf("%d are the same: %d", count, winningNumbers[b]);

            }
        }
    }

    return 0;
}

搜索匹配项并显示结果是两个单独的任务。 不尝试同时进行操作更简单,更灵活。

首先搜索匹配项并将其存储在数组中。 然后根据需要显示数组的内容。

int main (int argc, char *argv[])
{
    int winningNumbers[7];
    int lottoNumbers[7];
    int commonNumbers[7];
    int count = 0;

    // fill winningNumbers
    // fill lottoNumbers

    // NOTE: the following loop assumes that in both arrays
    // no number is repeated.
    // You should check that this is indeed the case.
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            if (lottoNumbers[i] == winningNumbers[j]) {
                commonNumbers[count] = lottoNumbers[i];
                count++;
            }
        }
    }

    printf ("%d are the same:", count);
    for (int i = 0; i < count; i++) {
        printf (" %d", commonNumbers[i]);
    }
    printf ("\n");

    return 0;
}

许多简单的程序应遵循以下结构:

  1. 读取并检查输入
  2. 将输入转换为输出
  3. 打印输出
for(b=0; b < 7; b++) {
    if (lottoNumbers[a] == winningNumbers[b]) {
        count = count + 1;
    }
}
printf("%d are the same: ", count);
for(b=0; b < 7; b++) {
    if (lottoNumbers[a] == winningNumbers[b]) {
    printf(" %d", winningNumbers[b]);
    }
}
printf("\n");
int finalArray[7];
int i;
for(a = 0; a < 7; a++) {
    for(b=0; b < 7; b++) {
        if (lottoNumbers[a] == winningNumbers[b]) {
            finalArray[count] = lottoNumbers[a];
            count = count + 1;
        }
    }
}
printf("%d are same: ", count);
for(i = 0; i < count; i++)
    printf("%d ", finalArray[i]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM