回到C中的for循环的开始

时间:2016-12-10 17:56:16

标签: c arrays

即使这个问题被问了一百万次,但我还没有找到真正有助于我的案例的答案,或者我根本看不到解决方案。

我被赋予了一个任务,即制作一个程序,该程序包含一个整数并计算每个数字出现在其中的次数,并且也不会两次显示相同的信息。由于我们目前正在使用数组,所以当然我必须使用数组,因为我的代码很乱,因为我缺乏C知识,我会尝试解释我的思考过程并给你代码。

输入一个数字之后,我将每个数字除以10,然后将这些数字放入一个数组,然后(因为数组反转)我反转了反向数组以使其看起来更好(即使它不是不需要)。在那之后,我有一堆恶心的循环,其中我尝试循环遍历整个数组,同时将第一个元素再次与所有元素进行比较,因此对于数组的每个元素,我再次将它与数组的每个元素进行比较。我还在每次检查后将checked元素添加到一个新数组中,因此我可以主要检查元素是否已经被比较过,所以我不必再做整件事,但这就是我的问题所在。我已经尝试了继续或转到大量的操作,但我找不到解决方案。所以我只使用了**编辑:返回0 **,看看我的想法是否在第一位好,对我来说似乎是,我只是缺乏回到for循环顶部的知识。请帮帮我?

//返回0后,程序在尝试检查数字1后完全停止,因为它已经被检查过了。我希望它继续检查其他的,但有许多版本的推杆继续,它只是没有做的工作。 //

///试图让代码看起来更好。 ///

#include <stdio.h>


#define MAX 100

int main()
{
int a[MAX];
int b[MAX];
int c[MAX];
int n;
int i;
int j;
int k;
int counter1;
int counter2;


printf("Enter a whole number: ");
scanf("%i",&n);
while (1)
{
    for (i=0,counter1=0;n>10;i++) 
    {
        a[i] = n%10;
        n=n/10;
        counter1+=1;
        if (n<10)
        a[counter1] = n;
    }
    break;      
}


printf("\nNumber o elements in the array: %i", counter1);
printf("\nElements of the array a:");
for (i=0;i<=counter1;i++)
{
    printf("%i ",a[i]);
}


printf("\nElements of the array b:");
for (i=counter1,j=0;i>=0;i--,j++) 
{   
    b[j] = a[i];
}
for (i=0;i<=counter1;i++) 
{   
    printf("%i ",b[i]);
}

for (i=0;i<=counter1;i++)
{   
    for(k=0;k<=counter1;k++)
    {
        if(b[i]==c[k])
        {
        return 0;
        }
    }
    for(j=0,counter2=0; j<=counter1;j++)
    {
        if (b[j] == b[i])
        {
        counter2+=1;
        }
    }
    printf("\nThe number %i appears %i time(s)", b[i], counter2);
    c[i]=b[i];
}
} 

4 个答案:

答案 0 :(得分:1)

手头的任务非常简单,当然也不需要复杂的结构,更不用说goto

您将数字放在数组中的想法很好,但是过早地增加counter。 (请记住,C中的数组以索引0开头。)所以让我们修复:

int n = 1144526;        // example number, assumed to be positive

int digits[12];         // array of digits
int ndigit = 0;

while (n) {
    digits[ndigit++] = n % 10;
    n /= 10;
}

++之后ndigit将在使用其值后递增ndigit。在C中使用它作为方括号内的数组索引非常常见。)

我们只想计算数字,所以反转数组确实没有必要。现在我们要计算所有数字。当我们第一次看到所有数字时,我们可以做到这一点,例如在337223中,首先计算所有3s,然后计算所有7s然后计算所有2s,但这将很快变得复杂。计算所有10位数字要容易得多:

int i, d;

for (d = 0; d < 10; d++) {
    int count = 0;

    for (i = 0; i < ndigit; i++) {
        if (digit[i] == d) count++;
    }

    if (count) printf("%d occurs %d times.\n", d, count);
}

外部循环遍历所有十位数字。内部循环计算数字数组中d的所有出现次数。如果计数为正数,请将其写出来。

如果你考虑一下,你可以做得更好。数字只能包含0到9的值。我们可以为每个数字保留一个计数数组,并将数字数组传递一次,随时计算数字:

int count[10] = {0};

for (i = 0; i < ndigit; i++) {
    count[digit[i]]++;
}

for (i = 0; i < 10; i++) {
    if (count[i]) printf("%d occurs %d times.\n", i, count[i]);
}

(请记住= {0}count的第一个元素显式设置为零,其余元素隐式设置,以便从一个包含十个零的数组开始。)

如果你考虑一下,你甚至不需要数组digit;你可以马上统计数字:

int count[10] = {0};

while (n) {
    count[n % 10]++;
    n /= 10;
}

for (i = 0; i < 10; i++) {
    if (count[i]) printf("%d occurs %d times.\n", i, count[i]);
}

最后,提出建议:如果您发现自己需要使用特殊工具来挽救复杂代码以执行简单任务,请退后一步并尝试简化问题。我的印象是你添加了更复杂的内容,即使你真的不明白。

例如,计算数字的方法非常困惑。例如,数组c是什么?在为它写出合理的值之前,你要先阅读它。尝试实现一个非常简单的解决方案,不要试着先聪明一点,然后寻求一个简单的解决方案。即使这不是你作为人类所做的事情,也要记住计算机擅长快速执行愚蠢的任务。

答案 1 :(得分:0)

我认为你需要的是一个“继续”而不是一个返回0。

for (i=0;i<=counter1;i++) {
  for(k=0;k<=counter1;k++) {
    if(b[i]==c[k]) {
      continue;   /* formerly return 0; */
    }

    for(j=0,counter2=0; j<=counter1;j++)
        if (b[j] == b[i]){
          counter2+=1;
        }
  }

答案 2 :(得分:0)

请尝试查看此计划是否可以为您提供帮助。

#include <stdio.h>

int main() {
    unsigned n;
    int arr[30];
    printf("Enter a whole number: ");
    scanf("%i", &n);
    int f = 0;
    while(n)
    {
        int b = n % 10;
        arr[f] = b;
        n /= 10;
        ++f;
    }
    for(int i=0;i<f;i++){
        int count=1;
        for(int j=i+1;j<=f-1;j++){
            if(arr[i]==arr[j] && arr[i]!='\0'){
                count++;
                arr[j]='\0';
            }
        }
        if(arr[i]!='\0'){
            printf("%d is %d times.\n",arr[i],count);
        }
    }
}

测试

Enter a whole number: 12234445
5 is 1 times.
4 is 3 times.
3 is 1 times.
2 is 2 times.
1 is 1 times.

答案 3 :(得分:0)

这是另一个只使用一个循环来分析输入的产品。我做了其他评论。

#include <stdio.h>

int main(void)
{
    int count[10] = { 0 };
    int n;
    int digit;
    int elems = 0;
    int diff = 0;

    printf("Enter a whole number: ");
    if(scanf("%d", &n) != 1 || n < 0) {           // used %d, %i can accept octal input
        puts("Please enter a positive number");   // always check result of scanf 
        return 1;
    }

    do {
        elems++;                                  // number of digits entered
        digit = n % 10;
        if(count[digit] == 0) {                   // number of different digits
            diff++;
        }
        count[digit]++;                           // count occurrence of each
        n /= 10;
    } while(n);                                   // do-while ensures a lone 0 works

    printf("Number of digits entered: %d\n", elems);
    printf("Number of different digits: %d\n", diff);
    printf("Occurrence:\n");
    for(n = 0; n < 10; n++) {
        if(count[n]) {
            printf("    %d of %d\n", count[n], n);
        }
    }
    return 0;
} 

计划会议:

Enter a whole number: 82773712
Number of digits entered: 8
Number of different digits: 5
Occurrence:
    1 of 1
    2 of 2
    1 of 3
    3 of 7
    1 of 8