-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2.c
51 lines (47 loc) · 810 Bytes
/
Q2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Lab01-2 圈荐 钦 备窍扁
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int tmp = 0;
int count = 1;
int oddsum1(int n){
int sum = 0;
for (int i = 1; i < n + 1; i += 2) {
sum += i;
}
return sum;
}
int oddsum2(int n){
int sum = 0;
int i = 1;
while (i < n+1) {
for (int i = 1; i < n + 1; i += 2) {
sum += i;
}
break;
}
return sum;
}
int oddsum3(int n){
if (count < n+1) {
if (count % 2 != 0) {
tmp += count;
}
count++;
oddsum3(n);
}
return tmp;
}
void main()
{
int n = 100;
int SUM = 0;
int i;
printf("(1) for oddsum1(%d) = %d\n", n, oddsum1(n));
printf("(2) while oddsum2(%d) = %d\n", n, oddsum2(n));
printf("(3) 犁蓖 oddsum3(%d) = %d\n", n, oddsum3(n));
// SUM 阑 备茄促.
for (i = 1; i < 100; i += 2) {
SUM += oddsum1(i);
}
printf("(4) SUM = %d\n", SUM);
}