链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1785
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
思路:比较水,四个位置依次判断即可,要注意的是首位不能为0,每一位改完的数不能和改之前一样
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#define LL __int64
using namespace std;
int t;
int n;
int main() {
scanf("%d", &t);
int p = 1;
while (t--) {
scanf("%d", &n);
printf("Case %d: ", p++);
int sum = 0;
int num;
int j;
int k = 0;
for (int i = 1; i <= 4; i++) {
if (i == 1)
j = 1;
else j = 0;
for (; j < 10; j++) {
if (i == 1){
num = j * 1000 + n % 1000;
if (j == n / 1000)
continue;
}
else if (i == 2){
if (j == n / 100 % 10)
continue;
num = j * 100 + n / 1000 * 1000 + n % 100;
}
else if (i == 3) {
if (j == n / 10 % 10)
continue;
num = j * 10 + n / 100 * 100 + n % 10;
}
else {
if (j == n % 10)
continue;
num = j + n / 10 * 10;
}
k = sqrt(num);
if (num == k*k)
sum++;
}
}
cout << sum << endl;
}
system("pause");
return 0;
}