zoukankan      html  css  js  c++  java
  • 征战蓝桥 —— 2013年第四届 —— C/C++A组第2题——排它平方数

    题目标题: 排它平方数

    小明正看着 203879 这个数字发呆。
    
    原来,203879 * 203879 = 41566646641
    
    这有什么神奇呢?仔细观察,203879 是个6位数,并且它的每个数位上的数字都是不同的,并且它平方后的所有数位上都不出现组成它自身的数字。
    
    具有这样特点的6位数还有一个,请你找出它!
    
    再归纳一下筛选要求:
    1. 6位正整数
    2. 每个数位上的数字不同
    3. 其平方数的每个数位不含原数字的任何组成数位
    

    答案是一个6位的正整数。

    请通过浏览器提交答案。
    注意:只提交另一6位数,题中已经给出的这个不要提交。
    注意:不要书写其它的内容(比如:说明性的文字)。

    代码

    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    bool check(long long x, long long xx);
    
    void i2s(long long, string &basic_string);
    
    using namespace std;
    
    int main(int argc, const char *argv[]) {
        for (int i = 1; i < 10; ++i) {
            for (int j = 0; j < 10; ++j) {
                if (j != i)
                    for (int k = 0; k < 10; ++k) {
                        if (k != i && k != j)
                            for (int l = 0; l < 10; ++l) {
                                if (l != i && l != j && l != k)
                                    for (int m = 0; m < 10; ++m) {
                                        if (m != i && m != j && m != k && m != l)
                                            for (int n = 0; n < 10; ++n) {
                                                if (n != i && n != j && n != k && n != l && n != m) {
                                                    long long x = i * 100000 + j * 10000 + k * 1000 + l * 100 + m * 10 + n;
                                                    if (check(x, x * x)) {
                                                        cout << x << " " << x * x << endl;
                                                    }
                                                }
                                            }
                                    }
                            }
                    }
            }
        }
        return 0;
    }
    bool check(long long x, long long xx) {
        string s_x, s_xx;
        i2s(x, s_x);
        i2s(xx, s_xx);
        for (int i = 0; i < s_x.length(); ++i) {
            if (s_xx.find(s_x[i]) != string::npos) {
                return false;
            }
        }
        return true;
    }
    
    void i2s(long long x, string &basic_string) {
        stringstream ss;
        ss << x;
        ss >> basic_string;
    }
    

    代码分析

        for (int i = 1; i < 10; ++i) {
            for (int j = 0; j < 10; ++j) {
                if (j != i)
                    for (int k = 0; k < 10; ++k) {
                        if (k != i && k != j)
                            for (int l = 0; l < 10; ++l) {
                                if (l != i && l != j && l != k)
                                    for (int m = 0; m < 10; ++m) {
                                        if (m != i && m != j && m != k && m != l)
                                            for (int n = 0; n < 10; ++n) {
                                                if (n != i && n != j && n != k && n != l && n != m) {
                                                    long long x = i * 100000 + j * 10000 + k * 1000 + l * 100 + m * 10 + n;
                                                    if (check(x, x * x)) {
                                                        cout << x << " " << x * x << endl;
                                                    }
                                                }
                                            }
                                    }
                            }
                    }
            }
        }
    

    遍历六位数字的所有情况,并且六位数字各不相等,输出检查合格的数。

    void i2s(long long x, string &basic_string) {
        stringstream ss;
        ss << x;
        ss >> basic_string;
    }
    

    将long long类型的变量转换为字符串类型的变量。

    bool check(long long x, long long xx) {
        string s_x, s_xx;
        i2s(x, s_x);
        i2s(xx, s_xx);
        for (int i = 0; i < s_x.length(); ++i) {
            if (s_xx.find(s_x[i]) != string::npos) {
                return false;
            }
        }
        return true;
    }
    

    如果在x2中找到x包含的数字,返回false,否则返回true。

    简化升级

    #include <iostream>
    #include <sstream>
    #include <cstring>
    #include <string>
    using namespace std;
    void i2s(long long num,string &str)
    {
    	stringstream ss;
    	ss<<num;ss>>str;
    }
    bool check(long long i1,long long i2)
    {
        string s1,s2;
    	i2s(i1,s1);i2s(i2,s2);
    	for(long long i=0;i<s1.length();i++)
    		if(s2.find(s1[i])!=string::npos) return false;
    	return true;
    
    }
    int main()
    {
    	for(int a=1;a<10;a++)
    		for(int b=0;b<10;b++)
    		if(b!=a)
    			for(int c=0;c<10;c++)
    			if(c!=a&&c!=b)
    				for(int d=0;d<10;d++)
    				if(d!=a&&d!=b&&d!=c)
    					for(int e=0;e<10;e++)
    					if(e!=a&&e!=b&&e!=c&&e!=d)
    						for(int f=0;f<10;f++)
    						if(f!=a&&f!=b&&f!=c&&f!=d&&f!=e)
    						{
    							long long i1=a*100000+b*10000+c*1000+d*100+e*10+f;
    							long long i2=i1*i1;
    							if(check(i1,i2)) cout<<i1<<endl;
    						}
    	return 0;
    }
    

    639172

  • 相关阅读:
    获取Finacial dimension value的description 值
    创建一个List获取数据的lookup
    定位form光标行
    Business Unit Lookup in Form
    Linu各种版本
    redis的具体使用
    php中date()函数使用的方法
    Spring整合Hibernate中自动建表
    Android之手机电池电量应用
    SSH整合时,关于访问数据库的load的错误
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338993.html
Copyright © 2011-2022 走看看