1 条题解

  • 0
    @ 2023-6-11 12:15:53

    C :

    #include<stdio.h>
    int huiWen(int x)
    {
    	int h=0,t=x;
    
    	while(x>0){
    		h=h*10+x%10;
    		x/=10;
    	}
    	if(h==t){
    		return 1;
    	}else{
    		return 0;
    	}
    }
    int main()
    {
    	int n,i,c=0;
    	scanf("%d",&n);
    	for(i=1;i<=n;i++){
    		if(huiWen(i)){
    			c++;
    		}
    	}
    	printf("%d",c);
    	return 0;
    }
    

    C++ :

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    bool sushu(int n){
    	int i;
    	bool f=true;
    	for(i=2;i<=sqrt(n);i++){
    		if(n%i==0){
    			f=false;
    			break;
    		}
    	}
    	if(n<=1){
    		f=false;
    	}
    	return f;
    }
    bool huiwen(int n){
    	bool f=false;
    	int g=n%10,s=n/10%10,b=n/100%10,q=n/1000%10,w=n/10000;
    	if(n<=99&&g==s||n<=9){
    		f=true;
    	}
    	else if(n>99&&n<=999&&g==b){
    		f=true;
    	}
    	else if(n>999&&n<=9999&&g==q&&s==b){
    		f=true;
    	}
    	else if(n>=10000&&g==w&&s==q){
    		f=true;
    	}
    	return f;
    	
    }
    int main(){
    	int n,i,c=0;
    	cin>>n;
    	for(i=1;i<=n;i++){
    		if(huiwen(i)==true){
    			c++;
    		}
    	}
    	cout<<c<<endl;
    }
    

    Python :

    def dcf(x):
        a = []
        while x > 0:
            s = x % 10
            x //= 10
            a.insert(0, s)
        return a
    
    
    n = int(input())
    l = []
    s = 0
    c = 0
    
    for i in range(10, n + 1):
        l = dcf(i)
        s = len(l)
        if s == 2 and l[0] == l[1]:
            c += 1
        elif s == 3 and l[0] == l[2]:
            c += 1
        elif s == 4 and l[0] == l[3] and l[1] == l[2]:
            c += 1
    print(c + 9)
    
    • 1

    信息

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