noip第19課作業


1. 誰考了第K名

在一次考試中,每個學生的成績都不相同,現知道了每個學生的學號和成績,求考第k名學生的學號和成績。

【輸入格式】

第一行有兩個整數,分別是學生的人數n(1<=n<=100),和求第k名學生的k(1<=k<=n)。其后有n行數據,每行包括一個學號(整數)和一個成績(浮點數),中間用一個空格分隔。

【輸出格式】

輸出第k名學生的學號和成績,中間用空格分隔。

【樣例輸入】

5 3

90788001 67.8

90788002 90.3

90788003 61

90788004 68.4

90788005 73.9

【樣例輸出】

90788004 68.4

#include <iostream>
#include <algorithm>
using namespace std;
struct student{
    int num;
    double score;    
}; 
bool cmp(student x,student y){
    return x.score > y.score;
}
int main(){
    int n,k;
    cin >> n>>k;
    student stu[101];
    for(int i = 1;i <= n;i++){
        cin >> stu[i].num>>stu[i].score;
    }
    sort(stu+1,stu+n+1,cmp);
    cout << stu[k].num << " " << stu[k].score <<endl;
    return 0;
}

2. 眾數

由文件給出N個1到30000間無序數正整數,其中1≤N≤10000,同一個正整數可能會出現多次,出現次數最多的整數稱為眾數。求出它的眾數及它出現的次數。

【輸入格式】

輸入文件第一行是正整數的個數N,第二行開始為N個正整數。

【輸出格式】

輸出文件有若干行,每行兩個數,第1個是眾數,第2個是眾數出現的次數。

【樣例輸入】

12

2  4  2  3  2  5  3  7  2  3  4  3

【樣例輸出】

2   4

3   4

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
long long a[40000];
int main () {
    long long n,i,j,m,s,t,max;
    cin>>n;
    max=0;
    for (i=1; i<=30000; i++)
        a[i]=0;
    for (i=1; i<=n; i++) {
        cin>>t;
        a[t]=a[t]+1;
    }
    for (i=1; i<=30000; i++)
        if (a[i]>max)
            max=a[i];
    for (i=1; i<=30000; i++)
        if (a[i]==max) cout<<i<<" "<<max<<endl;
}

1、單詞排序

輸入一行單詞序列,相鄰單詞之間由1個或多個空格間隔,請按照字典序輸出這些單詞,要求重復的單詞只輸出一次。(區分大小寫)

【輸入格式】

    一行單詞序列,最少1個單詞,最多100個單詞,每個單詞的長度不超過50,單詞之間至少用一個空格間隔。數據不含除字母、空格外的其他字符。

【輸出格式】

按字典序輸出這些單詞,重復的單詞只輸出一次。

【樣例輸入】

She wants to go to Peking University to study Chinese

【樣例輸出】

Chinese

Peking

She

University

go

study

to

wants

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
string s[100001];
int main(){
    int i,n = 0,t,j,k;
    string ss;
    while(cin >> ss){
        n++;
        s[n] = ss;
    }
    for(i = 1;i < n;++i){
        for(j = i+1;j <= n;++j){
            if(s[i] > s[j])
                swap(s[i],s[j]);
        }
    }
    for(i = 1;i <= n;++i){
        if(s[i-1] != s[i])
            cout << s[i] << endl;
    }
    return 0;
} 

2、整數奇偶排序

給定10個整數的序列,要求對其重新排序。排序要求:

1.奇數在前,偶數在后;2.奇數按從大到小排序;3.偶數按從小到大排序。

【輸入格式】

輸入一行,包含10個整數,彼此以一個空格分開,每個整數的范圍是大於等於0,小於等於100。

【輸出樣例】

按照要求排序后輸出一行,包含排序后的10個整數,數與數之間以一個空格分開。

【樣例輸入】

4 7 3 13 11 12 0 47 34 98

 

【樣例輸出】

47 13 11 7 3 0 4 12 34 98

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
long n,i,j,s,a[30000],b[30000];
long se(long l,long r) {
    long v,t,i,j;
    i=l;
    j=r;
    v=a[(l+r)/2];
    do {
        while (a[i]<v) i++;
        while (a[j]>v) j--;
        if (i<=j) {
            t=a[i];
            a[i]=a[j];
            a[j]=t;
            i++;
            j--;
        }
    } while(i<=j);
    if (l<j) se(l,j);
    if (i<r) se(i,r);
}
int main() {
    cin>>n;
    for (i=1; i<=n; i++) cin>>a[i];
    se(1,n);
    cin>>n;
    for (i=1; i<=n; i++) {
        //cin>>s;
        //cout<<a[s]<<endl;
        cin >> b[i];
    }
    for(i = 1;i <= n;i++){
        if(a[b[i]] != 0){
            cout << a[b[i]] << endl;
        }else{
            continue;
        }
    }
}

3、合影效果

小雲和朋友們去爬香山,為美麗的景色所陶醉,想合影留念。如果他們站成一排,男生全部在左(從拍照者的角度),並按照從矮到高的順序從左到右排,女生全部在右,並按照從高到矮的順序從左到右排,請問他們合影的效果是什么樣的(所有人的身高都不同)?

【輸入格式】

第一行是人數n(2<=n<=40,且至少有1個男生和1個女生)。

后面緊跟n行,每行輸入一個人的性別(男 male 或 女 female)和身高(浮點數,單位:米),兩個數據之間以空格分割。

【輸出格式】

n個浮點數,模擬站好隊后,拍照者眼中從左到右每個人的身高。每個浮點數需保留到小數點后2位,相鄰兩個數之間用單個空格隔開。

【樣例輸入】

6

male 1.72

male 1.78

female 1.61

male 1.65

female 1.70

female 1.56

【樣例輸出】

1.65 1.72 1.78 1.70 1.61 1.56

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;
float a[32768],b[32768];
int main(){
    int n,i,j,k = 0,f = 0;
    string s;
    float t,m;
    cin >> n;
    for(i = 1;i <= n;i++){
        cin >> s >> m;
        if(s == "male"){
            k++;
            b[k] = m;
        }else{
            f++;
            a[f] = m;
        }
    }
    for(i = 1;i <= k;i++){
        for(j = i+1;j <= k;j++){
            if(b[i] > b[j]){
                t = b[i];
                b[i] = b[j];
                b[j] = t;
            }
        }
    } 
    for(i = 1;i <= f;i++){
        for(j = i+1;j <= f;j++){
            if(a[i] < a[j]){
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
    }
    for(i = 1;i <= k;i++){
        printf("%0.2f ",b[i]);
    }
    for(i = 1;i <= f;i++){
        printf("%0.2f  ,",a[i]);
    }
    cout << endl;
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM