题目连接:http://oj.ecustacm.cn/problem.php?id=1452
1452: [蓝桥杯2019初赛]平方和
时间限制: 1 Sec 内存限制: 256 MB
提交: 953 解决: 513
[状态] [提交] [命题人:外部导入]
题目描述
小明对数位中含有2、0、1、9 的数字很感兴趣,在1 到40 中这样的数包
括1、2、9、10 至32、39 和40,共28 个,他们的和是574,平方和是14362。
注意,平方和是指将每个数分别平方后求和。
请问,在1 到2019 中,所有这样的数的平方和是多少?
这一题就是简单的枚举,将每一枚举出来的结果检验一下是否含有2
,0
,1
, 9
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 30000;
long long res;
int main(){
for (int i = 1; i <= 2019; i ++ ){
string s = to_string(i);
if (s.find("2") != string::npos ||
s.find("0") != string::npos ||
s.find("1") != string::npos ||
s.find("9") != string::npos)
res += i * i;
}
cout << res << endl;
return 0;
}