1 条题解

  • 0
    @ 2021-6-15 1:39:53

    C :

    #include<math.h>
    #include <stdio.h>
    int sum[10000]={0};
    int count(int n)
    {
    	int x=0;
    	while(n)
    	{	
    		if(n%10==2)x++;
    		n/=10;
    	}
    	return x;
    }
    void main()
    { 
    	int i;
    	int s,t;	
    	for(i=0;i<10001;i++)
    	{	
    		sum[i]=count(i);
    		sum[i]+=sum[i-1];
    	}
    	while(scanf("%d%d",&s,&t)!=EOF)
    	{	
    		printf("%d\n",sum[t]-sum[s-1]);
    	}
    }
    

    C++ :

    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
    	int l,r;
    	char buf[10];
    	int two = 0;
    	cin >> l >> r;
    	for(int i=l; i<=r; ++i)
    	{
    		int len = sprintf(buf,"%d",i);
    		for(int j=0; j<len; ++j)
    		{
    			if(buf[j] == '2')
    				two++;
    		}
    	}
    	cout << two << endl;
    	return 0;
    }
    
    

    Pascal :

    var  l,r,i,n,t:integer;
    begin
       while not(eof) do
       begin
         t:=0;
         readln(l,r);
         for i:=l to r do
         begin
           n:=i;
           while n>0 do
           begin
             if n mod 10=2 then inc(t);
             n:=n div 10;
           end;
         end;
         writeln(t);
       end;
    end.
    
    
    
    
    

    Java :

    import java.util.Scanner;
    
    
    public class Main {
    
    	static int  Count(int num,int find){
    		String strNum = num+"";
    		String strFind = find+"";
    		int fromIndex = 0;
    		int count = 0;
    		while(true){
    			int pos = strNum.indexOf(strFind, fromIndex);
    			if(pos != -1){
    				fromIndex=pos+1;
    				count++;
    			}
    			else
    				break;
    		}
    		return count;
    		
    	}
    	public static void main(String[] args) {
    		Scanner s = new Scanner(System.in);
    		int find = 2;
    		int count = 0;
    		while(s.hasNext()){
    			int L = s.nextInt();
    			int R = s.nextInt();
    			for(int i=L;i<=R;i++){
    				count += Count(i,find);
    			}
    			System.out.println(count);
    			count = 0;
    		}
    
    	}
    
    }
    
    
    • 1

    信息

    ID
    276
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者