mirror of
https://github.com/renbaoshuo/202401-programming-assignments.git
synced 2024-11-08 11:08:41 +00:00
【实践课内】6.循环结构3
This commit is contained in:
parent
c1c14e733f
commit
889f44af50
20
【实践课内】6.循环结构3/7-1 求简单交错序列前N项和.c
Normal file
20
【实践课内】6.循环结构3/7-1 求简单交错序列前N项和.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
double ans = 0;
|
||||
|
||||
scanf("%d", &n);
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (i % 2 == 0) {
|
||||
ans += 1.0 / (1 + 3 * i);
|
||||
} else {
|
||||
ans -= 1.0 / (1 + 3 * i);
|
||||
}
|
||||
}
|
||||
|
||||
printf("sum = %.3lf\n", ans);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
【实践课内】6.循环结构3/7-1 求简单交错序列前N项和.jpg
Normal file
BIN
【实践课内】6.循环结构3/7-1 求简单交错序列前N项和.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 282 KiB |
22
【实践课内】6.循环结构3/7-2 21循环-求和2.c
Normal file
22
【实践课内】6.循环结构3/7-2 21循环-求和2.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
long long n, ans = 0;
|
||||
|
||||
scanf("%lld", &n);
|
||||
|
||||
for (long long i = 1; i <= (long long)sqrt(n); i++) {
|
||||
if (n % i == 0) {
|
||||
ans += i;
|
||||
|
||||
if (i != n / i) {
|
||||
ans += n / i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("%lld\n", ans);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
【实践课内】6.循环结构3/7-2 21循环-求和2.jpg
Normal file
BIN
【实践课内】6.循环结构3/7-2 21循环-求和2.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 291 KiB |
22
【实践课内】6.循环结构3/7-3 个位数.c
Normal file
22
【实践课内】6.循环结构3/7-3 个位数.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
|
||||
scanf("%d", &n);
|
||||
|
||||
while (n >= 10) {
|
||||
int s = 0;
|
||||
|
||||
while (n) {
|
||||
s += n % 10;
|
||||
n /= 10;
|
||||
}
|
||||
|
||||
n = s;
|
||||
}
|
||||
|
||||
printf("%d\n", n);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
【实践课内】6.循环结构3/7-3 个位数.jpg
Normal file
BIN
【实践课内】6.循环结构3/7-3 个位数.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 306 KiB |
34
【实践课内】6.循环结构3/7-4 循环-分数矩阵.c
Normal file
34
【实践课内】6.循环结构3/7-4 循环-分数矩阵.c
Normal file
@ -0,0 +1,34 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
|
||||
while (scanf("%d", &n) != EOF, n != 0) {
|
||||
double ans = n;
|
||||
|
||||
for (int i = 2; i <= n; i++) {
|
||||
ans += (double)(n * 2 - 2 * (i - 1)) / (double)i;
|
||||
}
|
||||
|
||||
/*
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// 对角线左侧
|
||||
for (int j = i; j >= 2; j--) {
|
||||
ans += 1.0 / j;
|
||||
}
|
||||
|
||||
ans += 1;
|
||||
|
||||
// 对角线右侧
|
||||
for (int j = 2; j <= n - i + 1; j++) {
|
||||
ans += 1.0 / j;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
printf("%.2lf\n", ans);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
【实践课内】6.循环结构3/7-4 循环-分数矩阵.jpg
Normal file
BIN
【实践课内】6.循环结构3/7-4 循环-分数矩阵.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 302 KiB |
Loading…
Reference in New Issue
Block a user