如何检查是否一个INT变种中包含的具体数量 [英] How to check if a int var contains a specific number

查看:110
本文介绍了如何检查是否一个INT变种中包含的具体数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查是否INT变种中包含的具体数量

我不能找到一个解决方案。例如:我需要检查INT 457包含数字5某处

I cant find a solution for this. For example: i need to check if the int 457 contains the number 5 somewhere.

感谢您的帮助;)

推荐答案

457%10 = 7 *

457 % 10 = 7 *

十分之四百五十七= 45

457 / 10 = 45

45%10 = 5 *

45 % 10 = 5 *

十分之四十五= 4

4%10 = 4 *

4 % 10 = 4 *

4月10日= 0完成

4 / 10 = 0 done

明白了吗?

编辑:

下面是一个C实现我的回答暗示的算法。它会找到任何整数任何数字。它本质上是完全相同的,不同的是它为负整数,一旦数字被发现停止沙克蒂·辛格的答案......

Here's a C implementation of the algorithm that my answer implies. It will find any digit in any integer. It is essentially the exact same as Shakti Singh's answer except that it works for negative integers and stops as soon as the digit is found...

const int NUMBER = 457;         // This can be any integer
const int DIGIT_TO_FIND = 5;    // This can be any digit

int thisNumber = NUMBER >= 0 ? NUMBER : -NUMBER;    // ?: => Conditional Operator
int thisDigit;

while (thisNumber != 0)
{
    thisDigit = thisNumber % 10;    // Always equal to the last digit of thisNumber
    thisNumber = thisNumber / 10;   // Always equal to thisNumber with the last digit chopped off, or 0 if thisNumber is less than 10
    if (thisDigit == DIGIT_TO_FIND)
    {
        printf("%d contains digit %d", NUMBER, DIGIT_TO_FIND);
        break;
    }
}

这篇关于如何检查是否一个INT变种中包含的具体数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆