zoukankan      html  css  js  c++  java
  • 计蒜客--两数之和

    给定一个数组 number_inumberi​​,找到两个数,使得他们的和为一个给定的数值 targettarget。

    其中:number[index_1] + number[index_2]==targetnumber[index1​​]+number[index2​​]==target。

    注意:index_1index1​​ 必须小于 index_2index2​​ 且不能为 00。假设每一组输入只有唯一的一组解。

    例如,对于数组 [2,7,11,15][2,7,11,15] 和 target=9target=9,index_1index1​​ 的值为 11,index_2index2​​ 的值为 22。

    输入格式

    第一行输入一个整数 n(1 leq n leq 500)n(1n500),接下来的两行分别输入 nn 个整数组成的数组 number_i(0 leq number_i leq 1000)numberi​​(0numberi​​1000) 和一个整数 target(0 leq target leq 1000)target(0target1000)。

    输出格式

    输出一行由空格分隔的两个整数 index1index1 和 index2index2。注意,下标从 11 开始。

    样例输入

    3
    5 75 25
    100

    样例输出

    2 3

    题解:分类的人有毒 分成排序 我一来就排序哇了好多发
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=10005;
    33 const int mod=1e9+7;
    34 int a[505];
    35 int main()
    36 {
    37     std::ios::sync_with_stdio(false);
    38     int n,m;
    39     cin>>n;
    40     for(int i=1;i<=n;i++)
    41         cin>>a[i];
    42     cin>>m;
    43 //    sort(a,a+n);
    44     for(int i=1;i<n;i++){
    45         for(int j=i+1;j<=n;j++){
    46             if(a[i]+a[j]==m){
    47                 cout<<i<<" "<<j<<endl;
    48                 return 0;
    49             }
    50         }
    51     }
    52 }
  • 相关阅读:
    DC中为什么要用Uniquify?
    hdu 1596 find the safest road
    hdu2112 HDU Today
    hdu 2066 一个人的旅行
    poj 3026 Borg Maze
    poj 1979 Red and Black
    poj 1321 棋盘问题
    hdu 1010 Tempter of the Bone
    hdu 4861 Couple doubi
    codeforces584B Kolya and Tanya
  • 原文地址:https://www.cnblogs.com/wydxry/p/7276824.html
Copyright © 2011-2022 走看看