zoukankan      html  css  js  c++  java
  • 算法初步——哈希表A1048Find Coins

    思路: two pointers

    #include <bits/stdc++.h>
    #include<math.h>
    #include <string>
    using namespace std;
    const int MAX_LEN = 100005;
    bool cmp(int a,int b){
        return a>b;
    }
    int main(){
        //int temp[MAX_LEN];
        /*for(int i=0;i<MAX_LEN;++i){
            temp[i] = 0;
        }*/
        int n,m;
        cin>>n;
        cin>>m;
        int temp[n];
        for(int i=0;i<n;++i){
            cin>>temp[i];
        }
        sort(temp,temp+n,cmp);
        /*int count = 0;
        for(int i=0;i<MAX_LEN;++i){
            if(temp[i] > 0){
                count++;
            }
        }*/
        int fir = n-1;
        int end = 0;
        while(end != fir){
            if(temp[fir] + temp[end] < m){
                fir--;
            }
            if(temp[fir] + temp[end] > m){
                end++;
             }
             if(temp[fir] + temp[end] == m){
                 cout<<temp[fir]<<" "<<temp[end];
                 break;
             }   
        }
        if(end == fir){
            cout<<"No Solution";
        }
        system("pause");
        return 0;
    } 

    注意要continue

    #include <bits/stdc++.h>
    #include<math.h>
    #include <string>
    using namespace std;
    const int MAX_LEN = 100005;
    bool cmp(int a,int b){
        return a>b;
    }
    int main(){
        //int temp[MAX_LEN];
        /*for(int i=0;i<MAX_LEN;++i){
            temp[i] = 0;
        }*/
        int n,m;
        cin>>n;
        cin>>m;
        int temp[n];
        for(int i=0;i<n;++i){
            cin>>temp[i];
        }
        sort(temp,temp+n,cmp);
        /*int count = 0;
        for(int i=0;i<MAX_LEN;++i){
            if(temp[i] > 0){
                count++;
            }
        }*/
        int fir = n-1;
        int end = 0;
        while(end != fir){
            if(temp[fir] + temp[end] < m){
                fir--;
                continue;//注意要continue 
            }
            if(temp[fir] + temp[end] > m){
                end++;
                continue;//注意要continue 
             }
             if(temp[fir] + temp[end] == m){
                 cout<<temp[fir]<<" "<<temp[end];
                 break;
             }   
        }
        if(end == fir){
            cout<<"No Solution";
        }
        system("pause");
        return 0;
    } 
  • 相关阅读:
    强烈推荐:240多个jQuery插件【转】
    逆变与协变详解
    Mac入门 (二) 使用VMware Fusion虚拟机
    JQUERY UI DOWNLOAD
    几个常用Json组件的性能测试
    jQuery.extend 函数详解
    获取Portal中POWL程序的APPLID
    设计师和开发人员更快完成工作需求的20个惊人的jqury插件教程(上)
    Linux kernel中网络设备的管理
    mongodb修改器
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12163089.html
Copyright © 2011-2022 走看看