zoukankan      html  css  js  c++  java
  • 51Nod 1090 3个数和为0 + Atcoder Tenka1 Programmer Contest N/4 暴力枚举+二分

    思路都是一样的,枚举要求的结果中的一个数,其余的在此基础上二分,很常见的方法,也很容易想到,然而……还是要练啊,,,

    大概……一般……也许……不会T吧……

    51Nod 1090  3个数和为0 传送门

    直接贴代码~

    #include<iostream>   
    #include<algorithm>
    #include<vector> 
    #include<string.h>
    #include<stack>
    using namespace std;
    typedef long long ll;
    const int MAX = 1e3 + 5;
    int n;
    ll a[MAX];
    int main()
    {
        ios::sync_with_stdio(false);
        while (cin >> n)
        {
            for (int i = 0; i<n; i++)
                cin >> a[i];
            int flag = 1;
            sort(a, a + n);
            for (int i = 0; i<n; i++) //暴力枚举a[i],a[j],二分a[k] 
            {
                for (int j = i + 1; j<n; j++)
                {
                    ll x = -a[i] - a[j]; //记得用long long 不然会WA 
                    int l = j + 1, r = n - 1, mid;
                    while (l <= r)
                    {
                        mid = (l + r) / 2;
                        if (a[mid] < x) l = mid+1;
                        else if(a[mid]>x) r = mid - 1;
                        else {
                            flag = 0;
                            cout << a[i] << " " << a[j] << " " << a[mid] << endl;
                            break;
                        }
                    }
                }
            }
            if (flag)
                cout << "No Solution" << endl;
        }
        return 0;
    }
    //抄来的代码~感觉这个方法更好一些,以后如果有类似的三个数的问题可以用这个
    #include<iostream> #include<algorithm> #include<vector> #include<string.h> #include<stack> using namespace std; typedef long long ll; const int MAX=1e3+5; int n,a[MAX]; int main() { ios::sync_with_stdio(false ); while(cin>>n) { for(int i=0;i<n;i++) cin>>a[i]; int flag=1; sort(a,a+n); for(int i=0;i<n;i++) { int l=i+1,r=n-1,x; while(l<r) { x=a[i]+a[l]+a[r]; if(x>0) r--; else if(x<0) l++; else { flag=0; cout<<a[i]<<" "<<a[l]<<" "<<a[r]<<endl; l++;r--; } } } if(flag) cout<<"No Solution"<<endl; } return 0; }

    Atcoder Tenka1 Programmer Contest C:4/N 传送门 

    #include<iostream>   
    #include<algorithm>
    #include<vector> 
    #include<string.h>
    #include<stack>
    using namespace std;
    typedef long long ll;
    const int MAX = 1e3 + 5;
    ll n;
    int main()
    {
        ios::sync_with_stdio(false);
        while (cin >> n)
        {
            int flag = 0;
            for (ll h = 1; h < 3505; h++)
            {
                for (ll k = 1 ; k < 3505; k++)
                {
                    if ((4 * h*k - n*k - n*h) != 0) //这里要注意啊……分母不能为0
                    {
                        ll  w = (n*h*k) / (4 * h*k - n*k - n*h);
                        if (w > 0 && (n*h*k) % (4 * h*k - n*k - n*h) == 0)//这里WA了好多次……不太懂为什么正数和正数作除法会得负数……orz……一开始没判正负一直在WA
                        {
                            flag = 1;
                            cout << h << " " << k << " " << w << endl;
                            break;
                        }
                    }
                }
                if (flag) break;
            }
        }
        return 0;
    }
  • 相关阅读:
    利用Spark-mllab进行聚类,分类,回归分析的代码实现(python)
    c#项目返回文件案例
    设计模式 —— 组合模式
    设计模式 —— 备忘录
    设计模式 ——状态模式
    设计模式 —— 中介者模式
    设计模式 —— 适配器
    设计模式 —— 代理模式
    设计模式 ——门面模式
    设计模式 —— 享元模式
  • 原文地址:https://www.cnblogs.com/Egoist-/p/7627225.html
Copyright © 2011-2022 走看看