zoukankan      html  css  js  c++  java
  • [蓝桥杯2016初赛]四平方和

    以下几种解法:

    参考博客

    https://blog.csdn.net/Tianweidadada/article/details/79748074

    一、暴力:

    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #define N 5000009
    
    using namespace std;
    
    int main() {
        int n;
        int ans[4];
        int flag;
        while(cin >> n) {
            flag = 1;
            int max = sqrt(n);
            cout << max << endl;
            for (int a = 0; a <= max && flag;a++)
                for (int b = 0; b <= max && flag;b++)
                    for (int c = 0; c <= max && flag;c++)
                        for (int d = 0; d <= max && flag;d++) {
                            if (a*a + b*b + c*c + d*d == n) {
                                ans[0] = a;
                                ans[1] = b;
                                ans[2] = c;
                                ans[3] = d;
                                flag = 0;
                            }
                        }
            sort(ans, ans+4);
            cout << ans[0] << " ";
            cout << ans[1] << " "; 
            cout << ans[2] << " "; 
            cout << ans[3] << endl;
        }
        return 0;
    }
    View Code

    二、(参考的博客)

    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #define N 5000009
    
    using namespace std;
    
    int main() {
        int n;
        int ans[4];
        int flag;
        while(cin >> n) {
            flag = 1;
            int max = sqrt(n);
            for (int a = 0; a <= max && flag;a++)
                for (int b = 0; b <= max && flag;b++)
                    for (int c = 0; c <= max && flag;c++) {
                        int s = n - (a*a + b*b + c*c);
                        double l = sqrt((double) s);//第二次技巧
                        if(l == (int)l ) {
                            ans[0] = a;
                            ans[1] = b;
                            ans[2] = c;
                            ans[3] = l;
                            flag = 0;
                        }
                    }
            sort(ans, ans+4);
            cout << ans[0] << " ";
            cout << ans[1] << " "; 
            cout << ans[2] << " "; 
            cout << ans[3] << endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    关于Js异常
    gitea windows 安装
    spring boot 错误页面配置
    mysql 常用用函数
    nginx 安装 tomcat pfx 格式证书
    git pull 报错
    maven 打 jar 包,包含 xml, 包含 额外 jar
    git clone 分支代码
    git 切换远程分支
    mycat 在 mysql 8.0 下 无法连接 bug
  • 原文地址:https://www.cnblogs.com/hello-dummy/p/13233036.html
Copyright © 2011-2022 走看看