#include <iostream> using namespace std; #include <cmath> bool isSeven(int n) { if (n % 7 == 0) { return true; } else { int x; for (int i = 1; i <= 5; i++) { int y = pow(10,i); x = n % y; if (x % 7 == 0 && x != 0) { return true; } n = n - x; if(n <= 0){ return false; } } } return false; } int main() { int num; cin >> num; int cnt = 0; for (int i = 1; i <= num; i++) { if (isSeven(i)) { //cout <<"i: " <<i << endl; cnt++; } } cout << cnt << endl; }
通过求余计算,记得输出一下看看情况