From 441a7c1ba88dcb52b4d9cd0d88fcb969a1290eb0 Mon Sep 17 00:00:00 2001 From: flykhan Date: Sat, 8 Jul 2023 16:16:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC5=E5=A4=A9=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day5/homework/h1.c | 27 +++++++++++++++++++ day5/homework/h10.c | 15 +++++++++++ day5/homework/h11.c | 22 +++++++++++++++ day5/homework/h12.c | 29 ++++++++++++++++++++ day5/homework/h13.c | 34 +++++++++++++++++++++++ day5/homework/h14.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ day5/homework/h15.c | 24 +++++++++++++++++ day5/homework/h16.c | 23 ++++++++++++++++ day5/homework/h2.c | 8 ++++++ day5/homework/h3.c | 13 +++++++++ day5/homework/h4.c | 8 ++++++ day5/homework/h5.c | 13 +++++++++ day5/homework/h6.c | 12 +++++++++ day5/homework/h7.c | 12 +++++++++ day5/homework/h8.c | 20 ++++++++++++++ day5/homework/h9.c | 27 +++++++++++++++++++ 16 files changed, 353 insertions(+) create mode 100644 day5/homework/h1.c create mode 100644 day5/homework/h10.c create mode 100644 day5/homework/h11.c create mode 100644 day5/homework/h12.c create mode 100644 day5/homework/h13.c create mode 100644 day5/homework/h14.c create mode 100644 day5/homework/h15.c create mode 100644 day5/homework/h16.c create mode 100644 day5/homework/h2.c create mode 100644 day5/homework/h3.c create mode 100644 day5/homework/h4.c create mode 100644 day5/homework/h5.c create mode 100644 day5/homework/h6.c create mode 100644 day5/homework/h7.c create mode 100644 day5/homework/h8.c create mode 100644 day5/homework/h9.c diff --git a/day5/homework/h1.c b/day5/homework/h1.c new file mode 100644 index 0000000..1194f7a --- /dev/null +++ b/day5/homework/h1.c @@ -0,0 +1,27 @@ +// 请编程,定义char变量并赋值‘\101’, 输出这个变量的十进制数和字符内容。 +// 【提示】\ddd 代表8进制, % c 输出字符 +#include + +void printBinary(char num) +{ + int size = sizeof(num); + int i = size * 8 - 1; + while (i--) + { + int bit = (num >> i) & 1; + printf("%d", bit); + } + printf("\n"); +} + +int main() +{ + char x = '\101'; + printf("十进制数: %d\n", x); + printf("二进制数: "); + printBinary(x); + printf("八进制数: %o\n", x); + printf("十六进制数: 0x%hhx\n", x); + printf("字符内容: %c\n", x); + return 0; +} \ No newline at end of file diff --git a/day5/homework/h10.c b/day5/homework/h10.c new file mode 100644 index 0000000..e5cd3b9 --- /dev/null +++ b/day5/homework/h10.c @@ -0,0 +1,15 @@ +// 请编程, 输入一个圆的半径,输出圆的周长和面积 +#include + +#define PI 3.14 // 定义 PI + +int main() +{ + int r; // 定义半径 + printf("请输入圆的半径:"); + scanf("%d", &r); + printf("圆的周长为:%.2f\n", 2 * PI * r); + printf("圆的面积为:%.2f\n", PI * r * r); + + return 0; +} \ No newline at end of file diff --git a/day5/homework/h11.c b/day5/homework/h11.c new file mode 100644 index 0000000..5614881 --- /dev/null +++ b/day5/homework/h11.c @@ -0,0 +1,22 @@ +// 请编程, 输入两个整数, 输出两个整数所有公约数。 +// 例如, 输入 12 和 18, 输出 1 2 3 6。 +#include + +int main() +{ + int a, b; + printf("请输入两个整数:"); + scanf("%d%d", &a, &b); + int min = a < b ? a : b; // 取两个数中较小的一个 + while (min) + { + if (a % min == 0 && b % min == 0) + { + printf("%d ", min); + } + min--; + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/day5/homework/h12.c b/day5/homework/h12.c new file mode 100644 index 0000000..502b4ba --- /dev/null +++ b/day5/homework/h12.c @@ -0,0 +1,29 @@ +// 请编程, 输入一个整数n, 输出斐波那契数列中第n位上的数值 +// 【提示】斐波那契数列: 1 1 2 3 5 8 13 ... +// 【示例】n = 1, 输出1; n = 7, 输出13. +#include + +int main() +{ + int a = 1, b = 1, c, n; + printf("请输入一个整数 n : "); + scanf("%d", &n); + if (n >= 1) + c = a; + if (n >= 2) + c = b; + + int temp = n - 2; + while (n > 2 && temp > 0) + { + c = a + b; + // printf("%d ", c); + a = b; + b = c; + temp--; + } + + printf("%d\n", c); + + return 0; +} \ No newline at end of file diff --git a/day5/homework/h13.c b/day5/homework/h13.c new file mode 100644 index 0000000..78f006e --- /dev/null +++ b/day5/homework/h13.c @@ -0,0 +1,34 @@ +// 请编程,实现以下操作: 定义unsigned char 型变量a,赋值0x7f, +// 将其第1、5、7位置1,2、3、6位清0,其它位保持不变。 +#include + +void printBinary(char num) +{ + int size = sizeof(num); + for (int i = size * 8 - 1; i >= 0; i--) + { + int bit = (num >> i) & 1; + printf("%d", bit); + } + printf("\n"); +} + +int main() +{ + unsigned char a = 0x7f; // 十六进制 + printf("原样输出: "); + printBinary(a); + + a |= 0b1010001; // 0b 表示二进制数的标识符 + printf("1、5、7位置1输出: "); + printBinary(a); + + a &= ~0b100110; + printf("2、3、6位清0输出: "); + printBinary(a); + + printf("结果的十六进制输出: "); + printf("0x%hhx\n", a); // 0x59 + + return 0; +} \ No newline at end of file diff --git a/day5/homework/h14.c b/day5/homework/h14.c new file mode 100644 index 0000000..d1546e9 --- /dev/null +++ b/day5/homework/h14.c @@ -0,0 +1,66 @@ +// 请编程,根据个人收入计算个人所得税,免税额为3500 + +// > 7级超额累进税率: >> 全月应纳税所得额 税率 速算扣除数(元) >> 全月应纳税额不超过1500元 3 % 0 >> 全月应纳税额超过1500元至4500元 10 % 105 >> 全月应纳税额超过4500元至9000元 20 % 555 >> 全月应纳税额超过9000元至35000元 25 % 1005 >> 全月应纳税额超过35000元至55000元 30 % 2755 >> 全月应纳税额超过55000元至80000元 35 % 5505 >> 全月应纳税额超过80000元 45 % 13505 >> 例如:你月收入 : 3000, +// 低于免税额3500,不必缴税; +// >> 月收入 : 4000, +// 4000 - 3500 = 500 < 1500, 属于第一档,缴税 = (4000 - 3500) * 0.03 - 0 == 15 月收入 : 15000, 15000 - 3500 = 11500, 属于第四档, 缴税 = (15000 - 3500) * 0.25 - 1005 == 1870 >> 要求:输入你的税前收入,输出:你最后的所得以及缴纳的税额 +#include + +int main() +{ + int pretexIncome; // 税前收入 + double rate; // 税率 + int deduct; // 速算扣除数 + int tex; // 缴纳税额 + int aftertexIncome; // 税后收入 + printf("请输入你的税前收入: "); + scanf("%d", &pretexIncome); + + int diff = pretexIncome - 3500; // 征税差值 + if (diff <= 0) + { + rate = 0; + deduct = 0; + } + else if (diff > 0 && diff <= 1500) + { + rate = 0.03; + deduct = 0; + } + else if (diff > 1500 && diff <= 4500) + { + rate = 0.1; + deduct = 105; + } + else if (diff > 4500 && diff <= 9000) + { + rate = 0.2; + deduct = 555; + } + else if (diff > 9000 && diff <= 35000) + { + rate = 0.25; + deduct = 1005; + } + else if (diff > 35000 && diff <= 55000) + { + rate = 0.3; + deduct = 2755; + } + else if (diff > 55000 && diff <= 80000) + { + rate = 0.35; + deduct = 5505; + } + else if (diff > 80000) + { + rate = 0.45; + deduct = 13505; + } + + tex = diff * rate - deduct; + aftertexIncome = pretexIncome - tex; + printf("你最后的所得为 %d ,你所缴纳的税额为 %d\n", aftertexIncome, tex); + + return 0; +} \ No newline at end of file diff --git a/day5/homework/h15.c b/day5/homework/h15.c new file mode 100644 index 0000000..3258f7b --- /dev/null +++ b/day5/homework/h15.c @@ -0,0 +1,24 @@ +// 【扩展题】请编程,键盘输入一个整数, 输出它的八进制数 +// 【提示】使用int数组完成, int m[32]; +#include + +int main() +{ + int inputNum; + printf("请输入一个整数:"); + scanf("%d", &inputNum); + int m[32]; + int i = 0; + while (inputNum) + { + m[i++] = inputNum % 8; + inputNum /= 8; + } + printf("八进制数为:"); + while (i) + { + printf("%d", m[--i]); + } + + return 0; +} \ No newline at end of file diff --git a/day5/homework/h16.c b/day5/homework/h16.c new file mode 100644 index 0000000..4e83699 --- /dev/null +++ b/day5/homework/h16.c @@ -0,0 +1,23 @@ +// 【扩展题】请编程,键盘输入一个二进制数的字符串,输出转不同进制的数值 +// 【提示】char m[32] = ""; +#include + +int main() +{ + char m[32] = ""; // 用于存储二进制数的字符串 + printf("请输入一个二进制数的字符串:"); + scanf("%s", m); + + int i = 0; + int size = sizeof(m) / sizeof(m[0]); // 数组的大小 + int sum = 0; // 用于存储转换后的十进制数 + while (i < size && m[i] != '\0') + { + sum = sum * 2 + m[i] - '0'; // 每次会将高位的结果再次乘 2 + i++; + } + printf("十进制数为: %d\n", sum); + printf("八进制数为: 0%o\n", sum); + printf("十六进制数为: 0x%hhx\n", sum); + return 0; +} \ No newline at end of file diff --git a/day5/homework/h2.c b/day5/homework/h2.c new file mode 100644 index 0000000..2acafad --- /dev/null +++ b/day5/homework/h2.c @@ -0,0 +1,8 @@ +// 请编程,输出 '\0' == 0 的结果 +#include + +int main() +{ + printf("'\\0' == 0 的结果为: %d\n", '\0' == 0); // \0 需要转义为 \\0 + return 0; +} \ No newline at end of file diff --git a/day5/homework/h3.c b/day5/homework/h3.c new file mode 100644 index 0000000..a7d01f5 --- /dev/null +++ b/day5/homework/h3.c @@ -0,0 +1,13 @@ +// 请计算,【提示】考虑运算符优先级 int n = 0, m = 1; +// m = n++, n++, n++, m++; +// 问:n, m分别为多少? +#include + +int main() +{ + int n = 0, m = 1; + // m = n++; + m = n++, n++, n++, m++; // m = 0 , 然后 m++, m = 1 + printf("n = %d, m = %d\n", n, m); + return 0; +} \ No newline at end of file diff --git a/day5/homework/h4.c b/day5/homework/h4.c new file mode 100644 index 0000000..17582b3 --- /dev/null +++ b/day5/homework/h4.c @@ -0,0 +1,8 @@ +// 请计算, 12 ^ 3 && 12 & 3 的结果 【提示】考虑运算符优先级 +#include + +int main() +{ + printf("12 ^ 3 && 12 & 3 的结果为: %d\n", 12 ^ 3 && 12 & 3); + return 0; +} \ No newline at end of file diff --git a/day5/homework/h5.c b/day5/homework/h5.c new file mode 100644 index 0000000..cde672f --- /dev/null +++ b/day5/homework/h5.c @@ -0,0 +1,13 @@ +// 请计算, int i = 1; +// i += --i && -5; +// i的结果是多少? +#include + +int main() +{ + int i = 1; + i += --i && -5; // i = 1 + 0 && -5 = 1 + 0 = 1 + // i = ((i + (--i)) && (-5)); + printf("i = %d\n", i); + return 0; +} \ No newline at end of file diff --git a/day5/homework/h6.c b/day5/homework/h6.c new file mode 100644 index 0000000..f9799ca --- /dev/null +++ b/day5/homework/h6.c @@ -0,0 +1,12 @@ +// 请计算, char n = 1; +// n = ~n; +// n的结果是多少? +#include + +int main() +{ + char n = 1; + n = ~n; // n = ~n = ~00000001 = 11111110 = -2 + printf("n = %d\n", n); + return 0; +} \ No newline at end of file diff --git a/day5/homework/h7.c b/day5/homework/h7.c new file mode 100644 index 0000000..f5e30fd --- /dev/null +++ b/day5/homework/h7.c @@ -0,0 +1,12 @@ +// 请计算, short n = 112; +// n &= ~(3 << 4); +// n的结果是多少? +#include + +int main() +{ + short n = 112; + n &= ~(3 << 4); // n = n & (~(3 << 4)) = n & (~48) = n & (-49) = n & 65487 = 112 & 65487 = 112 + printf("n = %d\n", n); + return 0; +} \ No newline at end of file diff --git a/day5/homework/h8.c b/day5/homework/h8.c new file mode 100644 index 0000000..b2ae48c --- /dev/null +++ b/day5/homework/h8.c @@ -0,0 +1,20 @@ +// 请编程,输入一个整数,输出它的所有因数。 +#include + +int main() +{ + int num; + printf("请输入一个整数: "); + scanf("%d", &num); + int i = 1; + printf("%d 的因数有: ", num); + while (i <= num) + { + if (num % i == 0) + printf("%d ", i); + i++; + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/day5/homework/h9.c b/day5/homework/h9.c new file mode 100644 index 0000000..db2fb7d --- /dev/null +++ b/day5/homework/h9.c @@ -0,0 +1,27 @@ +// 请编程, 输入一个整数,输出它的二进制数中1和0的个数。 +#include + +int main() +{ + int inputNum; + int temp = 0, zeroCnt = 0, oneCnt = 0; + + printf("请输入一个整数: "); + scanf("%d", &inputNum); + + for (int i = 0; temp < inputNum; i++) + { + temp = 1 << i; + // if ((inputNum & temp) == temp) + // oneCnt++; + // else if ((inputNum | ~temp) == ~temp) + // zeroCnt++; + if (inputNum & temp) + oneCnt++; + else + zeroCnt++; + } + + printf("%d 的二进制中 1 有 %d 个, 0 有 %d 个", inputNum, oneCnt, zeroCnt); + return 0; +} \ No newline at end of file