diff --git a/.workflow/test.yml b/.workflow/test.yml index 7bcd60cc2e1b0d149731f183a4773a5be1fada26..017cda7463c33be2580efe01c31af0ad7eba6896 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: diff --git a/exercises/01_helloworld.c b/exercises/01_helloworld.c index e6100166bcb8fd1064fc94f2bb0e1a6952ba8bd1..29a435c671cfd67b61096050802a5b0767c1afa2 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 +} diff --git a/exercises/02_loop.c b/exercises/02_loop.c index 3e361e61656c2d9ea05348a15fa275dcd3201b8a..552e88c67072d28d81475bb49060feca0772f103 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 d80c6903dd22e3305e35ff2bfb82f949e9975617..9047331631a3535937759a47a117614186d4a507 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 28e3f5f8cd5dbb074c60d947c925a13d8918234e..2bfd128c021f19e2aa6178d26d91c5b8cda27bb0 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 330f2045f5cda70d21728503ef40accd04cea14f..6c0a0726a71b79cd54ec600453f666b9b6faa90f 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; +}