第七周必做题。
This commit is contained in:
37
07/Exercise01.c
Normal file
37
07/Exercise01.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
double f(double x);
|
||||
double S(double a, double b, int m);
|
||||
|
||||
double f(double x) {
|
||||
return pow(exp(1.0), -x*x);
|
||||
}
|
||||
|
||||
double S(double a, double b, int m) {
|
||||
double step = (b-a) / m;
|
||||
double ans = 0;
|
||||
int i = 0;
|
||||
for (i = 0; i < m; i++) {
|
||||
ans += step * (f(a + step * i) + f(a + step * (i + 1))) / 2;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main() {
|
||||
double a, b;
|
||||
int m;
|
||||
double bestResult;
|
||||
double lowerBond, upperBond;
|
||||
bestResult = S(-1, 1, 2000);
|
||||
printf("The best result we get is %.10lf\n", bestResult);
|
||||
lowerBond = ((int)(bestResult * 10000)) / 10000.0;
|
||||
upperBond = lowerBond + 0.0001;
|
||||
for (m = 1; m <= 2000; m++) {
|
||||
if (S(-1, 1, m) >= lowerBond && S(-1,1,m) < upperBond) {
|
||||
printf("Now the first m that satisfies the requirment is %d, the result is %.10lf.", m, S(-1,1,m));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
14
07/Exercise02.c
Normal file
14
07/Exercise02.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int old, stand, lie;
|
||||
for (old = 3; old < 97; old+=3) {
|
||||
for (stand = 1; stand < 100 - old; stand++) {
|
||||
lie = 100 - stand - old;
|
||||
if ((lie*3 + stand*5 + old/3) == 100) {
|
||||
printf("Stand: %d Lie %d Old: %d\n", stand, lie, old);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user