生活随笔
收集整理的這篇文章主要介紹了
sort函数的应用习题(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 6084問題【★】
- 按1的個數排序 【★】
- 01串排序【★★】
- 病人排隊【★★】
- 生日排序 【★★】
- 組隊 【★】
- 嚴格排名 【★】
6084問題【★】
https://nanti.jisuanke.com/t/T1470
-
題目
任意給出一個四位數,把它重新組成一個四位的最大數和一個最小數,算出兩者間的差。
-
例如:37213721 這個數,可以重組成:73217321 和 12371237,差值為 7321-12377321?1237。
-
輸入格式
一個四位數。
-
輸出格式
題目中所說的差值。
輸出時每行末尾的多余空格,不影響答案正確性
樣例輸入
3721
樣例輸出
6084
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std
;
char a
[5];
bool cmp(char a
,char b
)
{return a
>b
;
}
int str_int(char a
[])
{return (a
[0]-48)*1000+(a
[1]-48)*100+(a
[2]-48)*10+a
[3]-48;
}
int main(void)
{while( scanf("%s",a
) != EOF ){sort(a
,a
+4);int min
=str_int(a
);sort(a
,a
+4,cmp
);int max
=str_int(a
);printf("%d\n",max
-min
);}
}
按1的個數排序 【★】
https://nanti.jisuanke.com/t/T1480
-
題目
有一些 01 字串,將其按 1 的個數的多少的順序進行輸出。如果 1 的數量相等,則按照出現的先后順序排序。
-
輸入格式
輸入數據有若干行組成。第一行是一個數 n(1≤n≤100),代表串的個數。然后 n 行每一行是一個 0101 串,每個字符串長度不超過 200 。
-
輸出格式
重新排列 01 串的順序。使得串按題目描述的方式排序。
輸出時每行末尾的多余空格,不影響答案正確性
樣例輸入
6
10011111
00001101
1010101
1
0
1100
樣例輸出
0
1
1100
00001101
1010101
10011111
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std
;
struct student
{char id
[205];int number
;int n
;
}stu
[105];
bool cmp(student a
,student b
)
{if(a
.number
==b
.number
)return a
.n
<b
.n
;return a
.number
<b
.number
;
}
int main(void)
{int n
;while( scanf("%d",&n
) != EOF ){for(int i
=0;i
<n
;i
++){scanf("%s",stu
[i
].id
);stu
[i
].n
=i
;for(int j
=0;j
<strlen(stu
[i
].id
);j
++){if(stu
[i
].id
[j
]=='1')stu
[i
].number
++;}}sort(stu
,stu
+n
,cmp
);for(int i
=0;i
<n
;i
++){printf("%s\n",stu
[i
].id
);}}return 0;
}
01串排序【★★】
https://nanti.jisuanke.com/t/T1458
-
題目
將 01 串首先按長度排序,長度相同時,按 1的個數多少進行排序,1 的個數相同時再按 ASCII 碼值排序(字典序)。
-
輸入格式
第一行輸入一個整數 n (1≤n≤100),表示字符串的個數。
輸入數據中含有一些 01 串,01 串的長度不大于 256 個字符。
-
輸出格式
重新排列 01 串的順序,使得串按基本描述的方式排序,然后依次輸出。
輸出時每行末尾的多余空格,不影響答案正確性
樣例輸入
6
10011111
00001101
1010101
1
0
1100
樣例輸出
0
1
1100
1010101
00001101
10011111
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std
;
struct student
{char id
[205];int number
;int n
;
}stu
[105];
bool cmp(student a
,student b
)
{if(a
.n
==b
.n
){if(a
.number
==b
.number
)return strcmp(a
.id
,b
.id
)<0;return a
.number
<b
.number
;} return a
.n
<b
.n
;
}
int main(void)
{int n
;while( scanf("%d",&n
) != EOF ){for(int i
=0;i
<n
;i
++){scanf("%s",stu
[i
].id
);stu
[i
].n
=strlen(stu
[i
].id
);for(int j
=0;j
<strlen(stu
[i
].id
);j
++){if(stu
[i
].id
[j
]=='1')stu
[i
].number
++;}}sort(stu
,stu
+n
,cmp
);for(int i
=0;i
<n
;i
++){printf("%s\n",stu
[i
].id
);}}return 0;
}
病人排隊【★★】
https://nanti.jisuanke.com/t/T1155
- 題目
病人登記看病,編寫一個程序,將登記的病人按照以下原則排出看病的先后順序:
老年人(年齡 ≥60 歲)比非老年人優先看病。
老年人按年齡從大到小的順序看病,年齡相同的按登記的先后順序排序。
非老年人按登記的先后順序看病。 - 輸入格式
第 1 行,輸入一個小于 100 的正整數,表示病人的個數;
后面按照病人登記的先后順序,每行輸入一個病人的信息,包括:一個長度小于 10 的字符串表示病人的 ID(每個病人的 ID 各不相同且只含數字和字母),一個整數表示病人的年齡(不超過 100歲),中間用單個空格隔開。 - 輸出格式
按排好的看病順序輸出病人的 ID,每行一個。
輸出時每行末尾的多余空格,不影響答案正確性
樣例輸入
5
021075 40
004003 15
010158 67
021033 75
102012 30
樣例輸出
021033
010158
021075
004003
102012
思路: 先按照老年人的規則排序。這時候的排序已經是按照年齡從大到小排的。
這時候找到小于60的人,在按照年輕人的排序規則排序。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std
;
struct student
{char id
[15];int age
;int n
;
}stu
[105];
bool cmp(student a
,student b
)
{if(a
.age
==b
.age
)return a
.n
<b
.n
;return a
.age
>b
.age
;
}
bool cmp1(student a
,student b
)
{return a
.n
<b
.n
;
}
int main(void)
{int n
;int i
;while( scanf("%d",&n
) != EOF ){for(i
=0;i
<n
;i
++){scanf("%s %d",stu
[i
].id
,&stu
[i
].age
);stu
[i
].n
=i
;}sort(stu
,stu
+n
,cmp
);for( i
=0;i
<n
;i
++){if(stu
[i
].age
<60)break;}sort(stu
+i
,stu
+n
,cmp1
);for( i
=0;i
<n
;i
++){printf("%s\n",stu
[i
].id
);}} return 0;
}
生日排序 【★★】
https://nanti.jisuanke.com/t/T1715
-
題目:
蒜頭學院開學了,老師要統計班里每個人的生日,并按照出生日期從早到晚排序。
-
輸入格式
第一行一個整數 n (1≤n≤100),班級班級的人數。
接下來 n 行,每行包含一個字符串 s 和三個整數 y,m,d,表示姓名為 s的同學出生日期是 y 年 m 月 d日。
保證所有日期合法,姓名由小寫字母構成,不超過 20個字符。
-
輸出格式
輸出 n行,每行一個字符串表示姓名。如果有兩個同學出生日期相同,輸入靠后的同學先輸出。
輸出時每行末尾的多余空格,不影響答案正確性
樣例輸入
3
qwb 1996 6 30
gyt 1995 7 28
wc 1996 6 30
樣例輸出
gyt
wc
qwb
#include<iostream>
#include<string>
#include<algorithm>
using namespace std
;
struct birthday
{string name
;int year
;int month
;int day
;int id
;
}stu
[105];
bool cmp(birthday a
,birthday b
)
{if(a
.year
!=b
.year
){return a
.year
<b
.year
;}else{if(a
.month
!=b
.month
){return a
.month
<b
.month
;}else{if(a
.day
!=b
.day
){return a
.day
<b
.day
;}else{return a
.id
>b
.id
;}}}
}
int main(void)
{int n
;cin
>>n
;for(int i
=0;i
<n
;i
++){cin
>>stu
[i
].name
>>stu
[i
].year
>>stu
[i
].month
>>stu
[i
].day
;stu
[i
].id
=i
;}sort(stu
,stu
+n
,cmp
);for(int i
=0;i
<n
;i
++){cout
<<stu
[i
].name
<<endl
;}return 0;
}
我其實最開始的思路是都是字符串。將年月日字符串比較。
但是不知為啥只能同過3組
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std
;
struct student
{char name
[25];char year
[5];char yue
[3];char ri
[3];char birthday
[20];int id
;
}stu
[1005];
bool cmp(student a
,student b
)
{if(strcmp(a
.birthday
,b
.birthday
)==0)return a
.id
>b
.id
;return strcmp(a
.birthday
,b
.birthday
)<0;
}
int main(void)
{int n
;int i
=0;while( scanf("%d",&n
) != EOF ){for(i
=0;i
<n
;i
++){scanf("%s %s %s %s",stu
[i
].name
,stu
[i
].year
,stu
[i
].yue
,stu
[i
].ri
);strcpy(stu
[i
].birthday
,stu
[i
].year
);strcat(stu
[i
].birthday
,stu
[i
].yue
);strcat(stu
[i
].birthday
,stu
[i
].ri
);stu
[i
].id
=i
;}sort(stu
,stu
+n
,cmp
);for(i
=0;i
<n
;i
++){printf("%s %s\n",stu
[i
].name
,stu
[i
].birthday
);}}return 0;
}
組隊 【★】
https://nanti.jisuanke.com/t/T1445
-
題目
花椰妹當上了某學校程序設計競賽隊的教練。現在她要將集訓隊內的 n 名學生兩兩組隊。每位學生有一個能力值,只有能力值相同的兩人才能組隊。
當然這些學生也可以通過做題來提升自己的能力值。每位學生每做一道題提升一點能力值。
花椰妹想知道,這些學生最少還要做幾道題才能都組上隊。
-
輸入格式
輸入的第一行包含一個整數 n(2≤n≤100),并且保證是偶數。
輸入的第二行包括 n 個整數,為每個學生的能力值 輸入的第二行包括 n 個整數,為每個學生的能力值 Aiii(2<= Aiii <=100)
-
輸出格式
輸出只有一個整數——這些學生至少還要做多少道題。
輸出時每行末尾的多余空格,不影響答案正確性
樣例輸入1
6
5 10 2 3 14 5
樣例輸出1
5
樣例輸入2
2
1 100
樣例輸出2
99
#include<cstdio>
#include<algorithm>
using namespace std
;
int a
[105];
int main(void)
{int n
;int i
;int number
=0;while( scanf("%d",&n
) != EOF ){number
=0;for(i
=0;i
<n
;i
++)scanf("%d",&a
[i
]);sort(a
,a
+n
);for(i
=0;i
<n
;i
++){if(a
[i
]==a
[i
+1]){i
++;}else{number
+=a
[i
+1]-a
[i
];i
++;}}printf("%d\n",number
);}return 0;
}
嚴格排名 【★】
https://nanti.jisuanke.com/t/T3093
樣例輸入
5 3
1 2 3 2 4
樣例輸出
3
#include<cstdio>
#include<algorithm>
using namespace std
;
long int a
[100025];
int main(void)
{int n
,k
,number
;scanf("%d %d",&n
,&k
);int i
=0;for(i
=0;i
<n
;i
++){scanf("%ld",&a
[i
]);}sort(a
,a
+n
);number
=1;for(i
=1;i
<n
;i
++){if(a
[i
]==a
[i
-1]){continue;}number
++;if(number
==k
){printf("%ld\n",a
[i
]);return 0;}}printf("%d",a
[0]);return 0;
}
總結
以上是生活随笔為你收集整理的sort函数的应用习题(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。