7-1 输出素数之和
#include <stdio.h>
int main()
{
int num,i,n,sum;
scanf("%d",&num);
n=1;
sum=0;
while(num>0&&n<=10)
{
for(i=2;i<num;i++)
{
if(num%i==0)break;
}
if(i==num)
{
printf("%d ",num);
n++;
sum=sum+num;
}
num--;
}
printf("\nsum = %d",sum);
return 0;
}
7-2 求e的值
#include <stdio.h>
int main()
{
double e=0,c,n=1,sum=1;
scanf("%lf",&c);
while(1.0/sum>c)
{
e=1.0/sum+e;
sum=sum*n;//计算n的阶层
n++;
}
printf("e=%lf",e);
return 0;
}
7-3 打印菱形图案
#include <stdio.h>
int main()
{
int n,i,j,k=1;
scanf("%d",&n);
if(n%2==0)
{
printf("error");
scanf("%d",&n);
}
n=n/2;
while(n>0)
{
for(j=1;j<=n;j++)
{
printf(" ");
if(j==n)
{
for(i=1;i<=2*k-1;i++)
{
printf("* ");
}
printf("\n");
}
}
n--;
k++;
}
for(i=1;i<=2*k-1;i++)
{
printf("* ");
}
k--;
n++;
printf("\n");
while(k>0)
{
for(j=1;j<=n;j++)
{
printf(" ");
if(j==n)
{
for(i=1;i<=2*k-1;i++)
{
printf("* ");
}
printf("\n");
}
}
k--;
n++;
}
return 0;
}
7-4 换硬币
#include <stdio.h>
int main()
{
int a,b=1,c=1,total,sum,count=0;//a为五分,b为二分,c为一分,total为硬币数量
scanf("%d",&sum);//输入钱的数量
if(sum<=8||sum>=100)
{
scanf("%d",&sum);
}
a=sum/5;
while(a>0)//确定五分的数量
{
b=sum/2;
while(a*5+b*2+c>=sum)//确定两分的数量
{
while(b>0)
{
c=1;
while(a*5+b*2+c<=sum)//确定一分的数量
{
if(a*5+b*2+c==sum)
{
total=a+b+c;
printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n",a,b,c,total);
count++;
}
c++;
}
b--;
}
b--;
}
a--;
}
printf("count = %d\n",count);
return 0;
}