又一道简单题

来源:互联网 发布:笔记本风扇除尘软件 编辑:程序博客网 时间:2024/04/28 00:59
输入一个四个数字组成的整数 n,你的任务是数一数有多少种方法,恰好修改一个数字,把它 变成一个完全平方数(不能把首位修改成 0)。比如 n=7844,有两种方法:3844=62^2和 7744=88^2。

Input
输入第一行为整数 T (1<=T<=1000),即测试数据的组数,以后每行包含一个整数 n (1000<=n<=9999)。

Output
对于每组数据,输出恰好修改一个数字,把 n 变成完全平方数的方案数。

Sample Input
2
7844
9121
Sample Output
Case 1: 2

Case 2: 0


直接用暴力枚举

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int work(int n){    int y=sqrt(n);    if(y*y==n)        return 1;    else    {        return 0;    }}int main(){    int n,m,i,j,t;    int a[6],b[6];    scanf("%d",&t);    m=1;    while(t--)    {        int sum=0;        scanf("%d",&n);        printf("Case %d: ",m);        m++;        a[0]=(n/1000)%10;        a[1]=(n/100)%10;        a[2]=(n/10)%10;        a[3]=(n)%10;        for(b[0]=1; b[0]<=9; b[0]++)        {            for(b[1]=0; b[1]<=9; b[1]++)            {                for(b[2]=0; b[2]<=9; b[2]++)                {                    for(b[3]=0; b[3]<=9; b[3]++)                    {                        int f=0;                        if(b[0]==a[0])                            f++;                        if(b[1]==a[1])                            f++;                        if(b[2]==a[2])                            f++;                        if(b[3]==a[3])                            f++;                        if(f==3)                        {                            int k=b[0]*1000+b[1]*100+b[2]*10+b[3];                            int t=work(k);                            if(t==1)                                sum++;                        }                    }                }            }        }        printf("%d\n",sum);    }}




原创粉丝点击