开发者

Easy C project to show repeated numbers

I have a problem with this code.... The project should show me repeated number in the input number. For example:

$ ./a.out
Enter a number: 9893746595
Repeated: 9 5

Here is the code:

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

int main(void)
{   
    int a[10], b[10] ;
    int n,t;
    printf("Enter a number: ");
    for(n=0; n<10;n++)
    {
        scanf("%d", &开发者_如何转开发a[n]);
        n=t;
        a[n]=b[t];

    }
    for(n=0;n<10;n++)
    {
        for(t=n;t<10;t++)
        {
            if(a[n]=b[t])
            printf("%d", a[n]);
        }
    }
    return 0;
}


if(a[n]=b[t]) assigns b[t] to a[n].

You most likely wanted to use if(a[n] == b[t]) to compare those values.

It's a good idea to compile using the -Wall -Wextra -Werror flags so all warnings are enabled and treated like errors (so you can't simply ignore them). With those flags the compiler will yell at you for doing an accidental assignment.


Your code is bogus. ;-)

The usual approach here is to create an array of 10 ints, one for each digit, and count the occurrences of each digit in the user-supplied number.

There's an idiomatic technique to get the digits of a number num one at a time: use num % 10 to get the last digit, and num / 10 to get the number without its last digit. Then your program might look something like this:

int dcount[10] = {0};  // 10 ints, all initialized to 0

scanf("%d", &num);

while(num) {
        dcount[num % 10]++;   // increment dcount[i], where i is the last digit of num
        num /= 10;            // "remove" last digit from num
}

for (int i = 0; i < sizeof(dcount)/sizeof(dcount[0]); i++)
        printf("%d occured %d times\n", i, dcount[i]);

I didn't test the above code, so there may be some minor flaws. The general principle should be clear, though.

Hope that helps.


Your code assigns t to n before t has been initialized.

This is bound to cause problems. I haven't fully studied the rest of your code but you should start by initializing t before using it. If that still doesn't work, provide information such as what it does or doesn't do.


You might want to look at the first for loop. t is read from without ever being written to. Same for b. And, you are overwriting the just read in variable a[n] with b[t]. In the conditional for if, you meant == where = is written.

If you turn on every option in your compiler to emit warnings and strictly check for standard language compliance, it would have caught these.


int main()
{
   int i, number, digitCount[10];

   // Before starting, set the digit count for each digit to 0
   for (i = 0; i < 10; i++)
   {
      digitCount[i] = 0;
   }

   // Store the entire number in one int
   printf("Enter a number: ");
   scanf("%d", &number);

   // Find the remainder of number / 10 in order to get the last digit
   // Divide number by 10 in order to remove that digit
   // Continue to peel off digits until you reach zero
   while (number != 0)
   {
      digitCount[number % 10]++;
      number /= 10;
   }

   // For each digit which is counted more than once, print it
   printf("Repeated: ");
   for (i = 0; i < 10; i++)
   {
      if (digitCount[i] > 1)
      {
         printf("%d ", digitCount[i]);
      }
   }

   return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜