mirror of
https://github.com/renbaoshuo/202401-programming-assignments.git
synced 2024-11-23 09:38:42 +00:00
【实践课内】5.循环结构2
This commit is contained in:
parent
5d99dbe80d
commit
9371fd2d24
33
【实践课内】5.循环结构2/7-1 循环-sum.c
Normal file
33
【实践课内】5.循环结构2/7-1 循环-sum.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
【实践课内】5.循环结构2/7-1 循环-sum.jpg
Normal file
BIN
【实践课内】5.循环结构2/7-1 循环-sum.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 323 KiB |
22
【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.c
Normal file
22
【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.jpg
Normal file
BIN
【实践课内】5.循环结构2/7-2 循环-奇怪的斐波那契数列.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 306 KiB |
22
【实践课内】5.循环结构2/7-3 循环-字符统计.c
Normal file
22
【实践课内】5.循环结构2/7-3 循环-字符统计.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
【实践课内】5.循环结构2/7-3 循环-字符统计.jpg
Normal file
BIN
【实践课内】5.循环结构2/7-3 循环-字符统计.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 300 KiB |
17
【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.c
Normal file
17
【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.jpg
Normal file
BIN
【实践课内】5.循环结构2/7-4 循环-直角三角形的输出.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 282 KiB |
Loading…
Reference in New Issue
Block a user