题解 | #挑7#
挑7
https://www.nowcoder.com/practice/ba241b85371c409ea01ac0aa1a8d957b?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37
/* 思路:注意两个条件 1. 该数字能被 7 整除 2. 或该数字中包含 7 (利用 to_string(num).find('7') != string::npos 来判断数字中是否包含7) 两个满足一个即满足题目要求 做法: 遍历从 1 到 n 的所有数, 累加满足条件的数字 */ #include <iostream> #include <string> using namespace std; int main() { int n; cin >> n; int ans = 0; for(int i = 1; i < n+1; ++i){ if(i % 7 == 0 || to_string(i).find('7') != string::npos){ ans++; } } cout << ans << endl; return 0; } // 64 位输出请用 printf("%lld")
字符串操作 文章被收录于专栏
字符串操作