From 26bd3514b66510bdcf6824a954e3166389db52c3 Mon Sep 17 00:00:00 2001 From: flykhan Date: Sat, 8 Jul 2023 16:27:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC4=E5=A4=A9=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day4/homework/h10.c | 10 ++++++++++ day4/homework/h12.c | 11 +++++++++++ day4/homework/h13.c | 38 ++++++++++++++++++++++++++++++++++++++ day4/homework/h14.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ day4/homework/h2.c | 21 +++++++++++++++++++++ day4/homework/h3.c | 26 ++++++++++++++++++++++++++ day4/homework/h4.c | 7 +++++++ day4/homework/h7.c | 9 +++++++++ day4/homework/h8.c | 9 +++++++++ day4/homework/h9.c | 14 ++++++++++++++ 10 files changed, 190 insertions(+) create mode 100644 day4/homework/h10.c create mode 100644 day4/homework/h12.c create mode 100644 day4/homework/h13.c create mode 100644 day4/homework/h14.c create mode 100644 day4/homework/h2.c create mode 100644 day4/homework/h3.c create mode 100644 day4/homework/h4.c create mode 100644 day4/homework/h7.c create mode 100644 day4/homework/h8.c create mode 100644 day4/homework/h9.c diff --git a/day4/homework/h10.c b/day4/homework/h10.c new file mode 100644 index 0000000..efbc0a9 --- /dev/null +++ b/day4/homework/h10.c @@ -0,0 +1,10 @@ +// 请编程, 定义一个char类型的变量m,并初始化赋值0xf1;要求按原格式输出和十进制输出 +#include + +int main() +{ + char m = 0xf1; + printf("原格式输出: 0x%hhx\n", m); + printf("十进制输出: %d\n", m); + return 0; +} \ No newline at end of file diff --git a/day4/homework/h12.c b/day4/homework/h12.c new file mode 100644 index 0000000..c194320 --- /dev/null +++ b/day4/homework/h12.c @@ -0,0 +1,11 @@ +// 请编程, 定义char类型的变量n,初始化值为-128, 将n值减1之后,再输出它的十六进制数值和十进制数值 +#include + +int main() +{ + char n = -128; + n -= 1; + printf("十六进制数值为: 0x%hhx\n", n); + printf("十进制数值为: %d\n", n); + return 0; +} \ No newline at end of file diff --git a/day4/homework/h13.c b/day4/homework/h13.c new file mode 100644 index 0000000..e0ff954 --- /dev/null +++ b/day4/homework/h13.c @@ -0,0 +1,38 @@ +// 【扩展题1】请编程,输出0-1000以内的水仙花数 +// 【提示】水仙花数算法:一个数 = 它各位的立方和,例如 : 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 +#include + +void fun1() +{ + int i, j, k; + for (i = 0; i < 10; i++) + { + for (j = 0; j < 10; j++) + { + for (k = 0; k < 10; k++) + { + if (i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k) + printf("%d\n", i * 100 + j * 10 + k); + } + } + } +} + +void fun2() +{ + int x = 0; + while (x++ < 1000 - 1) // -1 是因为 1000 不是水仙花数 + { + int a = x / 100; + int b = x / 10 % 10; + int c = x % 10; + if (x == a * a * a + b * b * b + c * c * c) + printf("%d\n", x); + } +} + +int main() +{ + fun2(); + return 0; +} \ No newline at end of file diff --git a/day4/homework/h14.c b/day4/homework/h14.c new file mode 100644 index 0000000..6aaefa2 --- /dev/null +++ b/day4/homework/h14.c @@ -0,0 +1,45 @@ +// 【扩展题2】请编程,从键盘接收年、月、日三个整数,判断是这一年的第几天。 +// 【提示】必然考虑年是否为闰年,闰年条件:能被4整除且不能被100整除,或者能被400整除 + +#include + +int isRun(int year) +{ + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + return 1; // 是闰年 + return 0; +} + +int main() +{ + int year, month, day; + year = scanf("%d", &year); + month = scanf("%d", &month); + day = scanf("%d", &day); + int days = 0; + int mon_2 = isRun(year) ? 29 : 28; // 二月天数: 闰年29天, 平年28天 + if (month < 8) + { + if (month % 2 == 0) + { + days += month / 2 * 31 + (month / 2 - 1) * 30 + mon_2; + printf("%d\n", days); + } + else + { + days += (month / 2 + 1) * 31 + (month / 2 - 1) * 30 + mon_2; + printf("%d\n", days); + } + } + else + { + if (month % 2 == 0) + { + days += (month / 2 - 1) * 31 + month / 2 * 30 + mon_2; + } + else + { + days += (month / 2) * 31 + (month / 2) * 30 + mon_2; + } + } +} \ No newline at end of file diff --git a/day4/homework/h2.c b/day4/homework/h2.c new file mode 100644 index 0000000..4acaa5e --- /dev/null +++ b/day4/homework/h2.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + char a = 'a'; + short b = 12; + int c = 34; + unsigned int d = 43; + long int e = 232; + float f = 1.23f; + double g = 12.32; + + printf("sizeof a = %ld\n", sizeof(a)); + printf("sizeof b = %ld\n", sizeof(b)); + printf("sizeof c = %ld\n", sizeof(c)); + printf("sizeof d = %ld\n", sizeof(d)); + printf("sizeof e = %ld\n", sizeof(e)); + printf("sizeof f = %ld\n", sizeof(f)); + printf("sizeof g = %ld\n", sizeof(g)); + return 0; +} diff --git a/day4/homework/h3.c b/day4/homework/h3.c new file mode 100644 index 0000000..3a97413 --- /dev/null +++ b/day4/homework/h3.c @@ -0,0 +1,26 @@ +// 请编程,将一个整数变量输出不同进制格式的结果 +#include + +void printBinary(int num) +{ + // int size = sizeof(num) * 8; // 计算整数的位数 + int size = 8; // 计算整数的位数 + for (int i = size - 1; i >= 0; i--) + { + int bit = (num >> i) & 1; // 获取当前位的值 + printf("%d", bit); // 输出当前位的值 + } + printf("\n"); +} + +int main() +{ + int a = 19; + + printf("二进制输出: "); + printBinary(a); + printf("10 进制: %d\n", a); + printf("八进制输出: %o\n", a); + printf("十六进制输出: 0x%hhx\n", a); + return 0; +} diff --git a/day4/homework/h4.c b/day4/homework/h4.c new file mode 100644 index 0000000..7080776 --- /dev/null +++ b/day4/homework/h4.c @@ -0,0 +1,7 @@ +#include + +int main() +{ + printf("%d\t%d",3,4); + return 0; +} diff --git a/day4/homework/h7.c b/day4/homework/h7.c new file mode 100644 index 0000000..d68070b --- /dev/null +++ b/day4/homework/h7.c @@ -0,0 +1,9 @@ +// 请计算 char类型的0x89输出整数结果是多少? +#include + +int main() +{ + char c = 0x89; + printf("%d\n", c); + return 0; +} diff --git a/day4/homework/h8.c b/day4/homework/h8.c new file mode 100644 index 0000000..534a063 --- /dev/null +++ b/day4/homework/h8.c @@ -0,0 +1,9 @@ +// 请计算 0b11000001有符号整数结果是多少? +#include + +int main() +{ + signed char c = 0b11000001; + printf("%d\n", c); + return 0; +} \ No newline at end of file diff --git a/day4/homework/h9.c b/day4/homework/h9.c new file mode 100644 index 0000000..39b9132 --- /dev/null +++ b/day4/homework/h9.c @@ -0,0 +1,14 @@ +// 请编程,定义一个double类型的变量n和一个char类型的变量m,请计算它们的和并输出 +#include + +int main() +{ + double n = 2.3; + char m = 'a'; + + printf("a = %d\n", m); + + printf("n + m = %.2lf\n", n + m); + + return 0; +} \ No newline at end of file