题解:UVA13140 Squares, Lists and Digital Sums

· · 题解

思路:

枚举每个起点,如果符合要求就输出。

代码:

for(int i=1;;i++){
        if(cheak(i*i)
        &&cheak((i+1)*(i+1))
        &&cheak((i+2)*(i+2))
        &&cheak((i+3)*(i+3))
        &&cheak((i+4)*(i+4))
        &&cheak((i+5)*(i+5))
        &&cheak((i+6)*(i+6))){
            cout<<i<<" "<<i*i<<endl;
            cout<<i+1<<" "<<(i+1)*(i+1)<<endl;
            cout<<i+2<<" "<<(i+2)*(i+2)<<endl;
            cout<<i+3<<" "<<(i+3)*(i+3)<<endl;
            cout<<i+4<<" "<<(i+4)*(i+4)<<endl;
            cout<<i+5<<" "<<(i+5)*(i+5)<<endl;
            cout<<i+6<<" "<<(i+6)*(i+6)<<endl;
            break;
        }
    }

暴力枚举。

int cheak(int x){
    ans=0;
    while(x>0){
        ans+=x%10;
        x/=10;
    }
    int s=int(sqrt(ans));
    return ans==s*s;
}

判断函数。