第4天作业

This commit is contained in:
flykhan 2023-07-08 16:27:14 +08:00
parent 441a7c1ba8
commit 26bd3514b6
10 changed files with 190 additions and 0 deletions

10
day4/homework/h10.c Normal file
View File

@ -0,0 +1,10 @@
// 请编程, 定义一个char类型的变量m,并初始化赋值0xf1;要求按原格式输出和十进制输出
#include <stdio.h>
int main()
{
char m = 0xf1;
printf("原格式输出: 0x%hhx\n", m);
printf("十进制输出: %d\n", m);
return 0;
}

11
day4/homework/h12.c Normal file
View File

@ -0,0 +1,11 @@
// 请编程, 定义char类型的变量n初始化值为-128, 将n值减1之后再输出它的十六进制数值和十进制数值
#include <stdio.h>
int main()
{
char n = -128;
n -= 1;
printf("十六进制数值为: 0x%hhx\n", n);
printf("十进制数值为: %d\n", n);
return 0;
}

38
day4/homework/h13.c Normal file
View File

@ -0,0 +1,38 @@
// 【扩展题1】请编程输出0-1000以内的水仙花数
// 【提示】水仙花数算法:一个数 = 它各位的立方和,例如 : 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3
#include <stdio.h>
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;
}

45
day4/homework/h14.c Normal file
View File

@ -0,0 +1,45 @@
// 【扩展题2】请编程,从键盘接收年、月、日三个整数,判断是这一年的第几天。
// 【提示】必然考虑年是否为闰年,闰年条件:能被4整除且不能被100整除,或者能被400整除
#include <stdio.h>
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;
}
}
}

21
day4/homework/h2.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
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;
}

26
day4/homework/h3.c Normal file
View File

@ -0,0 +1,26 @@
// 请编程,将一个整数变量输出不同进制格式的结果
#include <stdio.h>
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;
}

7
day4/homework/h4.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
int main()
{
printf("%d\t%d",3,4);
return 0;
}

9
day4/homework/h7.c Normal file
View File

@ -0,0 +1,9 @@
// 请计算 char类型的0x89输出整数结果是多少
#include <stdio.h>
int main()
{
char c = 0x89;
printf("%d\n", c);
return 0;
}

9
day4/homework/h8.c Normal file
View File

@ -0,0 +1,9 @@
// 请计算 0b11000001有符号整数结果是多少
#include <stdio.h>
int main()
{
signed char c = 0b11000001;
printf("%d\n", c);
return 0;
}

14
day4/homework/h9.c Normal file
View File

@ -0,0 +1,14 @@
// 请编程定义一个double类型的变量n和一个char类型的变量m,请计算它们的和并输出
#include <stdio.h>
int main()
{
double n = 2.3;
char m = 'a';
printf("a = %d\n", m);
printf("n + m = %.2lf\n", n + m);
return 0;
}