80 条题解

  • 2
    @ 2020-09-13 13:52:16

    充分利用各种输入形式

    #include <iostream>
    #include <unordered_map>
    #include <string>
    #include <vector>
    #include <set>
    #include <algorithm>
    using namespace std;
    struct node{
    int id;
    string x;
    }arr[1001];
    bool cmp(node a, node b){
    return a.id < b.id;
    }
    int main(){
    int n;
    int i = 0;
    while(1){
    scanf("%d",&arr[i].id);
    i++;
    if(getchar() == '\n')
    break;
    }
    for(int j = 0; j < i; j++){
    cin >> arr[j].x;
    }
    sort(arr, arr+i, cmp);
    for(int j = 0; j < i; j++)
    cout << arr[j].x << endl;
    return 0;
    }

  • 2
    @ 2020-02-09 12:08:59
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<string>
    #include<stdlib.h>
    using namespace std;
    
    template<class T>
    void change(T &a, T &b)
    {
        T c=a;
        a=b;
        b=c;
    }
     
    int main()
    {
        vector<string> array;
        string temp;
        while(cin>>temp)
            array.push_back(temp);
    /*
    
        int n=0;
        while(n<10)
        {
            string temp;
            cin>>temp;
            array.push_back(temp);
            n++;
        }
    */      
        int amount=array.size()/2;
        int order[amount];
        string code[amount];
        for(int i=0;i<array.size();i++)
        {
            if(i<amount)
            {
                temp=array[i];
                order[i]=atoi(temp.c_str());
            }
            else
            {
                code[i-amount]=array[i];
            }
        }
        
    
        for(int i=1;i<amount;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(order[i]<order[j])
                {
                    change(order[i], order[j]);
                    change(code[i], code[j]);
                }
            }
        }
        
        for(int i=0;i<amount;i++)
            cout<<code[i]<<endl;
    }
    
    
  • 1
    @ 2023-10-07 23:35:43
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<string>
    #include<stdlib.h>
    using namespace std;
    
    template<class T>
    void change(T &a, T &b)
    {
        T c=a;
        a=b;
        b=c;
    }
     
    int main()
    {
        vector<string> array;
        string temp;
        while(cin>>temp)
            array.push_back(temp);
    /*
    
        int n=0;
        while(n<10)
        {
            string temp;
            cin>>temp;
            array.push_back(temp);
            n++;
        }
    */      
        int amount=array.size()/2;
        int order[amount];
        string code[amount];
        for(int i=0;i<array.size();i++)
        {
            if(i<amount)
            {
                temp=array[i];
                order[i]=atoi(temp.c_str());
            }
            else
            {
                code[i-amount]=array[i];
            }
        }
        
    
        for(int i=1;i<amount;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(order[i]<order[j])
                {
                    change(order[i], order[j]);
                    change(code[i], code[j]);
                }
            }
        }
        
        for(int i=0;i<amount;i++)
            cout<<code[i]<<endl;
    }
    
    
    
  • 1
    @ 2021-12-04 15:11:51

    这道题不难,重点就是输入,处理好输入就基本上没有问题了

    代码 :

    #include<bits/stdc++.h>
    using namespace std;
    int q[10001], n, K;
    string s[10001];
    int main() {
        cin.tie(0);
        while (true) {
            K++;
            scanf("%d", &q[K]);
            n = max(n, q[K]);
            if (getchar() == '\n') {
                break;
            }
        }
        for (int i = 1; i <= n; i++) {
            cin >> s[q[i]];
        }
        for (int i = 1; i <= n; i++) {
            cout << s[i];
            printf("\n");
        }
        return 0;
    }
    
  • 1
    @ 2021-03-19 18:19:57
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <deque>
    #include <set>
    #include <limits>
    #include <string>
    #include <sstream>
    using namespace std;
    
    namespace dts
    {
        class letter{
        public:
            int num;
            char name[1<<10];
        };
        
        int n,num[1<<10];
        letter lt[1<<10];
        
        void init()
        {
            int size=0;
            char s[1<<20];
            memset(s,0,sizeof(s));
            for (char c;(c=getchar())!='\n';size++)
                s[size]=c;
            n=0;
            for (int i=0;i<size;i++)
            {
                sscanf(&s[i],"%d",&lt[n++].num);
                while (i<size)
                    if (s[i]!=' ')
                        i++;
                    else
                        break;
            }
            for (int i=0;i<n;i++)
            {
                num[i]=i;
                memset(lt[i].name,0,sizeof(lt[i].name));
                scanf("%s",lt[i].name);
            }
        }
        int cmplt(letter a,letter b)
        {
            return a.num<b.num;
        }
        void qs(int l,int r)
        {
            int i,j;
            letter mid=lt[num[(l+r)>>1]];
            for (i=l,j=r;i<j;)
            {
                while (cmplt(lt[num[i]],mid))
                    i++;
                while (cmplt(mid,lt[num[j]]))
                    j--;
                if (i<=j)
                {
                    swap(num[i],num[j]);
                    i++,j--;
                }
            }
            if (l<j)
                qs(l,j);
            if (i<r)
                qs(i,r);
        }
        void print()
        {
            for (int i=0;i<n;i++)
                printf("%s\n",lt[num[i]].name);
        }
        void main()
        {
            init();
            qs(0,n-1);
            print();
        }
    };
    
    int main()
    {
        dts::main();
    }
    
  • 1
    @ 2020-09-13 13:51:19

    //
    // main.cpp
    // 123
    //
    // Created by Siyuan on 2020/8/3.
    // Copyright © 2020 Siyuan. All rights reserved.
    //

    #include <iostream>
    #include <unordered_map>
    #include <string>
    #include <vector>
    #include <set>
    #include <algorithm>
    using namespace std;
    struct node{
    int id;
    string x;
    }arr[1001];
    bool cmp(node a, node b){
    return a.id < b.id;
    }
    int main(){
    int n;
    int i = 0;
    while(1){
    scanf("%d",&arr[i].id);
    i++;
    if(getchar() == '\n')
    break;
    }
    for(int j = 0; j < i; j++){
    cin >> arr[j].x;
    }
    sort(arr, arr+i, cmp);
    for(int j = 0; j < i; j++)
    cout << arr[j].x << endl;
    return 0;
    }

  • 1
    @ 2018-09-02 16:22:52

    这题也太水了吧
    python

    
    xuhao=list(map(int,input().split()))
    tezhengma=list(input().split())
    
    A=[]
    for i in range(len(xuhao)):
        A.append((xuhao[i],tezhengma[i]))
    
    A=sorted(A,key=lambda s:s[0])
    
    for i in range(len(A)):
        print(A[i][1])
    
  • 1
    @ 2018-07-25 11:12:39

    很简单的一道题,主要是在输入处理,因为N的数量未知,所以得使用判断回车符来判断N输入结束,因此有两种办法
    (1)使用gets函数只有遇到回车符结束的特点来获取字符串,然后再用sscanf来格式化输入,那么就需要有一个保存第一行的空间,粗略计算一下,1<=N<=1000,那么就需要1000(数量) × 3(假设每个的位数)+ 若干空格(本题没有提到两个整数之间是有一个空格,那有可能有很长的空格),因此所开的字符数组的数量无法确定,只能使用变长字符数组(C++ string),那么只能使用C++的输入了;

    (2)使用scanf("%c")来读入,判断回车符即结束,因为输入一定是整数,因此只要简单累乘计算一下即可;

    对比两种,最终选择第二种,理由有:

    (1)利用第二种不需要新增空间保存,节约了空间;

    (2)如果要使用第一种,那么得使用C++ string,这样得使用C++的cin,C++的输入速度感人,要是有刷POJ就知道一般输入量巨大的用cin会超时,所以建议不要用cin;或者开一个自认为不会越界的字符数组,但是这样有可能出现RE的不确定因素。

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    int idx[1002];
    char str[1002][257];
    
    int main()
    {
        //freopen("input.txt", "r", stdin);
        
        int i, j;
        char ch;
        int num = 0, n = 0;
        do
        {
            scanf("%c", &ch);
            if(ch != ' ' && ch != '\n')
            {
                num = num * 10 + (ch - '0');
            }
            else if(num != 0) // num != 0 为了防止中间有多个空格的情况 
            {
                idx[n++] = num;
                num = 0;
            }
        }while(ch != '\n');
        
    //  for(i = 0; i < n; ++i) printf("%d ", idx[i]);
    //  printf("\n");
    
        for(i = 0; i < n; ++i)
        {
            scanf("%s", str[idx[i]]);
        }
        
        for(i = 1; i <= n; ++i)
        {
            printf("%s\n", str[i]);
        }
        
        //fclose(stdin);
        
        return 0;
    }
    
  • 1
    @ 2017-09-09 20:39:30
    //再一次秀出了智商下限。。之前居然把读入写错了。。。。。。。lol
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    int p = 0;
    bool flag = false;
    struct letter {
        int number;
        string s;
    } l[1005];
    
    int read() {
        if (flag) return -1;
        int x = 0;
        char c = getchar();
        while (c >= '0' && c <= '9') { x *= 10; x += c - '0'; c = getchar(); }
        if (c == '\n') flag = true;
        return x;
    }
    
    bool cmp(letter l1, letter l2) {
        return l1.number < l2.number;
    }
    
    int main() {
        int tmp;
        tmp = read();
        while (tmp != -1) {
            l[++p].number = tmp;
            tmp = read();
        }
        for (int i = 1; i <= p; i++) {
            cin >> l[i].s;
        }
        sort(l + 1, l + 1 + p, cmp);
        for (int i = 1; i <= p; i++) {
            cout << l[i].s << endl;
        }
        return 0;
    }
    
  • 1
    @ 2017-08-03 14:08:01

    1.首先不知道N的大小,所以可以使用字符串流;2.因为要保证小数要按照原格式输出,所以不能用浮点数,可以使用字符串保存小数

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1000+50;
    struct mail
    {
        int num;
        char tag[260];
        bool operator < (mail x) {return this->num<x.num;}
    }m[maxn];
    
    int main()
    {
        //freopen("input.txt","r",stdin);
        string s;
        getline(cin,s);
        stringstream ss(s);
        int n=0,x;
        while(ss>>x)
            m[n++].num=x;
        for(int i=0;i<n;i++)
            scanf("%s",m[i].tag);
        sort(m,m+n);
        for(int i=0;i<n;i++)
            printf("%s\n",m[i].tag);
        return 0;
    }
    
    
  • 0
    @ 2019-02-18 13:07:16
    /**
    *直接获取两行输入的所有数值,然后进行拆分成字符串数组,在通过快速排序进行排序,打表就ok了。
    */
    import java.util.Scanner;
    
    public class Main {
    
        static String[] sum;
        static String[] sum1;
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNext()) {
                String n = in.nextLine();
                String n1 = in.nextLine();
                sum = n.split(" ");
                sum1 = n1.split(" ");
                f(0, sum.length - 1);
                for (int i = 0; i < sum.length; i++)
                    System.out.println(sum1[i]);
            }
            in.close();
        }
    
        public static void f(int left, int right) {
            int i, j, temp;
            String t;
            i = left;
            j = right;
            if (i > j)
                return;
            temp = Integer.valueOf(sum[left]);
            while (i != j) {
                while (Integer.valueOf(sum[j]) >= temp && i < j)
                    j--;
                while (Integer.valueOf(sum[i]) <= temp && i < j)
                    i++;
                if (i < j) {
                    t = sum[i];
                    sum[i] = sum[j];
                    sum[j] = t;
                    t = sum1[i];
                    sum1[i] = sum1[j];
                    sum1[j] = t;
                }
            }
            t = sum[left];
            sum[left] = sum[i];
            sum[i] = t;
            t = sum1[left];
            sum1[left] = sum1[i];
            sum1[i] = t;
            f(left, i-1);
            f(i+1, right);
        }
    
    }
    
    
  • 0
    @ 2018-07-13 11:28:00

    #include<iostream>
    #include<sstream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn = 1000 + 5;
    struct Letter
    {
    int x; string s;
    bool operator < (const Letter& rhs) const
    {
    return x < rhs.x;
    }
    }letter[maxn];
    int n = 1; string str;
    int main()
    {
    getline (cin, str);
    stringstream ss(str);
    while(ss >> letter[n++].x);
    n--;n--;
    for(int i = 1; i <= n; i++)
    cin >> letter[i].s;
    sort(letter + 1, letter + n + 1);
    for(int i = 1; i <= n; i++)
    cout << letter[i].s << endl;
    return 0;
    }

  • 0
    @ 2017-10-28 20:36:47

    排序 (✿◡‿◡)
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #include<map>
    #include<string>
    #include<ctime>
    #define ll long long
    #define DB double
    #define inf 987654321
    #define mod 1000007
    using namespace std;
    inline int read()
    {
    int x=0,w=1;char ch=0;
    while(!isdigit(ch)){if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*w;
    }
    int buf[50];
    void write(int x)
    {
    if(x<0) putchar('-'),x=-x;
    buf[0]=0;
    while(x) buf[++buf[0]]=x%10,x/=10;
    if(buf[0]==0) buf[0]=1,buf[1]=0;
    while(buf[0]) putchar('0'+buf[buf[0]--]);
    }
    int n;
    struct po{
    int id;
    string s;
    }a[1002];
    bool cmp(po x,po y)
    {
    return x.id<y.id;
    }
    int main()
    {
    n=read();
    a[1].id=n;
    for(int i=2;i<=n;i++)
    {
    int x;x=read();
    if(x>n) n=x;
    a[i].id=x;
    }
    for(int i=1;i<=n;i++)
    cin>>a[i].s;
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    cout<<a[i].s<<endl;
    return 0;
    }

  • 0
    @ 2017-10-15 11:01:43
    lstnum=list(map(int,input().split()))
    lstsn=input().split()
    dt=zip(lstnum,lstsn)#组成一个由元组构成的列表,序号第一个,序列号第二个
    
    lst=sorted(dt, key=lambda e:e[0])#按照第一个列表各元素第一项排序
    for i in lst:
        print(i[1])
        
    
  • 0
    @ 2017-10-07 19:24:10

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<stack>
    #define maxa 1000+10
    #define FOR(i,x,y) for(i=x;i<=y;++i)
    using namespace std;
    struct node
    {
    string s;
    int num;
    }e[maxa];
    bool comp(node p,node q)
    {
    return p.num<q.num;
    }
    int main()
    {
    char ch;
    int i =0;
    do{
    scanf("%d",&e[i++].num);
    ch = getchar();
    }while(ch!='\n');
    int j;
    for(j=0;j<i;++j)
    cin>>e[j].s;
    sort(e,e+i,comp);
    FOR(j,0,i-1)
    cout<<e[j].s<<endl;
    }

  • 0
    @ 2017-05-17 19:37:04
    #include<stdio.h>
    int main()
    {
        char a[1002][300];
        int b[1001];
        char c;
        int i=0, j, k;
        do {
            scanf("%d", &k);
            b[i++] = k;
        } while ((c = getchar()) != '\r');  //读取序号,这里竟然要读\r回车符,读\n的同学会不停RE... 
        for (j = 0; j < i; j++)             //读取特征码
            scanf("%s", a[b[j]]);              
        for (j = 1; j <= i; j++)
            printf("%s\n", a[j]);
        getchar();
        return 0;
    }
    
    • @ 2017-11-24 11:51:01

      现在又换成'\n'啦

  • 0
    @ 2017-05-13 14:28:11

    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String str = input.nextLine();
    String[] s1 = str.split(" ");
    str = input.nextLine();
    String[] s2 = str.split(" ");
    int n=s1.length;
    int a[] = new int[n];
    for(int i=0;i<n;i++)
    {
    a[i] = Integer.valueOf(s1[i]);
    }

    boolean[] flag = new boolean[n];
    for(int j=0;j<n;j++)
    {
    int min=10000000;
    int p=0;
    for(int i=0;i<n;i++)
    {
    if(!flag[i])
    {
    if(a[i]<min)
    {
    p=i;
    min=a[i];
    }
    }
    }
    flag[p]=true;
    System.out.println(s2[p]);
    }

    }
    }

  • 0
    @ 2017-03-13 14:57:27
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<sstream>
    #define inf 1e9
    #define ll long long
    #define For(i,j,k) for(int i=j;i<=k;i++)
    #define Dow(i,j,k) for(int i=k;i>=j;i--)
    using namespace std;
    string ts;
    struct node
    {
        string s;
        int num;
    }a[1001];
    int tot;
    inline bool cmp(node x,node y)
    {
        return x.num<y.num;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        getline(cin,ts);
        stringstream ss(ts);
        while(ss>>a[++tot].num);
        tot--;
        For(i,1,tot)    cin>>a[i].s;
        sort(a+1,a+tot+1,cmp);
        For(i,1,tot)    cout<<a[i].s<<endl;
    }
    

    算短了吧。。

  • 0
    @ 2016-11-29 02:13:00
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <climits>  
    #include <cmath>
    #include <algorithm>
    #include <functional>
    #include <iterator>
    #include <cstring>
    #include <set>
    #include <vector>
    #include <queue>
    #include <list>
    #include <cctype>
    #include <string>
    
    using namespace std;  
    
    
    
    int main()  
    {  
        string s1;                                 
        getline(cin,s1);
        s1=s1+' ';
    
    
        string s2;
        getline(cin,s2);
        s2=s2+' ';
    
    
        vector<int> a;
        int index=0;
        int num=0;
        while (index<=s1.size())
        {
           num=10*num+s1[index]-'0';
           if(s1[index+1]==' ')
           {
            a.push_back(num);
            index++;
            num=0;
           }    
           index++; 
        } 
    
       
    
    
        vector<string> b;
        int index1,index2;
        index1=index2=0;
        while (index2<=s2.size())
        {
           if(s2[index2]==' ')
           {
            b.push_back(s2.substr(index1,index2-index1));        
            index1=index2+1;        
           }    
           index2++;
        } 
    
    
        int tt=1;
        while (tt<=a.size())
        {
            for(int i=0;i!=a.size();i++)
            {
            if(a[i]==tt){cout<<b[i]<<endl;break;}   
            }
            tt++;
        } 
    
    
        return 0;  
    }   
    
    
  • 0
    @ 2016-10-16 16:14:52

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    struct num{
    int x;
    char y[280];
    }a[1050];
    int cmp(struct num a,struct num b)
    {
    return a.x<b.x;
    }
    int main(){
    char ch[4000];
    char str[256000];
    // freopen("lyy.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    gets(ch);
    gets(str);
    int p=0;int q=0;
    while(p<strlen(ch)){
    int num=0;
    while(ch[p]!=' '&&p<strlen(ch)){
    num=10*num+ch[p]-'0';
    p++;
    }
    p++;
    a[q].x =num;
    q++;

    }
    int m=0;int n=0;
    while(m<strlen(str)){
    char s[300]={0};
    int j=0;
    while(str[m]!=' '&&m<strlen(str)){
    s[j++]=str[m++];
    }
    m++;
    strcpy(a[n].y ,s);
    n++;
    }
    sort(a,a+n,cmp);
    for(int i=0;i<q;i++){
    cout<<a[i].y<<endl;
    }
    return 0;
    }

信息

ID
1389
难度
4
分类
模拟 点击显示
标签
(无)
递交数
2200
已通过
919
通过率
42%
被复制
10
上传者