
- #include
- using namespace std;
- int ans;
- int main()
- {
- for(int i = 1; i <= 2020; i ++)
- {
- int x = i;
- while(x)
- {
- int a = x % 10;
- if(a == 2)ans ++;
- x /= 10;
- }
- }
- cout << ans;
- return 0;
- }

我们由题意暴力枚举
- #include
- using namespace std;
- long long ans;
- long long gcd(int a, int b)
- {
- return b == 0 ? a : gcd(b, a % b);
- }
- int main()
- {
- for(int i = 1; i <= 2020; i ++)
- {
- for(int j = 1; j <= 2020; j ++)
- {
- if(gcd(i, j) == 1)
- {
- ans ++;
- }
- }
- }
- cout << ans;
- return 0;
- }


观察图片,我们可以将其分为三大类(r为行,c为列, nextpos代表下一个数的位置
1.第一行
①r = 1, c奇数nextpos(r, c + 1)
②r = 1, c偶数nextpos(r + 1, c - 1)
2.第一列
①c = 1, r偶数nextpos(r + 1, c)
②c = 1,r奇数&&r != 1nextpos(r - 1, c + 1)
3.中间
①r != 1, c != 1, r + c 是奇数(r + 1, c - 1)
②r != 1, c != 1, r + c 是偶数(r - 1, c + 1)
对于这种中间情况举几个例子:
5(2, 2) : 2 + 2 = 4
14(2, 4) : 2 + 4 = 6
13(3, 3) : 3 + 3 = 6
12(4, 2) : 4 + 2 = 6
8(2, 3) : 2 + 3 = 5
9(3, 2) : 3 + 2 = 5
18(3, 4) : 3 + 4 = 7
答案 :761
- #include
- using namespace std;
-
- int main()
- {
- int ans = 1;
- int r = 1, c = 1;
- while(r != 20 || c != 20)
- {
- if(r == 1)
- {
- if(c & 1)c ++;
- else r ++, c --;
- }
- else if(c == 1)
- {
- if(r % 2 == 0)r ++;
- else r --, c ++;
- }
- else if((r + c) % 2) r ++, c--;
- else r --, c ++;
- ans ++;
- }
- cout << ans;
- return 0;
- }