HJ99 自守数(c++/python)

HJ99 自守数(c++/python)_第1张图片

c++:

#include
using namespace std;

int main(){
    int n;
    cin >> n;
    int count = 0;
    for(int i = 0; i <= n; i++){
        int j = i;
        int pow_i = i*i;
        while(j){
            if(j%10 == pow_i%10){
                j = j/10;
                pow_i = pow_i/10;
                
            }else{
                break;
            }
        }
        if(j == 0){
            count++;
        }
    }
    cout << count;
}

python:

while True:
    try:
        n = int(input())
        cnt = 0
        for i in range(n+1):
            if(str(i*i).endswith(str(i))):
                cnt += 1
        print(cnt)
    except:
        break

你可能感兴趣的:(力扣刷题笔记,c++,python)