c语言容易题

来源:百度知道 编辑:UC知道 时间:2024/06/17 12:29:34
四、编写程序
【4.1】打印出所有的“水仙花数”。水仙花数是指一个三位数,其各位数字立方和等于该数本身。例如:153=13+53+33
【4.2】输出20到200之间的素数及这些素数的和,每行输出6个数。
【4.3】从键盘输入变量h,例如,当h=4时,输出的图形如下。

*
***
*****
*******
*****
***
*

4.1
#include <stdio.h>
#include <math.h>
main()
{
int n,a[3];
for(n=100;n<1000;n++)
{
a[0]=n%10;
a[1]=n/10%10;
a[2]=n/100%10;
if(n==pow(a[0],3)+pow(a[1],3)+pow(a[2],3))
printf("%d ",n);
}
printf("\n");
}
-------------------------------------------------
4.2
#include <stdio.h>
#include <math.h>
main()
{
int m,n,i,sum=0,count=0;
for(n=20;n<=200;n++)
{
m=(int)sqrt(n);
for(i=2;i<m;i++)
if(n%i==0)
break;
if(i>=m)
{
sum+=n;
printf("%4d",n);
count++;
if(count%6==0)
printf("\n");
}
}
printf("\n");
}
------------------------------------------
4.3
因为百度的问题,不知道这个图形是菱形呢,还是三角形
下面的是菱形:
#include <stdio.h>
#include <math