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

【实践课外】2.选择结构1

This commit is contained in:
Baoshuo Ren 2024-10-18 18:46:44 +08:00
parent 5b0dfb811d
commit 6896ba08ed
Failed to extract signature
14 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,17 @@
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int d = n % 5;
if (1 <= d && d <= 3) {
printf("Fishing in day %d\n", n);
} else {
printf("Drying in day %d\n", n);
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 331 KiB

View File

@ -0,0 +1,51 @@
#include <stdio.h>
int main() {
double n, ans = 0;
scanf("%lf", &n);
if (0 < n && n < 36000) {
ans += 0.03 * n;
} else if (n >= 36000) {
ans += 0.03 * 36000;
}
if (36000 <= n && n < 144000) {
ans += 0.1 * (n - 36000);
} else if (n >= 144000) {
ans += 0.1 * (144000 - 36000);
}
if (144000 <= n && n < 300000) {
ans += 0.2 * (n - 144000);
} else if (n >= 300000) {
ans += 0.2 * (300000 - 144000);
}
if (300000 <= n && n < 420000) {
ans += 0.25 * (n - 300000);
} else if (n >= 420000) {
ans += 0.25 * (420000 - 300000);
}
if (420000 <= n && n < 660000) {
ans += 0.3 * (n - 420000);
} else if (n >= 660000) {
ans += 0.3 * (660000 - 420000);
}
if (660000 <= n && n < 960000) {
ans += 0.35 * (n - 660000);
} else if (n >= 960000) {
ans += 0.35 * (960000 - 660000);
}
if (960000 <= n) {
ans += 0.45 * (n - 960000);
}
printf("%.2lf\n", ans);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 356 KiB

View File

@ -0,0 +1,27 @@
#include <stdio.h>
const int DAYS[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
int main() {
int y, m, d;
scanf("%4d/%2d/%2d", &y, &m, &d);
int days = 0;
for (int i = 1; i < m; i++) {
days += DAYS[i];
if (i == 2) { // 2 月涉及到闰年
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
days++;
}
}
}
days += d;
printf("%d\n", days);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 328 KiB

View File

@ -0,0 +1,33 @@
#include <stdio.h>
int main() {
int hh, mm, ss;
scanf("%d:%d:%d", &hh, &mm, &ss);
int time_seconds = hh * 3600 + mm * 60 + ss;
int news_seconds = 19 * 3600;
int divide_seconds = 7 * 3600;
if (time_seconds <= divide_seconds) {
time_seconds += 24 * 3600;
}
if (time_seconds > news_seconds) {
int diff_seconds = time_seconds - news_seconds;
int diff_hh = diff_seconds / 3600;
int diff_mm = diff_seconds % 3600 / 60;
int diff_ss = diff_seconds % 60;
printf("+%02d:%02d:%02d\n", diff_hh, diff_mm, diff_ss);
} else {
int diff_seconds = news_seconds - time_seconds;
int diff_hh = diff_seconds / 3600;
int diff_mm = diff_seconds % 3600 / 60;
int diff_ss = diff_seconds % 60;
printf("-%02d:%02d:%02d\n", diff_hh, diff_mm, diff_ss);
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 KiB

View File

@ -0,0 +1,18 @@
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int ans1 = n * 7;
int ans2 = 15 + n * 5;
if (ans1 < ans2) {
printf("%d\n", ans1);
} else {
printf("%d\n", ans2);
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

View File

@ -0,0 +1,24 @@
#include <math.h>
#include <stdio.h>
int main() {
double x1, y1, x2, y2, x3, y3;
scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
double len1 = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
double len2 = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
double len3 = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
if (len1 + len2 <= len3 || len1 + len3 <= len2 || len2 + len3 <= len1) {
printf("Impossible\n");
} else {
double l = len1 + len2 + len3;
double p = l / 2;
double s = sqrt(p * (p - len1) * (p - len2) * (p - len3));
printf("L = %.2lf, A = %.2lf\n", l, s);
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 KiB

View File

@ -0,0 +1,44 @@
#include <math.h>
#include <stdio.h>
double no_neg_zero(double x) {
return x == -0.0 ? 0.0 : x;
}
int main() {
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0 && b == 0) {
if (c == 0) {
printf("Zero Equation\n");
} else {
printf("Not An Equation\n");
}
} else if (a == 0) {
double x = -c / b;
printf("%.2lf\n", no_neg_zero(x));
} else {
double delta = b * b - 4.0 * a * c;
if (delta > 0) {
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
printf("%.2lf\n%.2lf\n", no_neg_zero(x1), no_neg_zero(x2));
} else if (delta == 0) {
double x = -b / (2 * a);
printf("%.2lf\n", no_neg_zero(x));
} else if (delta < 0) {
double real = -b / (2 * a);
double imag = sqrt(-delta) / (2 * a);
printf("%.2lf+%.2lfi\n%.2lf-%.2lfi\n", no_neg_zero(real), no_neg_zero(imag), no_neg_zero(real), no_neg_zero(imag));
}
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 601 KiB