C语言作业(一)
Wucheng

放一点最近老师最近布置的作业


2/1+3/2+5/3+……n的前30项和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
int main(void)
{
double num,sum,a,b;
a = 2, b = 1,sum = 0;
for(int c = 0; c < 30;c ++)
{
num = a / b;//
sum = sum +num;//求和
a = a + b;//(1)a=3,b=1 (2)a=5.b=2
b = a - b;//(1)a=3,b=2 (2)a=5,b=3

}
printf("%f",sum);
}


输入数字后输出位数,分别输出单个数字,倒序输出(仅可使用加减乘除)

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
52
#include <stdio.h>
int main()
{
int num,size;

printf("请输入一个小于10^5的数:");
scanf("%d",&num);

printf("数字位数为:%d\n",num_size(num));

printf("数字分别为:");
part(num);
printf("\n");

printf("倒序输出为:%d\n",contrary(num));

}

int num_size(int x) //求数字的个数
{
int size;
size = 0;
while (x)
{
x /= 10;
size++;
}
return size;
}

int part(int x) //分别输出数字
{
int n = 1;
int put;

for(int num = num_size(x);num > 1;num--) n *= 10; //用于获取数字应该除以100还是1000还是10000

for(;n > 0;n /= 10)
{
put = x / n;
x %= n;
printf("%d ",put);//分别输出数字
}
}

int contrary(int x) //反向输出数字
{
int y;
for(y = 0; x; x /= 10)
y = y * 10 + x % 10;
return y;
}

输入一个数,输出最大公约数和最小公倍数

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
#include <stdio.h>
int main(void)
{
int m,n;
int num,min,max;

printf("请输入m和n:");

scanf("%d %d",&m,&n);

min = m < n ? m : n;
max = m > n? m : n;

for(num = min; num > 0; num--)
{
if(n % num == 0 && m % num == 0)
{
printf("最大公约数为%d\n",num);
break;
}

}

for(num = max; num <= m * n; num++)
{
if(num % m == 0 && num % n == 0)
{
printf("最大公倍数为%d",num);
break;
}
}

}

输入字符串,输出字符串中多少空格,多少数字,多少字母,多少其他字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
int main()
{
char a;
int letter,space,other,num;
letter = 0; space = 0; other = 0, num = 0;

printf("请输入字符串:");

while ((a = getchar()) != '\n')
{
if(a >= 65 && a <= 122) letter++;

else if(a >=48 && a <= 57) num++;

else if(a == ' ') space++;

else other++;
}

printf("该字符串字母有%d个字母\n数字有%d个\n空格有%d个\n其他字符有%d个\n",letter,num,space,other);

}

输出水仙花数(各位数数字的立方和为本身的三位数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int main(void)
{
int num,num_1,num_2,num_3;

for (size_t i = 100; i <= 999; i++)
{
num = i;
num_3 = num % 10;
num /= 10;

num_2 = num % 10;
num /= 10;

num_1 = num;

if(num_1*num_1*num_1 + num_2*num_2*num_2 + num_3*num_3*num_3 == i)
printf("%d是水仙花数\n",i);
}

}

输出1000以内的完数(数字的因子数的和为本身的数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main(void)
{
int num;
num = 0;

for (size_t m = 1; m <= 1000; m++)
{
for (size_t n = 1; n < m; n++)
{
if (m % n == 0) num = num + n;
}

if (num == m) printf("%d是完数\n",m);
num = 0;
}

}

用*打印三角形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main(void)
{
int a;

for (size_t i = 0; i <= 10; i += 2)
{
for(size_t x = 5; x > i / 2; x--)
printf(" ");

for(a = 0; a <= i ;a ++)
printf("* ");

printf("\n");
}

}

效果是这样的

1
2
3
4
5
6
          *
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *