简体   繁体   English

如何查看 integer 在 C 中是否有特定数字

[英]How to see if integer has a specific digit in it in C

I need to build a program, that would write all numbers from 0 to 100, but will place an * instead of any number that contains the digit 3 or can be divided by 3. This is what I have so far.我需要构建一个程序,它会写入从 0 到 100 的所有数字,但会放置一个 * 而不是任何包含数字 3 或可以除以 3 的数字。这就是我到目前为止所拥有的。 How can I make it work?我怎样才能让它工作?

#include <stdio.h>

main() {
  int i, c;
  c = 100;
  for (i = 0; i <= c; i++) {
    if (i % 3 == 0) {
      printf("*");
    }
    if (i)
      printf("%d\n", i);
  }
}

place an * instead of any number that contains the digit 3 or can be divided by 3.放置一个 * 而不是任何包含数字 3 或可以被 3 整除的数字。

OP's code took care of the "can be divided by 3" with i % 3 == 0 . OP 的代码使用i % 3 == 0处理了“可以除以 3”。

How about a little divide and conquer for the "contains the digit 3"?对“包含数字 3”进行一点分而治之怎么样? Put a function in there.将 function 放在那里。

if (contains_the_digit(i, 3) || (i % 3 == 0)) {
  printf("*\n");
} else {
  printf("%d\n", i);
}

Now what is left is to define contains_the_digit(int i, int digit)现在剩下的是定义contains_the_digit(int i, int digit)

Mathematically (nice and efficient):数学上(又好又高效):

bool contains_the_digit_via_math(int i, int digit) {
  do {
    if (abs(i % 10) == digit) { // Look at the least digit, abs() to handle negative `i`
      return true;
    }
    i /= 10; // Now look at the upper decimal digits
  } while (i);  
  return false;
}

Or textually:或文字:

bool contains_the_digit_via_string(int i, int digit) {
  char buf[30];  // Something certainly big enough
  sprintf(buf, "%d", i);
  return strchr(buf, digit + '0') != NULL; 
}

Or use your imagination for other ideas.或者将您的想象力用于其他想法。


  • The key is to take your problems and reduce them to smaller ones with helper functions: divide and conquer .关键是用辅助函数把你的问题简化为更小的问题:分而治之。
  1. Concert the number to a string将数字与字符串一致
  2. Replace '3' with '*' within that string在该字符串中将 '3' 替换为 '*'

ie IE

int to_be_converted =12345612343242432; // Or summat else
char num[100]; // Should be more than enough
sprintf(num, "%d", to_be_converted);

for (int i =0; num[i]; i++) {
   if (num[i] -- '3') num[i] = '*';
}
printf("Here you go %s", num);

That should do the trick这应该够了吧

Just ad the bit to go through the numbers and check if divisible by 3. I leave that to the reader.只需通过数字将位添加到 go 并检查是否可被 3 整除。我将其留给读者。

Seeing you forgot to add the return type int to your int main() , I think this is a good time to learn to write your own function!看到您忘记将返回类型int添加到您的int main()中,我认为这是学习编写自己的函数的好时机!

In this case, you want a function that can check whether the last digit of a number is a 3 when you represent that number as base-10.在这种情况下,您需要一个 function,当您将该数字表示为 base-10 时,它可以检查该数字的最后一位是否为3 That's easy!这很容易! The function should look like (you need to #include <stdbool.h> at the beginning of your file, too): function 应该看起来像(您也需要在文件开头添加#include <stdbool.h> ):

bool ends_in_decimal_3(int number) {
  // figure out a way to find the difference 
  // between number, rounded to multiples of 10
  // and the original number. If that difference==3, 
  // then this ends in 3 and you can `return true;`
}

Armed with that function, you can see whether your i itself ends in 3 , or whether i/10 ends in 3 and so on.有了那个 function,你可以看到你的i本身是否以3结尾,或者i/10是否以3结尾等等。 Remembering that division / in C between int s always rounds down is a good trick to do that, and also an important hint on how to implement your rounding in ends_in_decimal_3 .记住int s 之间 C 中的除法/总是向下舍入是一个很好的技巧,也是如何在ends_in_decimal_3中实现舍入的重要提示。

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

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