zoukankan      html  css  js  c++  java
  • HDU 1270 小希的数表 (暴力枚举+数学)

    题意:...

    析:我们可以知道,a1+a2=b1,那么我们可以枚举a1,那么a2就有了,并且a1+a3=b2,所以a3就有了,我们再从把里面的剩下的数两两相加,并从b数组中去掉,

    那么剩下的最小的就是a4,然后依次可以求出a5,a6....由于a最大才是5000,并且保证有唯一解,那么找到一个就直接退出。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <unordered_map>
    #include <unordered_set>
    #define debug() puts("++++");
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e4 + 5;
    const int mod = 2000;
    const int dr[] = {-1, 1, 0, 0};
    const int dc[] = {0, 0, 1, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int b[maxn], c[maxn];
    int a[maxn];
    
    bool judge(int x){
        a[1] = x;   a[2] = b[1] - a[1];
        int cnt = 2;
    
        for(int i = 2; i <= m; ++i){
            if(b[i])  a[++cnt] = b[i] - a[1];
            else continue;
            for(int j = 2; j < cnt; ++j){
                bool ok = false;
                for(int k = i+1; k <= m; ++k)  if(a[j] + a[cnt] == b[k]){
                    b[k] = 0;
                    ok = true;
                    break;
                }
                if(!ok)  return ok;
            }
        }
        return true;
    }
    
    int main(){
        while(scanf("%d", &n) == 1 && n){
            m = n * (n-1) / 2;
            for(int i = 1; i <= m; ++i)  scanf("%d", c+i);
            for(int i = 1; ; ++i){
                memcpy(b+1, c+1, 4*m);
                if(judge(i))  break;
            }
            for(int i = 1; i <= n; ++i)
                if(i == 1)  printf("%d", a[i]);
                else printf(" %d", a[i]);
            printf("
    ");
    
        }
        return 0;
    }
    
  • 相关阅读:
    WCF系列之双工通信 牧羊人
    WCF系列之承载(IISHTTP) 牧羊人
    在Ubuntu 10.10下安装JDK配置Eclipse及Tomcat【转载 + 订正】
    gconfeditor简介【转载】
    Ubuntu10.10(linux) 安装jdk1.6及环境变量的设置【转载】
    ubuntu下txt文件中文显示乱码的方法【转载】
    程序员的编辑器——VIM【转载】
    xx is not in the sudoers file 问题解决【转载】
    win7下配置PHP+apache+mysql【转载 + 补充】
    ubuntu忘记密码,忘记root密码的解决方法!【转载】
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6274811.html
Copyright © 2011-2022 走看看