【问题标题】:How to identify specific digits of an integer input in C?如何识别C中整数输入的特定数字?
【发布时间】:2010-09-09 03:05:54
【问题描述】:

我需要获取包含数字 1 的位数。我知道在 java 中我可以将输入作为 String 并使用 charAt,但我知道 C 中没有隐式 String 函数。如何我做到了?

【问题讨论】:

    标签: c string function


    【解决方案1】:

    除法和模数是你的朋友。

    #include "stdio.h"
    
    int main(){
        int digits[] = {0,0,0,0,0,0,0,0,0,0};
        int i = 11031;
    
        while(i > 0){
            digits[i % 10]++;
            i = i / 10;
        }
    
        printf("There are %d ones.\n", digits[1]);
    }
    

    【讨论】:

      【解决方案2】:

      家庭作业?

      您可以使用fread() 函数将其读入char*,然后将读取的字节数存储在单独的变量中。然后使用for 循环遍历缓冲区并计算每个字节的数量。

      【讨论】:

        【解决方案3】:

        尝试类似...

        int digit = 0;
        int value = 11031;
        
        while(value > 0)
        {
            digit = value % 10;
            /* Do something with digit... */
            value = value / 10;
        }
        

        【讨论】:

          【解决方案4】:

          我认为这是一个基本的理解问题,每个人都不可避免地会从一种语言切换到另一种语言。

          当您开始熟悉 java 时,了解 string 在 C 中如何工作的一个很好的参考是查看 string.h 的工作原理。 Java 中的字符串是一个内置的对象,而 C 中的字符串只是整数数组。

          那里有很多教程,在我年初开始时对我有帮助的是 http://www.physics.drexel.edu/students/courses/Comp_Phys/General/C_basics/ 查看字符串部分。

          有时提出问题比连续数小时翻阅教科书更快地加快学习速度。

          【讨论】:

            【解决方案5】:

            如果你只有数字,那么你可以这样做:

             int val; //Input
             ...
             int ones = 0;
             while(val != 0) {
               ones += ((val % 10) == 1) ? 1 : 0;
               val /= 10;
             }
            

            如果你有一个字符串 (char*),你会做这样的事情:

            while(*str != '\0') {
              if(*str++ == '1') {
                ones++;
              }
            }
            

            同样值得注意的是,c 确实有一个 charAt 函数,在某种程度上:

            "java".charAt(i) == "c the language"[i];
            

            通过对char*进行索引,可以得到你想要的值,但是需要小心,因为没有indexOutOfBounds异常。如果您超过字符串的末尾,程序将会崩溃,或者更糟糕的是它可能会继续运行,但内部状态会混乱。

            【讨论】:

              【解决方案6】:

              类似的东西:

              int val=11031;
              int count=0;
              int i=0;
              char buf[100];
              sprint(buf, "%d", val);
              for(i=0; (i < sizeof(buf)) && (buf[i]); i++) {
                if(buf[i] == '1')
                  count++;
              }
              

              【讨论】:

                【解决方案7】:
                int count_digit(int nr, int digit) {
                    int count=0;
                    while(nr>0) {
                      if(nr%10==digit)
                        count++;
                      nr=nr/10;
                    }
                    return count;
                }
                

                【讨论】:

                  【解决方案8】:

                  这对我来说听起来像是一个家庭作业问题。哦,好吧,这是你的生活。

                  您未能指定包含“输入整数”的变量的类型。如果输入整数是整数类型(比如“int”),试试这个:

                  int countOnes(int input)
                  {
                      int result = 0;
                      while(input) {
                          result += ((input%10)==1);
                          result /= 10;
                      }
                      return result;
                  }
                  

                  如果“输入整数”在字符串中,试试这个:

                  int countOnes(char *input)
                  {
                      int result = 0;
                      while(input && *input) {
                          result += (*input++ == '1');
                      }
                      return result;
                  }
                  

                  希望这会有所帮助。下一次,做你自己的功课。离开我的草坪!孩子们,这些天,...

                  【讨论】:

                    【解决方案9】:
                    int countDigit(int Number, int Digit)
                    {
                       int counter = 0;
                    
                       do
                       {
                          if( (Number%10) == Digit)
                          {
                             counter++;
                          }
                       }while(Digit>0)
                    
                       return counter;
                    }
                    

                    【讨论】:

                      【解决方案10】:

                      类似这样的事情:

                      #include <stdio.h>
                      
                      main() {
                              char buf[100];
                              char *p = buf;
                              int n = 0;
                              scanf("%s", buf);
                              while (*p) {
                                      if (*p == '1') {
                                              n++;
                                      }
                                      p++;
                              }
                              printf ("'%s' contains %i ones\n", buf, n);
                      }
                      

                      【讨论】:

                        【解决方案11】:

                        这样就可以了。 :-)

                        int count_digits(int n, int d) {
                          int count = 0;
                          while(n*10/=10) if (n%10==d) count++
                          return count;
                        }
                        

                        【讨论】:

                          【解决方案12】:

                          对于所有将此问题称为作业问题的人:我不得不说,你们中的大多数人都提供了作业答案。

                          您不使用除法/模数来获取生产代码中的数字,首先是因为它不是最理想的(您的 CPU 是为二进制算术而不是十进制设计的),其次是因为它不直观。即使它最初不是字符串,最好将其转换为一个然后计算字符数(std::count 是 C++ 的方式)。

                          【讨论】:

                          • 将整数转换为字符串的唯一方法是使用除法/模数。即使您使用最简单的方法(来自 C 的 itoa),也可以通过这种方式完成... 总之,@hoyhoy 的答案比您的更优化,因为您需要先转换整数然后计算字符数
                          猜你喜欢
                          • 1970-01-01
                          • 2019-12-16
                          • 2021-12-20
                          • 2021-04-16
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          相关资源
                          最近更新 更多