1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-11-23 09:38:42 +00:00

【实践课外】4.循环结构1

This commit is contained in:
Baoshuo Ren 2024-10-25 16:43:54 +08:00
parent f8068129fe
commit 5d99dbe80d
Failed to extract signature
16 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,20 @@
#include <stdio.h>
int gcd(int x, int y) {
if (x == 0) return y;
return gcd(y % x, x);
}
int lcm(int x, int y) {
return x / gcd(x, y) * y;
}
int main() {
int m, n;
scanf("%d%d", &m, &n);
printf("%d %d\n", gcd(m, n), lcm(m, n));
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

View File

@ -0,0 +1,28 @@
#include <stdio.h>
int main() {
int x;
scanf("%d", &x);
if (x <= 1) {
printf("No\n");
} else {
int is_prime = 1;
for (int i = 2; i <= x / i; i++) {
if (x % i == 0) {
is_prime = 0;
break;
}
}
if (is_prime == 1) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 322 KiB

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 281 KiB

View File

@ -0,0 +1,22 @@
#include <stdio.h>
int main() {
int n;
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) {
char c;
double h;
scanf("%c %lf\n", &c, &h);
if (c == 'M') {
printf("%.2lf\n", h / 1.09);
} else { // c == 'F'
printf("%.2lf\n", h * 1.09);
}
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 352 KiB

View File

@ -0,0 +1,19 @@
#include <stdio.h>
int main() {
int m, n, s1 = 0, s2 = 0;
scanf("%d%d", &m, &n);
for (int i = m; i <= n; i++) {
if (i % 2 == 1) {
s1 += i * i;
} else { // i % 2 == 0
s2 += i * i * i;
}
}
printf("%d %d\n", s1, s2);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 305 KiB

View File

@ -0,0 +1,17 @@
#include <stdio.h>
int main() {
long long n;
int ans = 0;
scanf("%lld", &n);
while (n != 0) {
ans += n % 10;
n /= 10;
}
printf("%d\n", ans);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB

View File

@ -0,0 +1,27 @@
#include <stdio.h>
int main() {
int year;
scanf("%d", &year);
if (2000 < year && year <= 2100) {
int count = 0;
for (int i = 2001; i <= year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
printf("%d\n", i);
count++;
}
}
if (count == 0) {
printf("None\n");
}
} else {
printf("Invalid year!\n");
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 KiB

View File

@ -0,0 +1,16 @@
#include <stdio.h>
int main() {
int n;
double ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
ans += 1.0 / (i * 2 + 1);
}
printf("%.2lf\n", ans);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB