PAT b1036-b1040题解

来源:互联网 发布:php jsonp callback 编辑:程序博客网 时间:2024/06/06 20:24

1、b1036

#include <cstdio>int main(){int col, row;char symbol;scanf("%d %c", &col, &symbol);if(col % 2 == 0){row = col / 2;}if(col % 2 == 1){row = col / 2 + 1;}for(int i = 0; i < col; i++){printf("%c", symbol);}printf("\n");for(int i = 0; i < row - 2; i++){printf("%c", symbol);for(int i = 0; i < col -2; i++){printf(" ");}printf("%c", symbol);printf("\n");} for(int i = 0; i < col; i++){printf("%c", symbol);}return 0;}

2、b1037

#include <cstdio>const int Gallon = 17 * 29;const int Sickle = 29;int main(){int a1, b1, c1;int a2, b2, c2;scanf("%d.%d.%d %d.%d.%d", &a1, &b1, &c1, &a2, &b2, &c2);int price = a1 * Gallon + b1 * Sickle + c1;int money = a2 * Gallon + b2 * Sickle + c2;int change = money - price;if(change < 0){printf("-");change = -change; }printf("%d.%d.%d\n", change / Gallon, change % Gallon / Sickle, change % Sickle);return 0;}

3、b1038

#include <cstdio>int grade[101] = {0};int main(){int n;scanf("%d", &n);for(int i = 0; i < n; i++){int score;scanf("%d", &score);grade[score]++;}int m;scanf("%d", &m);for(int i = 0; i < m; i++){int score;scanf("%d", &score);if(i == m -1){printf("%d", grade[score]);}else{printf("%d ", grade[score]);}}return 0;}

4、b1039

#include <cstdio>#include <cstring>int hashTable[128] = {0};char str1[1010];char str2[1010];int count = 0;int main(){gets(str1);int len1 = strlen(str1);for(int i = 0; i < len1; i++){hashTable[str1[i]]++;}gets(str2);int len2 = strlen(str2);for(int i = 0; i < len2; i++){hashTable[str2[i]]--;if(hashTable[str2[i]] < 0){count++;}}if(!count){printf("Yes %d", len1 -len2);}else{printf("No %d", count);}return 0;}

5、b1040

#include <cstdio>#include <cstring>const int MAXN = 100010;const int MOD = 1000000007;char str[MAXN];int leftNumP[MAXN] = {0};int main(){gets(str);int len = strlen(str);for(int i = 0; i < len; i++){if(i > 0){leftNumP[i] = leftNumP[i - 1];}if(str[i] == 'P'){leftNumP[i]++;}}int ans = 0, rightNumT = 0;for(int i = len - 1; i >= 0; i--){if(str[i] == 'T'){rightNumT++;}else if(str[i] == 'A'){ans = (ans + leftNumP[i] * rightNumT) % MOD;}}printf("%d", ans);return 0;} 


原创粉丝点击