繁体   English   中英

C 中的重复代码不适用于大数字

[英]Repetition code in C isn't working for large numbers

我正在用 C 编写一个代码来查找给定数字中重复的数字,我编写的代码适用于小数字,但如果我输入一个大值 N < 1000,输出就会混乱。这是我的代码; 请帮帮我!

输入:1839138012980192380192381090981839

我得到这个输出:0 2 3 5 7 8


#include <stdio.h>

int main()
{
    int digit, digits[10], flag = 0, i;
    long long num;

    scanf("%lld", &num);

    while (num)
    {
        digit = num % 10;
        if (digits[digit])
            flag = 1;
        digits[digit]++;
        num /= 10;
    }

    if (flag)
    {
        for (i = 0; i < 10; i++)
        {
            if (digits[i] > 1)
                printf("%d ", i);
        }
        printf("\n");
    }
    else
        printf("The are no repeated digits.\n");

    return 0;
}

long long类型只能表示有限范围的数字。 在您的 C 实现中, 1839138012980192380192381090981839 对于long long来说太大了,并且scanf("%lld", &num)不起作用。

相反,使用c = getchar();读取输入的每个字符c = getchar(); ,其中c被声明为int 如果在getchar之后, cEOF ,则停止循环并打印结果。 如果c不是EOF ,则使用if (isdigit((unsigned char) c))检查它是否是数字。 isdigit函数在<ctype.h>定义,因此包含该标题。

如果字符是数字,则使用c - '0'将其从字符转换为它表示的数字。 您可以使用int d = c - '0'; 将数字存储在d 然后增加数字d的计数。

如果字符不是数字,您可以决定要做什么:

  • 在用户输入的行的末尾可能会有一个换行符'\\n' 您可能想忽略它。 当您看到换行符时,您可以结束循环并打印结果,您可以继续阅读以查看在看到EOF之前是否还有其他数字或字符,如果有则向用户报告问题,或者您可以忽略它并继续循环。
  • 输入中可能有空格。 您可能想要忽略它们,或者您可能想要向用户报告问题。
  • 如果还有其他字符,您可能需要向用户报告问题。

这是另一种方法,您可以将其与某个最大长度的字符串(由常量MAX_LEN定义)一起使用。

由一堆char组成的字符串每个字符将使用一个字节,因此您可以将MAX_LEN定义为系统内存中的字节数,通常,尽管实际上您可能会使用更小且更合理的数字。

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

#define MAX_LEN 12345

int main()
{
  int digit, digits_checker[10] = {0}, flag = 0, i;
  char* num;

  /* set aside space for the string and its terminator */
  num = malloc(MAX_LEN + 1);

  /* read num from input */
  scanf("%s", num);

  /* get the string length */
  size_t num_length = strlen(num);

  /* walk over every character in num */
  for (size_t position = 0; position < num_length; position++)
    {
      /* 
         We assume that the value of num[position] is an 
         ASCII character, from '0' to '9'. (If you can't make
         that assumption, check the input and quit with an
         error, if a non-digit character is found.)

         If the input is valid, the digits 0-9 in the ASCII 
         table start at 48 ('0') and end at 57 ('9'). Subtracting 
         48, therefore, gives you the integer value at each digit 
         in num.
      */
      digit = num[position] - 48;
      /*
        Increment a counter for each digit
      */
      digits_checker[digit]++;
    }

  /* num is no longer needed, so we free its memory */
  free(num);
  
  /* check each digit */
  for (i = 0; i < 10; i++)
    {
      if (digits_checker[i] > 1) {
        printf("%d ", i);
        flag = 1;
      }
    }
  if (!flag) {
    printf("The are no repeated digits.\n");
  }
  else {
    printf("\n");
  }
  
  return 0;
}

检查输入的建议是一个很好的建议。 您不一定认为某人输入的内容将完全由数字字符组成。

但希望这演示了如何为字符串留出空间,以及如何一次一个字符地通读它。

暂无
暂无

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

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