[蓝桥杯2019初赛]平方和

题目描述

小明对数位中含有2、0、1、9 的数字很感兴趣,在1 到40 中这样的数包
括1、2、9、10 至32、39 和40,共28 个,他们的和是574,平方和是14362。
注意,平方和是指将每个数分别平方后求和。
请问,在1 到2019 中,所有这样的数的平方和是多少?

代码如下:

#include <iostream>
using namespace std;

bool check(int x) {
    
    
	int c;
	while (x) {
    
    
		c = x % 10;
		if (c == 0 || c == 2 || c == 1 || c == 9) {
    
    
			return true;
		}
		x /= 10;
	}
	return false;
}

int main() {
    
    
	long long  res = 0;
	for (int i = 1; i <= 2019; i++) {
    
    
		if (check(i)) {
    
    
			res += i * i;
		}
	}
	cout << res << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_51955470/article/details/114241058