From 87e499431c3b974f4387d508fe2d286741294e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=AB=E8=8C=AB?= <18309506725@163.com> Date: Mon, 3 Jun 2024 01:53:18 +0000 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E7=AD=94=E9=A2=98?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 茫茫 <18309506725@163.com> --- exercises/01_helloworld.c | 4 +-- exercises/02_loop.c | 5 +++- exercises/03_nested_loops.c | 11 ++++++++ exercises/04_prime_number.c | 31 +++++++++++++++------- exercises/05_josephus_ring.c | 50 ++++++++++++++++++++++++++---------- 5 files changed, 75 insertions(+), 26 deletions(-) diff --git a/exercises/01_helloworld.c b/exercises/01_helloworld.c index e610016..f040b4e 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 3e361e6..9364553 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 d80c690..e19d2c4 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 28e3f5f..bef2773 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 330f204..e94051a 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 -- Gitee