diff --git a/source/_drafts/answer5.md b/source/_drafts/answer5.md new file mode 100644 index 0000000..c6bd6db --- /dev/null +++ b/source/_drafts/answer5.md @@ -0,0 +1,231 @@ +--- +title: 第五次作业(答案) +date: 2020-11-11 22:11:05 +tags: +--- + +# P138-17 + +答案:A对Z,B对X,C对Y + +```C +#include + +int main() +{ + int result[3] = { 0, 0, 0 }; // 分别表示ABC三人的对手 + + // 分别给ABC指派对手 + for (int i = 0; i < 3; ++i) { // A + result[0] = 'X' + i; + for (int j = 0; j < 3; ++j) { // B + result[1] = 'X' + j; + for (int k = 0; k < 3; ++k) { // C + result[2] = 'X' + k; + // 检查合法性 + + // 两人的对手不能一样 + if (result[0] == result[1]) { + continue; + } + if (result[0] == result[2]) { + continue; + } + if (result[1] == result[2]) { + continue; + } + + // A不和X比 + if (result[0] == 'X') { + continue; + } + + // C 不和X,Z比 + if (result[2] == 'X') { + continue; + } + if (result[2] == 'Z') { + continue; + } + + // 剩下的就是正确答案 + printf("A的对手是%c\n", result[0]); + printf("B的对手是%c\n", result[1]); + printf("C的对手是%c\n", result[2]); + } + } + } + return 0; +} +``` + +# 二分查找 + +```C +#include + +int main() +{ + int a[100]; + + printf("二分查找"); + int n; + printf("请输入n:\nn = "); + scanf("%d", &n); + printf("请输入n个有序数(从小到大):\n"); + for (int i = 0; i < n; ++i) { + scanf("%d", a + i); + } + printf("请输入你要找的数:"); + int m; + scanf("%d", &m); + + int l = 0; + int r = n - 1; + while (1) { + if (l == r) { + if (m == a[l]) { + printf("a[%d] = %d\n", l, m); + } else if (m < a[l]) { + for (int i = n - 1; i >= l; --i) { + a[i + 1] = a[i]; + } + a[l] = m; + printf("%d被插入到了a[%d]的位置\n", m, l); + for (int i = 0; i < n + 1; ++i) { + printf("a[%d] = %d\n", i, a[i]); + } + } else { + for (int i = n - 1; i >= l + 1; --i) { + a[i + 1] = a[i]; + } + a[l + 1] = m; + printf("%d被插入到了a[%d]的位置\n", m, l + 1); + for (int i = 0; i < n + 1; ++i) { + printf("a[%d] = %d\n", i, a[i]); + } + } + break; + } else if (r == l + 1) { + if (m == a[l]) { + printf("a[%d] = %d\n", l, m); + break; + } else if (m < a[l]) { + for (int i = n - 1; i >= l; --i) { + a[i + 1] = a[i]; + } + a[l] = m; + printf("%d被插入到了a[%d]的位置\n", m, l); + for (int i = 0; i < n + 1; ++i) { + printf("a[%d] = %d\n", i, a[i]); + } + break; + } else { + l++; + } + } else { + int c = (l + r) / 2; + if (m == a[c]) { + printf("a[%d] = %d\n", c, m); + break; + } else if (m < a[c]) { + r = c - 1; + } else { + l = c + 1; + } + } + } + + return 0; +} +``` + +# P165-3 + +```C +#include + +int main() +{ + int a[3][3]; + printf("请输入一个3x3的矩阵:\n"); + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + scanf("%d", &a[i][j]); + } + } + int sum = a[0][0] + a[0][2] + a[1][1] + a[2][0] + a[2][2]; + + printf("对角线元素之和为%d\n", sum); + + return 0; +} +``` + +# P165-6 + +```C +#include + +int main() +{ + int a[10][10]; + for (int i = 0; i < 10; ++i) { + a[i][0] = 1; + a[i][i] = 1; + } + + for (int i = 1; i < 10; ++i) { + for (int j = 1; j < i; ++j) { + a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; + } + } + + for (int i = 0; i < 10; ++i) { + for (int j = 0; j <= i; ++j) { + printf("%-5d", a[i][j]); + } + printf("\n"); + } + + return 0; +} +``` + +# P165-10 + +```C +#include + +int main() +{ + int upper = 0, lower = 0, num = 0, space = 0, others = 0; + for (int i = 0; i < 80; ++i) { + int c = getchar(); + + if (c == ' ') { + space++; + continue; + } + if (c >= 'a' && c <= 'z') { + lower++; + continue; + } + if (c >= 'A' && c <= 'Z') { + upper++; + continue; + } + if (c >= '0' && c <= '9') { + num++; + continue; + } + others++; + } + printf("大写字母有%d\n", upper); + printf("小写字母有%d\n", lower); + printf("数字有%d\n", num); + printf("空格%d\n", space); + printf("其它字符有%d\n", others); + return 0; +} +```