2 条题解

  • 1
    @ 2022-7-17 10:18:05

    C++ :

    #include<iostream>
    using namespace std;
    int main()
    {
    	int L,R,x,s,a;
    	cin>>L>>R;
    	x=0;
    	for(int i=L;i<=R;i++)
    	{
    		s=i;
    		while(s!=0)
    		{
    		a=s%10;
    		if(a==2) x=x+1;
    		s=s/10;
    		}
    	}
    	cout<<x<<endl;
    	return 0;
    }
    

    Python :

    # coding=utf-8
    a,b=map(int,input().split())
    c=0
    for i in range(a,b+1):
        while i>0:
            if i%10==2:
                c+=1
            i//=10
    print(c)
    
    • 0
      @ 2022-12-20 14:12:16

      你知道的 字符串实现的代码量要少很多

      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
          int l,r;
          int cnt=0;
          cin >> l >> r;
          for(int i=l;i<=r;i++)
          {
              string a=to_string(i);
              for(int j=0;j<a.length();j++)
                  if(a[j]=='2') cnt++;
          }
          cout << cnt;
          return 0;
      }
      
      • 1

      信息

      ID
      762
      时间
      1000ms
      内存
      64MiB
      难度
      4
      标签
      递交数
      287
      已通过
      137
      上传者