打印杨辉三角

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
#include <stdio.h>
#include <stdlib.h>
int main()
{
int line ;
int i,j;
scanf("%d",&line);
for (i = 0; i<line; i++)//打印第一部分
{
for (j = 0; j<line - i - 1; j++)
{
printf(" ");
}
for (j = 0; j<2 * i + 1; j++)
{
printf("*");
}
printf("\n");
}
for (i = line-1; i>0; i--)//打印第二部分
{
for (j = 0; j<line-i; j++)
{
printf(" ");
}
for (j = 0; j<2*i-1; j++)
{
printf("*");
}
printf("\n");
}
system("pause");
return 0;
}

打印0~999的水仙花数

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
/*
在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一N位数,其各个数之N次方和等于该数。
例如153、370、371及407就是三位数的水仙花数,其各个数之立方和等于该数:
153 = 1^3 + 5^3 + 3^3。
370 = 3^3 + 7^3 + 0^3。
371 = 3^3 + 7^3 + 1^3。
407 = 4^3 + 0^3 + 7^3。
*/
#include <stdio.h>
int main()
{
int shu;
int first_shu;
int second_shu;
int there_shu;
for (shu = 101; shu<999; shu++)
{
first_shu = shu / 100;
second_shu = shu % 100 / 10;
there_shu = shu % 10 / 1;
if (first_shu*first_shu*first_shu + second_shu*second_shu*second_shu + there_shu*there_shu*there_shu == shu)
{
printf("%d ", shu);
}
}
system("pause");
return 0;
}

求和2+22+222+2222+22222

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*3. 
求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,
例如:2+22+222+2222+22222
*/
#include <stdio.h>

int main()
{
int a;
int i;
sum = 0;
for(i=0;i<5;i++)
{
sum = sum*10+a;
sum1 +=sum;
}
system("pause");
return 0;
}