1 条题解

  • 0
    @ 2021-6-15 10:09:22

    C :

    #include<stdio.h>
    
    int main()
    {
       int n,x;
       while(scanf("%d%d",&n,&x)!=EOF)
       {
    	 int count=0;
         for(int i=1;i<=n;i++)
    	 {
    	    for(int j=i;j>0;)
    		{
    		  int num=j%10;
    		  if(num==x)
    			  count++;
    		  j=j/10;
    		}
    	  }
    	 printf("%d\n",count);
       }
       return 0;
    }
    

    C++ :

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

    Pascal :

    var n,x,ans,i,x1:longint;
    begin
      read(n,x);
      ans:=0;
      for i:=1 to n do
        begin
          x1:=i;
          repeat
            if x1 mod 10=x then inc(ans);
            x1:=x1 div 10;
          until x1=0;
        end;
      writeln(ans);
    end.
    

    Java :

    
    import java.util.*;
    public class Main {
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		int count=0,b=0;
    	//	System.out.print(l);
    		int n=input.nextInt();
    		int m=input.nextInt();
    		int a[]=new int[n];
    		for(int i=0;i<n;i++) {
    			a[i]=i+1;
    			if(a[i]<10) {
    				if(a[i]==m) {
    					count++;
    				}
    			}else {
    				while(true) {
    				b=a[i]%10;
    				a[i]=a[i]/10;
    				if(b==m) {
    					count++;
    				}
    				if((a[i]+"").length()==1) {
    					if(a[i]==m) {
    						count++;
    					}
    					break;
    				}
    			}
    		}
    			}
    		System.out.print(count);
    	}
    
    }
    
    

    Python :

    def func(x, r):
    	res = 0
    	while (x):
    		if (x % 10 == r):
    			res += 1
    		x //= 10
    	return res
    
    s = raw_input()
    while (s != ""):
    	a = [int(i) for i in s.split()]
    	ans = 0
    	for x in xrange(1, a[0] + 1):
    		ans += func(x, a[1])
    	print ans
    	s = raw_input()
    
    • 1

    信息

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