算法笔记实战 模拟
文章目錄
- A + B 和 C
- 部分 A+B
- B1026 程序運行時間
- B1046 劃拳
- B1008 數組元素循環右移
- 數字圖像分類
- B1018 錘頭剪刀布
- shuffling Machine
- Shortest Distance
- B1010 一元多項式求導
- A1009 多項式乘法
- B1041 考試座位號
- B1004成績排名
- B1028 人口普查
- World Cup Betting
- A1006 sign in and sign out
- A1036 boys vs girls
- B1027 打印沙漏
- A1031 Hello world for U
- B1002 寫出這個數
- B1014福爾摩斯的約會
- B1024科學計數法
- B1048數字加密
A + B 和 C
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int N = 80; int t, a, b, c;int main() {cin >> t;for (int i = 1; i <= t; i ++ ) {scanf("%d%d%d", &a, &b, &c);if (a + b > c) printf("Case #%d: true\n", i);else printf("Case #%d: false\n", i);}return 0; }錯誤之處在于數據范圍爆炸了int,要用long long
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int N = 80; long long t, a, b, c;int main() {cin >> t;for (int i = 1; i <= t; i ++ ) {scanf("%lld%lld%lld", &a, &b, &c);if (a + b > c) printf("Case #%d: true\n", i);else printf("Case #%d: false\n", i);}return 0; }int 范圍是 -263到263-1, 爆了 int 用 long long
部分 A+B
#include <iostream> #include <cstdio> #include <cstring>using namespace std;typedef long long LL;const int N = 80;LL a, b, pa, pb; int da, db;int main() {cin >> a >> da >> b >> db;while (a) {int t = a % 10;if (t == da) {pa = 10 * pa + da;}a /= 10;}while (b) {int t = b % 10;if (t == db) {pb = 10 * pb + db;}b /= 10;}LL res = pa + pb;cout << res << endl;return 0; }B1026 程序運行時間
近似函數round在cmath中
問題在于輸出怎么填充0 %02d 整數,占兩位,不足填前導0
B1046 劃拳
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 110;int n; int aS, aD, bS, bD;int main() {cin >> n;int resA = 0, resB = 0;while (n -- ) {scanf("%d %d %d %d", &aS, &aD, &bS, &bD);if ((aD == aS + bS) && (bD != aD)) {resB ++ ;} else if ((bD == aS + bS) && (bD != aD)) {resA ++ ;}}cout << resA << ' ' << resB << endl;return 0; }B1008 數組元素循環右移
錯誤代碼
錯誤原因 M 是 可能大于 N 的
因此 m = n % m;
數字圖像分類
通過部分測評
其實可以同時開一個計數數組和一個累加數值數組
B1018 錘頭剪刀布
簡單做法,沒有考慮字典序輸出
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1e5 + 10;int n; int a[3], b[3]; int aCnt[3], bCnt[3];int main() {cin >> n;while (n -- ) {char l, r;cin >> l >> r;if ((l == 'J') && (r == 'B')) a[0] ++, b[2] ++ , aCnt[0] ++ ;if ((l == 'C') && (r == 'J')) a[0] ++, b[2] ++ , aCnt[1] ++ ;if ((l == 'B') && (r == 'C')) a[0] ++, b[2] ++ , aCnt[2] ++ ;if ((r == 'J') && (l == 'B')) b[0] ++, a[2] ++ , bCnt[0] ++ ;if ((r == 'C') && (l == 'J')) b[0] ++, a[2] ++ , bCnt[1] ++ ;if ((r == 'B') && (l == 'C')) b[0] ++, a[2] ++ , bCnt[2] ++ ;if ((r == 'J') && (r == l)) a[1] ++, b[1] ++;if ((r == 'C') && (r == l)) a[1] ++, b[1] ++;if ((r == 'B') && (r == l)) a[1] ++, b[1] ++;}printf("%d %d %d\n", a[0], a[1], a[2]);printf("%d %d %d\n", b[0], b[1], b[2]);if (aCnt[0] > aCnt[1] && aCnt[0] > aCnt[2]) printf("J ");else if (aCnt[1] > aCnt[0] && aCnt[1] > aCnt[2]) printf("C ");else printf("B ");if (bCnt[0] > bCnt[1] && bCnt[0] > bCnt[2]) printf("J\n");else if (bCnt[1] > bCnt[0] && bCnt[1] > bCnt[2]) printf("C\n");else printf("B\n");return 0; }使用scanf()讀入c的時候有\n的問題,要使用 getchar 吸收掉換行符,同時可以做字母和數字之間的映射,減少復雜度。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1e5 + 10;int change(char c) {if (c == 'B') return 0;if (c == 'C') return 1;if (c == 'J') return 2; }int main() {char mp[3] = {'B', 'C', 'J'}; // mp[0] : 'B', mp[1] : 'C', mp[2] : 'J'int n;scanf("%d", &n);int time_A[3] = {0}, time_B[3] = {0}; // 記錄 A B 三種局勢的次數int hand_A[3] = {0}, hand_B[3] = {0}; // 記錄三種手勢獲勝的次數char c1, c2;int k1, k2;for (int i = 0; i < n; i ++ ) {getchar();scanf("%c %c", &c1, &c2);k1 = change(c1);k2 = change(c2);if ((k1 + 1) % 3 == k2) { // 甲獲勝time_A[0] ++ ;time_B[2] ++ ;hand_A[k1] ++ ;} else if (k1 == k2) { // 平局time_A[1] ++ ;time_B[1] ++ ;} else { // 乙獲勝time_A[2] ++ ;time_B[0] ++ ;hand_B[k2] ++ ;}}printf("%d %d %d\n", time_A[0], time_A[1], time_A[2]);printf("%d %d %d\n", time_B[0], time_B[1], time_B[2]);int id1 = 0, id2 = 0;for (int i = 0; i < 3; i ++ ) {if (hand_A[i] > hand_A[id1]) id1 = i;if (hand_B[i] > hand_B[id2]) id2 = i;}printf("%c %c\n", mp[id1], mp[id2]);return 0; }標準答案版
shuffling Machine
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 60;int n; int go[N], back[N], form[N];int main() {cin >> n;char* mp[] = {"a", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "S12", "S13","H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "H11", "H12", "H13", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13","D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "J1", "J2"};for (int i = 1; i <= 54; i ++ ) scanf("%d", &form[i]), go[i] = i, back[i] = i;while (n -- ) {for (int i = 1; i <= 54; i ++ ) {back[form[i]] = go[i];}memcpy(go, back, sizeof(go));}for (int i = 1; i <= 54; i ++ ) {printf("%s", mp[go[i]]);if (i != 54) {printf(" ");}}printf("\n");return 0; }是memcpy 而不是 memcopy 少一個字母 o
存放字符串用指針形變量 申明方式是char * name[] =
Shortest Distance
我寫的代碼的問題,這份代碼超時
修改后
B1010 一元多項式求導
A1009 多項式乘法
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 2000 + 10;struct pointMent {double res; // 答案double cof; // 系數 }p[N];int main() { int n, k;double x;cin >> n;for (int i = 0; i < n; i ++ ) {scanf("%d %lf", &k, &x);p[k].cof = x;}cin >> n;for (int i = 0; i < n; i ++ ) {scanf("%d %lf", &k, &x);for (int j = 0; j <= 1000; j ++ ) {p[j + k].res += p[j].cof * x; }}int cnt = 0;for (int i = 0; i < N; i ++ ) {if (p[i].res != 0.0) cnt ++ ;}printf("%d", cnt);for (int i = N; i >= 0; i -- ) {if (p[i].res != 0.0) {printf(" %d %.1f", i, p[i].res);}}printf("\n");return 0; }double類型和0比較要用0.0比較,同一個位置要存儲多個信息的時候可以使用結構體
B1041 考試座位號
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1000 + 10;struct student {long long id; // 準考證考int test; // 試機號int exam; // 考試號 }stu[N];int n, m;int main() { cin >> n;for (int i = 0; i < n; i ++ ) {scanf("%lld %d %d", &stu[i].id, &stu[i].test, &stu[i].exam);}cin >> m;for (int i = 0; i < m; i ++ ) {int test;scanf("%d", &test);for (int j = 0; i < N; j ++ ) {if (stu[j].test == test) {printf("%lld %d", stu[j].id, stu[j].exam);if (i != m - 1) {printf("\n");}break;}}}printf("\n");return 0; }使用結構體是關鍵
B1004成績排名
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1000 + 10;struct student {char name[20]; // 姓名char id[20]; // 學號int score; // 考試成績 }stu[N];int n;int main() { cin >> n;for (int i = 0; i < n; i ++ ) {scanf("%s %s %d", stu[i].name, stu[i].id, &stu[i].score);}int minId = 0;int maxId = 0;for (int i = 0; i < n; i ++ ) {if (stu[i].score > stu[maxId].score) maxId = i;if (stu[i].score < stu[minId].score) minId = i;}printf("%s %s\n", stu[maxId].name, stu[maxId].id);printf("%s %s\n", stu[minId].name, stu[minId].id);return 0; }B1028 人口普查
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1e5 + 10;struct student {char name[20]; // 姓名LL birthday; // 生日 }stu[N];int n;int main() { cin >> n;int cnt = 0;int minId = -1;int maxId = -1;for (int i = 0; i < n; i ++ ) {int yy, mm, dd;scanf("%s %d/%d/%d", stu[i].name, &yy, &mm, &dd);stu[i].birthday = yy * 10000 + mm * 100 + dd;if (stu[i].birthday >= 18140906 && stu[i].birthday <= 20140906) {cnt ++ ;if (stu[i].birthday > stu[minId].birthday || minId == -1) {minId = i;}if (stu[i].birthday < stu[maxId].birthday || maxId == -1) {maxId = i;}}}if (cnt) {printf("%d %s %s\n", cnt, stu[maxId].name, stu[minId].name);} else {printf("0\n"); }return 0; }加上對所有都不合法的特判
World Cup Betting
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1e5 + 10;struct student {char name[20]; // 姓名LL birthday; // 生日 }stu[N];int n;int main() { char mp[4] = {'W', 'T', 'L'};double p[4][4] = {};for (int i = 0; i < 3; i ++ ) {scanf("%lf %lf %lf", &p[i][0], &p[i][1], &p[i][2]);}double res = 1;for (int i = 0; i < 3; i ++ ) {int I = 0;double val = p[i][I];for (int j = 0; j < 3; j ++ ) {if (p[i][j] > val) {val = p[i][j];I = j;}}printf("%c ", mp[I]);res *= val;}res = (res * 0.65 - 1) * 2;printf("%.2lf\n", res);return 0; }接收double數據類型一定要用%lf
A1006 sign in and sign out
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1e5 + 10;struct person {char name[20]; // 姓名int start; // 進入int end; // 離開 }p[N];int n;int main() { cin >> n;int minId = 0;int maxId = 0;for (int i = 0; i < n; i ++ ) {int h1, m1, s1, h2, m2, s2;scanf("%s %d:%d:%d %d:%d:%d", p[i].name, &h1, &m1, &s1, &h2, &m2, &s2);p[i].start = h1 * 3600 + m1 * 60 + s1;p[i].end = h2 *3600 + m2 * 60 + s2;if (p[minId].start > p[i].start) {minId = i;}if (p[maxId].end < p[i].end) {maxId = i;}}printf("%s %s\n", p[minId].name, p[maxId].name);return 0; }A1036 boys vs girls
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1e5 + 10;struct person {char name[20]; // 姓名char sexy; // 性別char lesson[20]; // 課程int score; // 得分 }p[N];int n;int main() { cin >> n;int minId = -1;int maxId = -1;for (int i = 0; i < n; i ++ ) {scanf("%s %c %s %d", &p[i].name, &p[i].sexy, &p[i].lesson, &p[i].score);if (p[i].sexy == 'M' && (minId == -1 || p[minId].score > p[i].score)) {minId = i;}if (p[i].sexy == 'F' && (maxId == -1 || p[maxId].score < p[i].score)) {maxId = i;}}if (maxId == -1) {printf("Absent\n");} else {printf("%s %s\n", p[maxId].name, p[maxId].lesson);}if (minId == -1) {printf("Absent\n");} else {printf("%s %s\n", p[minId].name, p[minId].lesson);}if (maxId == -1 || minId == -1) {printf("NA\n");} else {printf("%d\n", p[maxId].score - p[minId].score);}return 0; }B1027 打印沙漏
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1e5 + 10;int n; char c;int main() { cin >> n >> c;int row = 1;int res = 0;while ((2 * row - 1 + 1) / 2 * row * 2 - 1 <= n ) {row ++ ;}row -- ;res = n - ((2 * row - 1 + 1) / 2 * row * 2 - 1);for (int i = 1; i <= row; i ++ ) {for (int j = i - 1; j > 0; j -- ) printf(" ");for (int j = 2 * (row - i + 1) - 1; j >= 1; j -- ) printf("%c", c);printf("\n");}for (int i = 2; i <= row; i ++ ) {for (int j = row - i; j > 0; j -- ) printf(" ");for (int j = 2 * i - 1; j > 0; j -- ) printf("%c", c);printf("\n");}printf("%d\n", res);return 0; }A1031 Hello world for U
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 1e3;int n; char c[N][N], a[N];int main() { scanf("%s", a);int len = strlen(a);int side = (len + 2) / 3;int bottom = (len + 2) - side * 2;for (int i = 0; i < side; i ++ ) {c[i][0] = a[i];c[i][bottom - 1] = a[side + bottom + side - 3 - i];}for (int i = 0; i < bottom; i ++ ) {c[side - 1][i] = a[side - 1 + i];}for (int i = 0; i < side; i ++ ) {for (int j = 0; j < bottom; j ++ ) {if (c[i][j] != NULL) {printf("%c", c[i][j]);} else {printf(" ");}}printf("\n");}return 0; }B1002 寫出這個數
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 110;char a[N]; char b[N][N]; int n, cnt[20];int main() { cin >> a;int len = strlen(a);char mp[][20] = {"lin", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};for (int i = 0; i < len; i ++ ) {cnt[a[i] - '0'] ++ ;}for (int i = 0; i <= 9; i ++ ) {n += i * cnt[i];}int num = 0;while (n) {int r = n % 10;strcpy(b[num], mp[r]);num ++ ;n /= 10;}for (int i = num - 1; i >= 0; i -- ) {printf("%s", b[i]);if (i != 0) {printf(" ");}}printf("\n");return 0; }字符串復制strcpy(des, src)
B1014福爾摩斯的約會
錯誤
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 70;char a[10][N];int main() { char dayMap[][N] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};for (int i = 0; i < 4; i ++ ) {scanf("%s", a[i]);}int flag = 0;int len = strlen(a[0]);for (int i = 0; i < len; i ++ ) {if (a[0][i] == a[1][i] && (a[0][i] >= 'A' && a[0][i] <= 'G') && flag == 0) {printf("%s ", dayMap[a[0][i] - 'A']);flag ++ ;continue;}if (a[0][i] == a[1][i] && flag == 1) {if (a[0][i] >= '0' && a[0][i] <= 9) {printf("0%c:", a[0][i]);} else if (a[0][i] >= 'A' && a[0][i] <= 'N') {printf("%d:", a[0][i] - 'A' + 10);} else {continue;}flag ++ ;continue;}if (flag == 2) {break;}}len = strlen(a[2]);for (int i = 0; i < len; i ++ ) {if (a[2][i] == a[3][i] && ((a[2][i] >= 'a' && a[2][i] <= 'z') || (a[2][i] >= 'A' && a[2][i] <= 'Z'))) {printf("%02d\n", i);}}return 0; }B1024科學計數法
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 10010;char str[N];int main() { scanf("%s", str);int len = strlen(str);if (str[0] == '-') printf("-");int pos = 0; // 存放 E 的位置while (str[pos] != 'E') {pos ++ ;}int exp = 0; // 存放指數for (int i = pos + 2; i < len; i ++ ) {exp = exp * 10 + (str[i] - '0');}if (exp == 0) {for (int i = 1; i < pos; i ++ ) {printf("%c", str[i]);}}if (str[pos + 1] == '-') { // 如果是負指數printf("0.");for (int i = 0; i < exp - 1; i ++ ) {printf("0");}printf("%c", str[1]); // 輸出除了小數點以外的數據for (int i = 3; i < pos; i ++ ) {printf("%c", str[i]);}} else { // 如果指數為正for (int i = 1; i < pos; i ++ ) { // 輸出小數點移動之后的數if (str[i] == '.') continue;printf("%c", str[i]);if (i == exp + 2 && pos - 3 != exp) {printf(".");}}// 如果指數 exp 較大,輸出多余的 0for (int i = 0; i < exp - (pos - 3); i ++ ) {printf("0");}}printf("\n");return 0; }B1048數字加密
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h>using namespace std;typedef long long LL;const int N = 110;char A[N], B[N], ans[N]; char mp[20] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};void reverse(char s[]) {int len = strlen(s);for (int i = 0; i < len / 2; i ++ ) {char temp = s[i];s[i] = s[len - 1 - i];s[len - 1 - i] = temp;} }int main() { cin >> A >> B;reverse(A);reverse(B);int lenA = strlen(A);int lenB = strlen(B);int len = lenA >= lenB ? lenA : lenB;for (int i = 0; i < len; i ++ ) {char a = i < lenA ? A[i] : '0';char b = i < lenB ? B[i] : '0';if (i % 2 == 0) { // 奇數ans[i] = mp[(a - '0' + b - '0') % 13];} else { // 偶數ans[i] = (b - a + 10) % 10 + '0';}}reverse(ans);puts(ans);return 0; }正確做法
總結
- 上一篇: 字符串处理 回文串 说反话
- 下一篇: python多线程信息提示