From 01732cc14f1a4748e27a673e47dbe7d4017fb4e9 Mon Sep 17 00:00:00 2001 From: MwumLi Date: Wed, 13 Sep 2023 03:13:55 +0800 Subject: [PATCH] feat: add c codes for iteration (#735) Co-authored-by: Yudong Jin --- codes/c/chapter_computational_complexity/CMakeLists.txt | 1 + codes/c/chapter_computational_complexity/iteration.c | 9 ++++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/codes/c/chapter_computational_complexity/CMakeLists.txt b/codes/c/chapter_computational_complexity/CMakeLists.txt index 6938858c..9698a3bd 100644 --- a/codes/c/chapter_computational_complexity/CMakeLists.txt +++ b/codes/c/chapter_computational_complexity/CMakeLists.txt @@ -1,3 +1,4 @@ +add_executable(iteration iteration.c) add_executable(time_complexity time_complexity.c) add_executable(worst_best_time_complexity worst_best_time_complexity.c) add_executable(space_complexity space_complexity.c) diff --git a/codes/c/chapter_computational_complexity/iteration.c b/codes/c/chapter_computational_complexity/iteration.c index 6268f03a..de49ab51 100644 --- a/codes/c/chapter_computational_complexity/iteration.c +++ b/codes/c/chapter_computational_complexity/iteration.c @@ -1,7 +1,7 @@ /** * File: iteration.c * Created Time: 2023-09-09 - * Author: Gonglja (glj0@outlook.com) + * Author: Gonglja (glj0@outlook.com), MwumLi (mwumli@hotmail.com) */ #include "../utils/common.h" @@ -10,7 +10,7 @@ int forLoop(int n) { int res = 0; // 循环求和 1, 2, ..., n-1, n - for (int i = 1; i <= n; ++i) { + for (int i = 1; i <= n; i++) { res += i; } return res; @@ -47,11 +47,10 @@ char *nestedForLoop(int n) { // n * n 为对应点数量,"(i, j), " 对应字符串长最大为 6+10*2,加上最后一个空字符 \0 的额外空间 int size = n * n * 26 + 1; char *res = malloc(size * sizeof(char)); - // 循环 i = 1, 2, ..., n-1, n - for (int i = 1; i <= n; ++i) { + for (int i = 1; i <= n; i++) { // 循环 j = 1, 2, ..., n-1, n - for (int j = 1; j <= n; ++j) { + for (int j = 1; j <= n; j++) { char tmp[26]; snprintf(tmp, sizeof(tmp), "(%d, %d), ", i, j); strncat(res, tmp, size - strlen(res) - 1);