2 条题解

  • 0
    @ 2024-4-8 10:52:12
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    int n=0;
    for(int i=1;i<2021;i++)
    {
    int b;
    b=i;
    while(b)
    {
    if(b%10==2)
    {
    n++;
    }
    b=b/10;
    }
    }
    cout<<n<<endl;
    return 0;
    }
    
    • 0
      @ 2024-4-7 0:58:01

      【2020年省赛B组】试题A: 门牌制作

      题解

      简单模拟题。

      • 枚举数字120221\sim 2022,把每个数按照十进制分解,再统计2出现的次数即可。
      • 也可以将每个数字都转换为字符串,然后直接统计这些字符串总共有多少个字符2。

      最后的答案为624

      提交代码

      #include<bits/stdc++.h>
      using namespace std;
      int get(int x)
      {
      	int cnt = 0;
      	while(x)
      	{
      		if(x % 10 == 2) cnt++;
      		x /= 10;
      	}
          return cnt;
      }
      
      signed main()
      {
          ios::sync_with_stdio(false);
          cin.tie(0);
      	int ans = 0;
      	for(int i = 1; i <= 2020; i++)
      	{
      		ans += get(i);	
      	}
      	cout << ans << "\n";
          return 0;
      }
      
      • 1

      信息

      ID
      986
      时间
      1000ms
      内存
      256MiB
      难度
      1
      标签
      递交数
      112
      已通过
      47
      上传者