while True: try: N=int(input()) res=[] for i in range(7,N+1):#如果直接用range(N+1)的话,会将0也算进去,所以从7开始最保险 if '7' in str(i) or i%7==0: res.append(i) print(len(res)) except: break
#include <iostream> #include <string> #include <sstream> using namespace std; int main(){ int N; while(cin >> N){ int count=0; string str; for(int i=7;i<=N;i++){ stringstream a; a << i; a >> str; if(str.find('7')!=str.npos || i%7==0) count++; } cout << count << endl; } return 0; }
#include <iostream> using namespace std; int main(){ int N; while(cin >> N){ int count=0; if(N<7) count=0; else{ for(int i=7;i<=N;i++){//先判断是否能被7整除,之后依次判断个位、十位、百位、千位...各个位是否为7 if(i%7==0 || i%10==7 || (i%100)/10==7 || (i%1000)/100==7||(i%10000)/1000==7) count++; } } cout << count << endl; } return 0; }