From 8be8f62946b8b15412fdecb0f40369df2a8a9270 Mon Sep 17 00:00:00 2001 From: 181******69 <2031263745@qq.com> Date: Thu, 6 Jun 2024 06:50:26 +0000 Subject: [PATCH 1/3] 1 Signed-off-by: 181******69 <2031263745@qq.com> --- exercises/01_helloworld.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/exercises/01_helloworld.c b/exercises/01_helloworld.c index e610016..29a435c 100644 --- a/exercises/01_helloworld.c +++ b/exercises/01_helloworld.c @@ -3,9 +3,7 @@ int main(){ // Print "Hello World!" to the console - - - + printf("Hello World!\n"); return 0; -} \ No newline at end of file +} -- Gitee From e074b1446726cf0d474bd92a9f31bc9d88eed374 Mon Sep 17 00:00:00 2001 From: 181******69 <2031263745@qq.com> Date: Thu, 6 Jun 2024 06:56:09 +0000 Subject: [PATCH 2/3] 1 Signed-off-by: 181******69 <2031263745@qq.com> --- .workflow/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.workflow/test.yml b/.workflow/test.yml index 7bcd60c..017cda7 100644 --- a/.workflow/test.yml +++ b/.workflow/test.yml @@ -1,6 +1,6 @@ version: '1.0' -name: test -displayName: test +name: take +displayName: take triggers: trigger: auto push: -- Gitee From 26005a2c8f36ff7bf617deffe3ba4b6e40bfa78b Mon Sep 17 00:00:00 2001 From: 181******69 <2031263745@qq.com> Date: Thu, 6 Jun 2024 07:03:32 +0000 Subject: [PATCH 3/3] all Signed-off-by: 181******69 <2031263745@qq.com> --- exercises/02_loop.c | 9 +++++--- exercises/03_nested_loops.c | 17 ++++++++++---- exercises/04_prime_number.c | 35 ++++++++++++++++++++-------- exercises/05_josephus_ring.c | 45 +++++++++++++++++++++--------------- 4 files changed, 70 insertions(+), 36 deletions(-) diff --git a/exercises/02_loop.c b/exercises/02_loop.c index 3e361e6..552e88c 100644 --- a/exercises/02_loop.c +++ b/exercises/02_loop.c @@ -7,7 +7,10 @@ int main(void) { - //TODO + // 使用 for 循环从 1 迭代到 10 + for (int i = 1; i <= 10; i++) { + printf("%d\n", i); // 打印当前的计数值,每个值打印在新的一行上 + } - return 0; -} \ No newline at end of file + return 0; +} diff --git a/exercises/03_nested_loops.c b/exercises/03_nested_loops.c index d80c690..9047331 100644 --- a/exercises/03_nested_loops.c +++ b/exercises/03_nested_loops.c @@ -10,8 +10,17 @@ */ int main(void) -{ - //TODO +{ + // 外层循环控制行数 + for (int i = 1; i <= 9; i++) { + // 内层循环控制列数 + for (int j = 1; j <= i; j++) { + // 打印当前列数和行数的乘积 + printf("%d*%d=%d\t", j, i, i * j); + } + // 打印换行符以开始新的一行 + printf("\n"); + } - return 0; -} \ No newline at end of file + return 0; +} diff --git a/exercises/04_prime_number.c b/exercises/04_prime_number.c index 28e3f5f..2bfd128 100644 --- a/exercises/04_prime_number.c +++ b/exercises/04_prime_number.c @@ -1,19 +1,34 @@ #include #include -//查找100以内的最大素数 +// 查找100以内的最大素数 int main(void) { - int i, j; - int max = 0; + int i, j; + int max = 0; + int is_prime; - for (i = 1; i <= 100; i++) - { - //TODO - } + for (i = 2; i <= 100; i++) // 从2开始,因为1不是素数 + { + is_prime = 1; // 假设i是素数 - printf("max = %d\n", max); + for (j = 2; j <= sqrt(i); j++) // 只检查到sqrt(i)就可以了 + { + if (i % j == 0) // 如果i能被j整除,那么i不是素数 + { + is_prime = 0; + break; + } + } - return 0; -} \ No newline at end of file + if (is_prime) // 如果i是素数,更新max + { + max = i; + } + } + + printf("max = %d\n", max); + + return 0; +} diff --git a/exercises/05_josephus_ring.c b/exercises/05_josephus_ring.c index 330f204..6c0a072 100644 --- a/exercises/05_josephus_ring.c +++ b/exercises/05_josephus_ring.c @@ -1,29 +1,36 @@ #include -/** - * 给定100个人站成一圈,从第1个人开始依次报数。 - * 每数到3的人将会被淘汰,然后继续从下一个人开始报数。 - * 这个过程会一直持续,直到所有的人都被淘汰。 - * 请编写一个C语言程序来模拟这个过程,并且输出每一个被淘汰人的编号。 - * 要求:输出每一个被淘汰人的编号,每淘汰一个人输出一行,格式为:"%d out \n"(每输出一次换行) -*/ - -#define ALL_NUM 100 -#define COUNT_NUM 3 -#define OUT_NUM 3 +#define ALL_NUM 100 +#define COUNT_NUM 3 +#define OUT_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 left = ALL_NUM; /* 剩余人数 */ + int pos = 0; /* 当前报数位置 */ + int step = 0; /* 当前报数 */ + + /* 初始化数组,每个人的编号从1到100 */ + for (int i = 0; i < ALL_NUM; i++) { + people[i] = i + 1; + } + + while (left > 0) { + if (people[pos] != 0) { /* 如果这个位置的人还没有被淘汰 */ + step++; /* 报数加1 */ - //TODO - - + if (step == COUNT_NUM) { /* 如果报数到3 */ + printf("%d out\n", people[pos]); /* 输出被淘汰人的编号 */ + people[pos] = 0; /* 将这个位置的人标记为已淘汰 */ + left--; /* 剩余人数减1 */ + step = 0; /* 重置报数 */ + } + } + pos = (pos + 1) % ALL_NUM; /* 移动到下一个位置,循环回到开头 */ + } - return 0; -} \ No newline at end of file + return 0; +} -- Gitee