#include <iostream> #include <string> using namespace std; int main() { int n; cin >> n; int count = 0; for (int i = 1; i <= n; i++) { if (i % 7 == 0 || (to_string(i).find('7', 0) + 1)) { count++; } } cout << count; } // 64 位输出请用 printf("%lld")
这题很简单。
7的倍数很好操作;是否包含数字7,有人将遍历数的每一个数位进行遍历,这种方法也不用借助任何其他库(除了基本的iostream)。
笔者是将每一遍历数转化为字符串,用find()函数,如果找到则count++;如果找不到返回的值不是0,而是-1,所以需要+1。