解题思路:
不对的99%都是漏了带有7的数字
首先是7的倍数,n%7就可以得出;
个位:只有7且恰好是7的倍数;
十位:x7、7x(以下x均表示任意1-9)
x7可以求末尾就行n%10==7; 7x求十位n/10==7;
百位:7xx、x7x、xx7
7xx求百位 n/100==7; x7x求十位 n/10%10==7; 个位n%100==7;
千位:7xxx、x7xx、xx7x、xxx7
7xxx求千位 n/1000==7; x7xx求百位 n/100%10==7; xx7x十位n/10%100==7; 个位n%1000==7;
万位:(因为不超过30000,所以没有7xxxx)x7xxx、xx7xx、xxx7x、xxxx7
x7xxx求千位n/1000%10==7; xx7xx求百位n/100%100==7; xxx7x求十位n/10%1000==7; 个位n%10000==7;
注意事项:
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,flag=0;
cin>>n;
for(int i=7;i<=n;i++){
if(i%7==0||i%10==7||i/10==7||i/100==7||i/10%10==7
||i%100==7||i/1000==7||i/100%10==7||i/10%100==7||
i%1000==7||i/1000%10==7||i/100%100==7||
i/10%1000==7||i%10000==7)
flag++;
}
cout<<flag;
return 0;
}
0.0分
5 人评分