题解 | #挑7#
挑7
https://www.nowcoder.com/practice/ba241b85371c409ea01ac0aa1a8d957b
tempn 为 1~n 中的任意一位,tempn = i;
7的倍数就是 tempn%7 == 0;
有7 就从个位开始判断:tempn % 10 == 7?
如果个位有 7,count++ ,i++;如果没有, tempn /= 10 再判断个位有没有7,一直到tempn为0,i++;
#include <iostream>
using namespace std;
int main() {
int n,tempn,count = 0;
cin >> n;
tempn = n;
for(int i = 1; i <= n;i++){
tempn = i;
if(tempn % 7 == 0){
count++;
}else{
while(tempn){
if(tempn % 10 == 7){
count++;
break;
}
tempn /= 10;
}
}
}
cout << count ;
return 0;
}
// 64 位输出请用 printf("%lld")
查看19道真题和解析