定义一个函数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;
}