diff --git a/【实践课内】5.循环结构2/7-1 循环-sum.c b/【实践课内】5.循环结构2/7-1 循环-sum.c new file mode 100644 index 0000000..41ccb76 --- /dev/null +++ b/【实践课内】5.循环结构2/7-1 循环-sum.c @@ -0,0 +1,33 @@ +#include + +int main() { + int n, sum_pos = 0, sum_neg = 0; + + scanf("%d", &n); + + for (int i = 1; i <= n; i++) { + int x; + + scanf("%d", &x); + + if (x > 0) { + sum_pos += x; + } else if (x < 0) { + sum_neg += x; + } // when x == 0, do nothing + } + + if (sum_pos == 0) { + printf("no positive number "); + } else { + printf("%d ", sum_pos); + } + + if (sum_neg == 0) { + printf("no negative number\n"); + } else { + printf("%d\n", sum_neg); + } + + return 0; +} diff --git a/【实践课内】5.循环结构2/7-1 循环-sum.jpg b/【实践课内】5.循环结构2/7-1 循环-sum.jpg new file mode 100644 index 0000000..228f571 Binary files /dev/null and b/【实践课内】5.循环结构2/7-1 循环-sum.jpg differ diff --git a/【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.c b/【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.c new file mode 100644 index 0000000..e47a88a --- /dev/null +++ b/【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.c @@ -0,0 +1,22 @@ +#include + +int main() { + int n, f, + f_pre2 = 7, f_pre1 = 11; + + scanf("%d", &n); + + for (int i = 2; i <= n; i++) { + f = (f_pre1 + f_pre2) % 3; + f_pre2 = f_pre1; + f_pre1 = f; + } + + if (f == 0) { + printf("YES\n"); + } else { + printf("NO\n"); + } + + return 0; +} diff --git a/【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.jpg b/【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.jpg new file mode 100644 index 0000000..f9dff3e Binary files /dev/null and b/【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.jpg differ diff --git a/【实践课内】5.循环结构2/7-3 循环-字符统计.c b/【实践课内】5.循环结构2/7-3 循环-字符统计.c new file mode 100644 index 0000000..9447021 --- /dev/null +++ b/【实践课内】5.循环结构2/7-3 循环-字符统计.c @@ -0,0 +1,22 @@ +#include + +int main() { + int sum_alpha = 0, sum_digit = 0, sum_space = 0, sum_other = 0; + char c; + + while (scanf("%c", &c) != EOF && c != '\n') { + if (('A' <= c && c <= 'Z') || ('a' <= c && c <= ' z')) { // isalpha(c) + sum_alpha++; + } else if ('0' <= c && c <= '9') { // isdigit(c) + sum_digit++; + } else if (c == ' ') { + sum_space++; + } else { + sum_other++; + } + } + + printf("%d %d %d %d\n", sum_alpha, sum_space, sum_digit, sum_other); + + return 0; +} diff --git a/【实践课内】5.循环结构2/7-3 循环-字符统计.jpg b/【实践课内】5.循环结构2/7-3 循环-字符统计.jpg new file mode 100644 index 0000000..42a6520 Binary files /dev/null and b/【实践课内】5.循环结构2/7-3 循环-字符统计.jpg differ diff --git a/【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.c b/【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.c new file mode 100644 index 0000000..572f784 --- /dev/null +++ b/【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.c @@ -0,0 +1,17 @@ +#include + +int main() { + int n; + + scanf("%d", &n); + + for (int i = n; i >= 1; i--) { + for (int j = 1; j <= i; j++) { + printf("*"); + } + + printf("\n"); + } + + return 0; +} diff --git a/【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.jpg b/【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.jpg new file mode 100644 index 0000000..5d837fd Binary files /dev/null and b/【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.jpg differ