English 中文(简体)
C. 显示重复数字的便利项目
原标题:Easy C project to show repeated numbers
  • 时间:2011-04-19 22:40:29
  •  标签:
  • c

我对这一法典有问题。 该项目应在投入号中显示我重复的数字。 例如:

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

该守则是:

#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]signsb[t] to a[n]

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

利用<代码>-Wall-Wextra-Werror进行汇编是一个好主意,因此所有警告都得以实现,并像错误一样得到处理(因此,你只能忽视这些警告)。 编辑用这些旗帜在你进行意外指派。

页: 1

通常的做法是建立10个<代码>int的阵列,每个数字各一个,并在用户提供的数字中计算每个数字的发生。

具备获得编号num数字的辅助技术。 每次使用<代码>num % 10,以获得最后一位数,num / 10,以获得最后一位数。 之后,您的方案可能会考虑这样的内容:

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
", i, dcount[i]);

我没有对上述法典进行测试,因此可能有一些小的缺陷。 但一般原则应当明确。

Hope that helps.

页: 1

这必然会造成问题。 我没有完全研究过你守则的其余部分,但首先,在使用该编码之前,你应先入选<>t<>t。 如果仍然没有工作,则提供信息,如它做什么或做什么。

您不妨看一看for loop.t。 缩略语 并且,你在<代码>a[n]与b[t]上读的正文上书写字。 在<代码>if的有条件条件下,请注明=,其中填写=

如果你们在汇编者中选择每一种办法,以发出警告,严格检查遵守标准语言的情况,就会发现这种情况。

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;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签