1
0
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:
Baoshuo Ren 2024-10-30 10:26:56 +08:00
parent 5d99dbe80d
commit 9371fd2d24
Failed to extract signature
8 changed files with 94 additions and 0 deletions

View 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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 323 KiB

View 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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 306 KiB

View 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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 KiB

View 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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB