diff --git a/exercises/01_helloworld.c b/exercises/01_helloworld.c index e6100166bcb8fd1064fc94f2bb0e1a6952ba8bd1..f040b4ed7ea28c41abdfcac208ddaa8aafcd667b 100644 --- a/exercises/01_helloworld.c +++ b/exercises/01_helloworld.c @@ -4,8 +4,6 @@ int main(){ // Print "Hello World!" to the console - - - + printf("Hello World!\n"); return 0; } \ No newline at end of file diff --git a/exercises/02_loop.c b/exercises/02_loop.c index 3e361e61656c2d9ea05348a15fa275dcd3201b8a..9364553c31bf4871fe5c18bc073ce692bf2e5aee 100644 --- a/exercises/02_loop.c +++ b/exercises/02_loop.c @@ -8,6 +8,9 @@ int main(void) { //TODO - + int i; + for (i = 1; i <= 10; i++) { + printf("%d\n", i); + } return 0; } \ No newline at end of file diff --git a/exercises/03_nested_loops.c b/exercises/03_nested_loops.c index d80c6903dd22e3305e35ff2bfb82f949e9975617..e19d2c4b839698e5d8f39b3c1e9afe060c0350bb 100644 --- a/exercises/03_nested_loops.c +++ b/exercises/03_nested_loops.c @@ -12,6 +12,17 @@ int main(void) { //TODO + int i, j; + // 外层循环控制乘法表的行数 + for (i = 1; i <= 9; i++) { + // 内层循环控制每行中的列数 + for (j = 1; j <= i; j++) { + // 打印出当前列数和行数的乘积,后面跟随一个制表符 + printf("%d*%d=%d\t", j, i, i * j); + } + // 每行结束后打印一个换行符 + printf("\n"); + } return 0; } \ No newline at end of file diff --git a/exercises/04_prime_number.c b/exercises/04_prime_number.c index 28e3f5f8cd5dbb074c60d947c925a13d8918234e..bef27739951afcdb19ad4a42bfe66b1816dab945 100644 --- a/exercises/04_prime_number.c +++ b/exercises/04_prime_number.c @@ -1,19 +1,32 @@ #include #include - //查找100以内的最大素数 + + int main(void) { - int i, j; - int max = 0; + int i, j; + int max = 0; - for (i = 1; i <= 100; i++) - { - //TODO - } + for (i = 2; i <= 100; i++) // 从2开始,因为1不是素数 + { + int isPrime = 1; // 假设当前数是素数 + for (j = 2; j * j <= i; j++) // 只检查到i的平方根 + { + if (i % j == 0) // 如果i能被j整除,则不是素数 + { + isPrime = 0; // 标记为非素数 + break; // 跳出内层循环 + } + } + if (isPrime && i > max) // 如果是素数且大于当前最大素数 + { + max = i; // 更新最大素数 + } + } - printf("max = %d\n", max); + printf("max = %d\n", max); // 打印最大素数 - return 0; + return 0; } \ No newline at end of file diff --git a/exercises/05_josephus_ring.c b/exercises/05_josephus_ring.c index 330f2045f5cda70d21728503ef40accd04cea14f..e94051af30e3d32b73d09d73373b223fe4c603bf 100644 --- a/exercises/05_josephus_ring.c +++ b/exercises/05_josephus_ring.c @@ -8,22 +8,46 @@ * 要求:输出每一个被淘汰人的编号,每淘汰一个人输出一行,格式为:"%d out \n"(每输出一次换行) */ -#define ALL_NUM 100 -#define COUNT_NUM 3 -#define OUT_NUM 3 +#include +#define ALL_NUM 100 +#define COUNT_NUM 3 -/* people id array such as (1,2,3,4,5,6) */ int people[ALL_NUM]; -int main(void) -{ - int left; /* 剩余人数 */ - int pos; /* 当前报数位置 */ - int step; /* 当前报数 */ +int main(void) { + int left = ALL_NUM; /* 剩余人数 */ + int pos = 0; /* 当前报数位置 */ + int step = 0; /* 当前报数 */ + + // 初始化people数组 + for (int i = 0; i < ALL_NUM; i++) { + people[i] = i + 1; + } + + // 模拟淘汰过程 + while (left > 0) { + // 如果当前位置的人已经被淘汰,则跳过 + if (people[pos] == 0) { + pos = (pos + 1) % ALL_NUM; + continue; + } + + // 报数 + step++; + if (step == COUNT_NUM) { + // 输出被淘汰人的编号 + printf("%d out \n", people[pos]); + // 标记被淘汰 + people[pos] = 0; + // 剩余人数减一 + left--; + // 重置报数 + step = 0; + } - //TODO - - + // 移动到下一个人 + pos = (pos + 1) % ALL_NUM; + } - return 0; + return 0; } \ No newline at end of file