zoukankan      html  css  js  c++  java
  • CSU 1785: 又一道简单题

    1785: 又一道简单题

            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-6;
    const int inf = 0x3f3f3f3f;
    const int N = 100005;
    int n, m, tot;
    int a[10550];
    int r[550], c[550];
    int x, y, pr, pc, k = 1;
    int ok(int x)
    {
        int tp=sqrt(x*1.0);
        return tp*tp==x;
    }
    int change(string s)
    {
        int sum = 0;
        for(int i=0; i<s.size(); i++)
        {
            sum = sum * 10 + s[i] - '0';
        }
        return sum;
    }
    int sq[100005];
    void init()
    {
        int k = 0;
        for(int i=32; i<100; i++)
            sq[k++] = i * i;
    }
    int main()
    {
    
        int t, k = 1;
        cin >> t;
        while(t--)
        {
            string s, s1, s2, s3, s4;
            int cnt = 0;
            cin>>s;
            int a,b,c,d;
            s1 = s;s2 = s;s3 = s;s4 = s;    //将s分别复制给中间变量
            a = s1[0]-'0'; b = s2[1]-'0'; c = s3[2]-'0'; d = s4[3]-'0'; //记录原4位数各个数位数值
            //printf("%d %d %d %d
    ",a,b,c,d);
            for(int i=1; i<=9 ; i++) //第一位改变不能和原来的相等&&第一位不能为0
            {
                if(i==a) continue; //注意!不能把i!=a写在for循环里面 一遇到a会直接跳出循环而漏掉情况!!!
                s1[0] = i + '0'; //改变第一位
                //cout<<s1[0]<<endl;
                if(ok(change(s1))) { //将字符串变为数值再判断是否为完全平方数
                    //printf("%d
    ",change(s1));
                    cnt++;
                }
            }
            for(int i=0; i<=9 ; i++)
            {
                if(i==b) continue;
                s2[1] = i + '0';
                //cout<<s2[1] <<endl;
                if(ok(change(s2))){
                    //printf("%d
    ",change(s2));
                    cnt++;
                }
            }
            for(int i=0; i<=9 ; i++)
            {
                if(i==c) continue;
                s3[2] = i + '0';
                //cout<<s3[2]<<endl;
                if(ok(change(s3))) {
                    //printf("%d
    ",change(s3));
                    cnt++;
                }
            }
            for(int i=0; i<=9 ; i++)
            {
                if(i==d) continue;
                s4[3] = i + '0';
                //cout<<s4[3]<<endl;
                if(ok(change(s4))) {
                    //printf("%d
    ",change(s4));
                    cnt++;
                }
            }
            printf("Case %d: ",k++);
            cout<<cnt<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    微信公众号项目部署
    数据库存入年月日时分秒类型时间问题
    路径问题
    常用DOS命令
    解决Maven下载慢的问题
    害人不浅的数学翻译
    Webpack4 踩坑记
    React 踩坑记
    what's the problem of Object oriented programming
    为什么JVM调优?
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8711644.html
Copyright © 2011-2022 走看看