CSU 1785: 又一道简单题
2024-09-24 09:55:45
1785: 又一道简单题
Submit Page Summary Time Limit: 5 Sec Memory Limit: 128 Mb Submitted: 602 Solved: 234
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
Hint
Source
湖南省第十一届大学生计算机程序设计竞赛
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<sstream>
#include<queue>
#include<cmath>
#include<list>
#include<vector>
#include<string>
using namespace std;
#define long long ll
const double PI = acos(-1.0);
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int N = ;
int n, m, tot;
int a[];
int r[], c[];
int x, y, pr, pc, k = ;
int ok(int x)
{
int tp=sqrt(x*1.0);
return tp*tp==x;
}
int change(string s)
{
int sum = ;
for(int i=; i<s.size(); i++)
{
sum = sum * + s[i] - '';
}
return sum;
}
int sq[];
void init()
{
int k = ;
for(int i=; i<; i++)
sq[k++] = i * i;
}
int main()
{ int t, k = ;
cin >> t;
while(t--)
{
string s, s1, s2, s3, s4;
int cnt = ;
cin>>s;
int a,b,c,d;
s1 = s;s2 = s;s3 = s;s4 = s; //将s分别复制给中间变量
a = s1[]-''; b = s2[]-''; c = s3[]-''; d = s4[]-''; //记录原4位数各个数位数值
//printf("%d %d %d %d\n",a,b,c,d);
for(int i=; i<= ; i++) //第一位改变不能和原来的相等&&第一位不能为0
{
if(i==a) continue; //注意!不能把i!=a写在for循环里面 一遇到a会直接跳出循环而漏掉情况!!!
s1[] = i + ''; //改变第一位
//cout<<s1[0]<<endl;
if(ok(change(s1))) { //将字符串变为数值再判断是否为完全平方数
//printf("%d\n",change(s1));
cnt++;
}
}
for(int i=; i<= ; i++)
{
if(i==b) continue;
s2[] = i + '';
//cout<<s2[1] <<endl;
if(ok(change(s2))){
//printf("%d\n",change(s2));
cnt++;
}
}
for(int i=; i<= ; i++)
{
if(i==c) continue;
s3[] = i + '';
//cout<<s3[2]<<endl;
if(ok(change(s3))) {
//printf("%d\n",change(s3));
cnt++;
}
}
for(int i=; i<= ; i++)
{
if(i==d) continue;
s4[] = i + '';
//cout<<s4[3]<<endl;
if(ok(change(s4))) {
//printf("%d\n",change(s4));
cnt++;
}
}
printf("Case %d: ",k++);
cout<<cnt<<endl;
}
return ;
}
最新文章
- JavaScript 全栈工程师培训教程(来自阮一峰)
- ShareSdk使用心得
- css中visiblity和display异同
- centos7重启rsyslog服务|centos7重启syslog服务
- malloc、calloc、realloc的区别
- sql server 2016 management studio没有的解决方式
- 基于Tengine的反向代理详细配置
- 一个简单的Inno Setup例子
- linux shell 中";2>;&;1";含义
- Linux:备份
- jQuery原型技术分解
- 常用的十大Python开发工具
- commons-lang3 事件机制 <;EventListenerSupport>;
- openstack-mitaka部署
- odoo qweb 记录
- SwipeableFlatList 实现类似于QQ列表滑动
- 利用mycat实现基于mysql5.5主从复制的读写分离
- MyCAT简易入门 (Linux)
- centos 安装elk监控
- Docker容器学习与分享10
热门文章
- B1056 组合数的和 (15分)
- HDU 6153 KMP
- appcompat_v7\res\values-v21\themes_base.xml:158: error: Error: No resource
- json对象数据列数
- 极简Node教程-七天从小白变大神(二:中间件是核心)
- 【Restore IP Addresses 】cpp
- easyui在datagrid只想选择一条
- 在控制器“xxxx”上找不到与该请求匹配的操作
- httpClient get方式抓取数据
- 一个 Observation