NBUT 1647
Description
输入一个四个数字组成的整数 n,你的任务是数一数有多少种方法,恰好修改一个数字,把它 变成一个完全平方数(不能把首位修改成 0)。比如 n=7844,有两种方法:3844=62^2和 7744=88^2。
Input
Output
Sample Input
2 7844 9121
Sample Output
Case 1: 2 Case 2: 0
用2层for循环,分别控制个十百千位上的数字变化。且每次只改变一个位置上的数字,并且要把之前原始位置上的数字标记一下。避免判断n值。
My solution:/*2016.3.19*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int x[19],mark[19],y[19];
int main()
{
int i,j,k,n,t,p,r,q,h,ans,l;
scanf("%d",&t);
y[1]=1000;
y[2]=100;
y[3]=10;
y[4]=1;
for(i=1;i<=t;i++)
{
ans=0;
scanf("%d",&p);
q=p;
for(j=4;j>=1;j--)//把数转化到数组 中保存
{
x[j]=q%10;
q/=10;
}
for(j=1;j<=4;j++)//决定改变哪个位置上的数字
{
memset(mark,0,sizeof(mark));
q=p;
q=q-x[j]*y[j];
if(j==1)//如果改变千位上的数字,则千位不能为0
mark[0]=1;
mark[x[j]]=1;//标记原来位置上的数字
for(k=0;k<=9;k++)//从0~9依次查询
{
if(!mark[k])
{
h=q+k*y[j];//得到的新的四位数
l=sqrt(h);
if(l*l==h)
{
ans++;
}
}
}
}
printf("Case %d: %d\n",i,ans);
}
return 0;
}