首页 留言 登录
例6.4定义一个函数check(n,d),如果数字d在正整数n的某位中出现,则返回true,否则返回false

定义一个函数check(n,d),如果数字d在正整数n的某位中出现,则返回true,否则返回false

例如

check(326719,3)== true

check(326719,4)== true

程序如下:

#include <bits/stdc++.h> 
using namespace std;
bool check(int,int);
int main(){
    int a,b;
    cin >> a >> b;
    if(check(a,b)==true) cout << "true" << endl;
    else{ cout << "false" << endl;}

    return 0;
}

bool check(int n,int d){
    while(n){
        int e = n%10; // n:12345 e:5
        if(e==d) return true;
        n/=10;        // n: 12345 /= 10 --> 1234
    }                 
    // n == 0 时,循环退出 
    return false; 
}
上一篇:bubble冒泡排序,从大往小排函数
下一篇:二维前缀和
验证码
评论留言 (0条)