PAT b1001-1005题解

来源:互联网 发布:debian安装软件命令 编辑:程序博客网 时间:2024/05/17 23:54

一、b1001

#include <cstdio>int main(){int n, step;scanf("%d", &n);while(n != 1){if(n % 2 == 0) n /= 2;else n = ( 3 * n + 1) / 2;step++;}printf("%d\n", step);return 0;}

二、b1002

#include <cstdio>#include <cstring>char a[105];char* change(int n){switch(n){case 0: return "ling"; break;case 1: return "yi"; break;case 2: return "er"; break;case 3: return "san"; break;case 4: return "si"; break;case 5: return "wu"; break;case 6: return "liu"; break;case 7: return "qi"; break;case 8: return "ba"; break;case 9: return "jiu"; break;}}int main(){scanf("%s", a);int len, sum = 0;len = strlen(a);for(int i = 0; i < len; i++){sum = sum + a[i] - '0';}int bai = 0, shi = 0, ge = 0;bai = sum / 100;shi = sum / 10 % 10;ge = sum % 10;if(bai != 0){printf("%s ", change(bai));printf("%s ", change(shi));printf("%s", change(ge));}else if(shi != 0){printf("%s ", change(shi));printf("%s", change(ge));}else{printf("%s", change(ge));}return 0;}

三、b1003

#include <cstdio>#include <cstring>int main(){int T;scanf("%d", &T);while(T--){char str[110];scanf("%s", str);int len = strlen(str);int num_p = 0, num_t = 0, other = 0;int loc_p = -1, loc_t = -1;for(int i = 0; i < len; i++){if(str[i] == 'P'){num_p++;loc_p = i;}else if(str[i] == 'T'){num_t++;loc_t = i;}else if(str[i] != 'A') other++;}if((num_p != 1) || (num_t != 1) || (other != 0) || (loc_t - loc_p <= 1)){printf("NO\n");continue;}int x = loc_p, y = loc_t - loc_p - 1, z = len - loc_t - 1;if(z - x * (y - 1) == x){printf("YES\n");}else{printf("NO\n");}}return 0;}

四、b1004

#include <cstdio>struct Student{char name[15];char id[15];int score;} temp, ans_max, ans_min;int main(){int n;scanf("%d", &n);    ans_max.score = -1;ans_min.score = 101;for(int i = 0; i < n; i++){scanf("%s%s%d", temp.name, temp.id, &temp.score);if(temp.score > ans_max.score){ans_max = temp;}if(temp.score < ans_min.score){ans_min = temp;}}printf("%s %s\n", ans_max.name, ans_max.id);printf("%s %s", ans_min.name, ans_min.id);return 0;}


五、b1005

#include <cstdio>#include <algorithm>using namespace std;bool cmp(int a, int b){return a > b;}bool hashTable[10000] = {false};int a[110];int main(){int n, m;scanf("%d", &n);for(int i = 0; i < n; i++){scanf("%d", &a[i]);m = a[i];while(m != 1){if(m % 2 == 1){m = (3 * m + 1) / 2;}else {m = m / 2;}hashTable[m] = true;}}int count = 0;for(int i = 0; i < n; i++){if(hashTable[a[i]] == false){count++;}}sort(a, a + n, cmp);for(int i = 0; i < n; i++){if(hashTable[a[i]] == false){printf("%d", a[i]);count--;if(count > 0) printf(" ");}}return 0;} 



原创粉丝点击