zoukankan      html  css  js  c++  java
  • C++结构体的应用_YCOJ

    结构体是一种自定义的东西,用struct来定义。在他里面,
    可以装许多东西,比如int,string,char,bool等等等等。
    如:

        struct a{
        string name;
        int a;
        int b;
        };
        ……
        a s;
        cin >> s.name;
    
    
    	代码示范:	
        例题:
        身高排序
    

    Description

    有 n 个同学排成一排,从左到右依次编号为 1~n。

    现在给出了这个 n 位同学的身高,但是并不是按照高矮排序的。现在这些同学需要按照身高从矮到高的顺序重新排列,矮的同学在左边,高的同学在右边。如果两个同学身高相同,那么这两个同学的相对顺序不应该发生变化。

    请你输出排序以后从左到右同学的编号。

    Input

    输入一个行一个整数 n(1≤n≤1000)。

    接下来一行输入 n 个整数,依次表示从左到右的同学的身高,每个同学身高小于等于 300。

    Output

    一行输出 n 个整数,两个数中间用空格隔开,行末不要有多余空格。

    表示重新排列以后从左到右的同学的编号。

    Sample Input 1 
    
    5
    156 178 145 190 156
    Sample Output 1
    
    3 1 5 2 4
    解题代码:
    cpp
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct student{
    int h;
    int name;//name=编号 
    };//定义结构体 
    bool cmp(student x,student y){
    	return x.h<y.h;
    }//排序函数 
    int main(){
    int n,b=0;
    cin >>n;//人数 
    student a[n];//定义student变量a 
    for (int i=0;i<n;i++){
    	b++;
    	a[i].name=b;
    }
    for (int i=0;i<n;i++){
    	cin >>a[i].h;
    }
    stable_sort(a,a+n,cmp);//更稳定的sort排序 
    for(int i=0;i<=n-2;i++){
    	cout << a[i].name<<" ";//为了去末尾空格,最后一个单独输出 
    }
    cout << a[n-1].name;//输出最后一个 
    return 0;
    }
    注:结构体排序需要写一个排序函数。
    这就是结构体。
    

    标准结尾:
    在这里插入图片描述

                                                 END
  • 相关阅读:
    利用performSelectorInBackground和performSelectorOnMainThread实现多线程刷新UI
    iOS之NSCocoaErrorDomain Code=3840
    iOS之原生地图与高德地图
    iOS之Storyboard References
    iOS之内购
    iOS之上线被拒
    iOS之可拖拽重排的CollectionView
    iOS之内购
    ios专题
    ios专题
  • 原文地址:https://www.cnblogs.com/herobrine-life/p/10959465.html
Copyright © 2011-2022 走看看