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 11:44:18 +08:00
parent 9371fd2d24
commit 012b17b867
Failed to extract signature
16 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,25 @@
#include <stdio.h>
int main() {
int a, b, sum = 0;
scanf("%d %d", &a, &b);
for (int i = 0; i < (b - a + 1); i++) {
printf("%5d", a + i);
sum += a + i;
if (i % 5 == 4) {
printf("\n");
}
}
if ((b - a) % 5 != 4) {
printf("\n");
}
printf("Sum = %d\n", sum);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 KiB

View File

@ -0,0 +1,21 @@
#include <stdio.h>
int main() {
int a, n, sum = 0;
scanf("%d%d", &a, &n);
for (int i = 1; i <= n; i++) {
int x = 0;
for (int j = 0; j < i; j++) {
x = x * 10 + a;
}
sum += x;
}
printf("%d\n", sum);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB

View File

@ -0,0 +1,21 @@
#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
if (sum == n) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

View File

@ -0,0 +1,18 @@
#include <stdio.h>
int main() {
int n;
char c = 'A';
scanf("%d", &n);
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("%c ", c++);
}
printf("\n");
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 KiB

View File

@ -0,0 +1,17 @@
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d*%d=%-4d", j, i, i * j);
}
printf("\n");
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 341 KiB

View File

@ -0,0 +1,23 @@
#include <stdio.h>
int main() {
int x, count = 0;
scanf("%d", &x);
for (int i = x / 5; i >= 1; i--) {
for (int j = x / 2; j >= 1; j--) {
for (int k = x; k >= 1; k--) {
if (i * 5 + j * 2 + k == x) {
printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n", i, j, k, i + j + k);
count++;
}
}
}
}
printf("count = %d\n", count);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 KiB

View File

@ -0,0 +1,42 @@
#include <stdio.h>
int main() {
int num, n, finished = 0;
scanf("%d%d", &num, &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
if (x < 0) {
printf("Game Over\n");
finished = 1;
break;
} else if (x == num) {
if (i == 1) {
printf("Bingo!\n");
} else if (i <= 3) {
printf("Lucky You!\n");
} else {
printf("Good Guess!\n");
}
finished = 1;
break;
} else if (x < num) {
printf("Too small\n");
} else { // x > num
printf("Too big\n");
}
}
if (finished == 0) {
printf("Game Over\n");
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 KiB

View File

@ -0,0 +1,39 @@
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
// 上半部分
for (int i = n * 2 - 1; i >= 1; i -= 2) {
// 空格
for (int j = 1; j <= (n * 2 - 1 - i) / 2; j++) {
printf(" ");
}
// 沙漏主体
for (int j = 1; j <= i; j++) {
printf("#");
}
printf("\n");
}
// 下半部分
for (int i = 3; i <= n * 2 - 1; i+= 2) {
// 空格
for (int j = 1; j <= (n * 2 - 1 - i) / 2; j++) {
printf(" ");
}
// 沙漏主体
for (int j = 1; j <= i; j++) {
printf("#");
}
printf("\n");
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 KiB