本篇博客会持续更新…
1 试题A
1.1 题目
1.2 参考代码
#include <iostream>
using namespace std;
bool isContain(int x)
{
if (x % 10 == 2 || x % 10 == 0 || x% 10 == 1 || x% 10 == 9)
{
return true;
}
else
{
if (x < 10)
{
return false;
}
x = x / 10;
return isContain(x);
}
}
int main()
{
long long result = 0;
for (int i = 1; i <= 2019; ++i)
{
if (isContain(i))
{
result += i*i;
}
}
cout << result;
return 0;
}
1.3 答案
最终计算出的答案是:2658417853
2 试题B
2.1 题目
2.2 参考代码
#include <iostream>
using namespace std;
int main()
{
int arr[3]{ 1, 1, 1 };
int j = 0;
for (int i = 3; i < 20190324; ++i)
{
arr[j] = (arr[0] + arr[1] + arr[2]) % 10000; // 余上10000是因为题目要求只需要最后4位。除此之外,如果不余上10000,其中某些数组会超出计算机能表示的范围(即使使用long long类型也会超出)。
j = (j + 1) % 3;
}
cout << arr[(j+2) % 3];
return 0;
}
2.3 答案
2.4 注意
注意!如果你不余上10000来计算的话,中间某些数字会超出计算机能表示的范围,即使你使用long long也不行,如下图所示。