Description
输入一个四个数字组成的整数 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
将n变化后的数字举出再进行判断即可
#include <iostream>
#include <math.h>
using namespace std;
inline bool f(int x)//判断是否是完全平方数
{
int t=ceil(sqrt(x));
if(t*t==x) return true;
else return false;
}
int main()
{
int T,n;
cin>>T;
for(int i=1;i<=T;i++)
{
int way=0,ans=10,dns=1,temp;
cin>>n;
cout<<"Case "<<i<<": ";
for(int j=1;j<=4;j++)
{
int k=(n%ans)/dns; //分别取出个十百千四位的数字
temp=n-k*dns;
for(int cc=0;cc<=9;cc++)
{
if(!(cc==0&&j==4)&&(cc!=k)) //只有当不是它本身且千位不为0时才进行判断
if(f(temp+cc*dns)) //如果是平方数,++way
++way;
}
ans=ans*10;dns=dns*10;
}
cout<<way<<endl;
}
}
对于取出n的四位数字可在读入n之后就计算出来,不用像上面一样在循环中再去计算