求水仙花数和完数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求水仙花数和完数相关的知识,希望对你有一定的参考价值。
能不能把过程也写出来,每个步骤的作用
参考技术A 1.5请参照本章例题,编写一个C程序,输出以下信息:************
Very Goodj!
************
解:
main()
printf(" ************ \n");
printf("\n");
printf(" Very Good! \n");
printf("\n");
printf(" ************\n");
1.6编写一个程序,输入a b c三个值,输出其中最大者。
解:main()
int a,b,c,max;
printf("请输入三个数a,b,c:\n");
scanf("%d,%d,%d",&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf("最大数为:%d",max);
第三章
3.3 请将下面各数用八进制数和十六进制数表示:
(1)10 (2)32 (3)75 (4)-617
(5)-111 (6)2483 (7)-28654 (8)21003
解:十 八 十六
(10)=(12)=(a)
(32)=(40)=20
(75)=(113)=4b
(-617)=(176627)=fd97
-111=177621=ff91
2483=4663=963
-28654=110022=9012
21003=51013=520b
3.5字符常量与字符串常量有什么区别?
解:字符常量是一个字符,用单引号括起来。字符串常量是由0个或若干个字符
而成,用双引号把它们括起来,存储时自动在字符串最后加一个结束符号'\0'.
3.6写出以下程序的运行结果:
#include<stdio.h>
void main()
char c1='a',c2='b',c3='c',c4='\101',c5='\116';
printf("a%c b%c\tc%c\tabc\n",c1,c2,c3);
printf("\t\b%c %c\n",c4,c5);
解:程序的运行结果为:
aabb cc abc
A N
3.7将"China"译成密码.密码规律:用原来的字母后面第4个字母代替原来的字母,
例如,字母"A"后面第4个字母是"E",用"E"代替"A".因此,"China"应译为"Glmre".
请编一程序,用赋初值的议程使c1,c2,c3,c4,c5分别变成'G','1','m','r','e',并
输出.
main()
char c1="C",c2="h",c3="i",c4='n',c5='a';
c1+=4;
c2+=4;
c3+=4;
c4+=4;
c5+=4;
printf("密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5);
3.8例3.6能否改成如下:
#include<stdio.h>
void main()
int c1,c2;(原为 char c1,c2)
c1=97;
c2=98;
printf("%c%c\n",c1,c2);
printf("%d%d\n",c1,c2);
解:可以.因为在可输出的字符范围内,用整型和字符型作用相同.
3.9求下面算术表达式的值.
(1)x+a%3*(int)(x+y)%2/4=2.5(x=2.5,a=7,y=4.7)
(2)(float)(a+b)/2+(int)x%(int)y=3.5(设a=2,b=3,x=3.5,y=2.5)
3.10写出下面程序的运行结果:
#include<stdio.h>
void main()
int i,j,m,n;
i=8;
j=10;
m=++i;
n=j++;
printf("%d,%d,%d,%d\n",i,j,m,n);
解:结果: 9,11,9,10
第4章
4.4.a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1='a',c2='b'.想得
到以下的输出格式和结果,请写出程序要求输出的结果如下:
a= 3 b= 4 c= 5
x=1.200000,y=2.400000,z=-3.600000
x+y= 3.60 y+z=-1.20 z+x=-2.40
u= 51274 n= 128765
c1='a' or 97(ASCII)
c2='B' or 98(ASCII)
解:
main()
int a,b,c;
long int u,n;
float x,y,z;
char c1,c2;
a=3;b=4;c=5;
x=1.2;y=2.4;z=-3.6;
u=51274;n=128765;
c1='a';c2='b';
printf("\n");
printf("a=%2d b=%2d c=%2d\n",a,b,c);
printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z);
printf("x+y=%5.2f y=z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x);
printf("u=%6ld n=%9ld\n",u,n);
printf("c1='%c' or %d(ASCII)\n",c1,c2);
printf("c2='%c' or %d(ASCII)\n",c2,c2);
4.5请写出下面程序的输出结果.
结果:
57
5 7
67.856400,-789.123962
67.856400 ,-789.123962
67.86,-789.12,67.856400,-789.123962,67.856400,-789.123962
6.785640e+001,-7.89e+002
A,65,101,41
1234567,4553207,d687
65535,17777,ffff,-1
COMPUTER, COM
4.6用下面的scanf函数输入数据,使a=3,b=7,x=8.5,y=71.82,c1='A',c2='a',
问在键盘上如何输入?
main()
int a,b;
float x,y;
char c1,c2;
scanf("a=%d b=%d,&a,&b);
scanf(" x=%f y=%e",&x,&y);
scanf(" c1=%c c2=%c",&c1,&c2);
解:可按如下方式在键盘上输入:
a=3 b=7
x=8.5 y=71.82
c1=A c2=a
说明:在边疆使用一个或多个scnaf函数时,第一个输入行末尾输入的"回车"被第二
个scanf函数吸收,因此在第二\三个scanf函数的双引号后设一个空格以抵消上行
入的"回车".如果没有这个空格,按上面输入数据会出错,读者目前对此只留有一
初步概念即可,以后再进一步深入理解.
4.7用下面的scanf函数输入数据使a=10,b=20,c1='A',c2='a',x=1.5,y=-3.75,z=57.8,请问
在键盘上如何输入数据?
scanf("%5d%5d%c%c%f%f%*f %f",&a,&b,&c1,&c2,&y,&z);
解:
main()
int a,b;
float x,y,z;
char c1,c2;
scanf("%5d%5d%c%c%f%f",&a,&b,&c1,&c2,&x,&y,&z);
运行时输入:
10 20Aa1.5 -3.75 +1.5,67.8
注解:按%5d格式的要求输入a与b时,要先键入三个空格,而后再打入10与20。%*f是用来禁止赋值的。在输入时,对应于%*f的地方,随意打入了一个数1.5,该值不会赋给任何变量。
3.8设圆半径r=1.5,圆柱高h=3,求圆周长,圆面积,圆球表面积,圆球体积,圆柱体积,用scanf输入数据,输出计算结果,输出时要求有文字说明,取小数点后两位数字.请编程.
解:main()
float pi,h,r,l,s,sq,vq,vz;
pi=3.1415926;
printf("请输入圆半径r圆柱高h:\n");
scanf("%f,%f",&r,&h);
l=2*pi*r;
s=r*r*pi;
sq=4*pi*r*r;
vq=4.0/3.0*pi*r*r*r;
vz=pi*r*r*h;
printf("圆周长为: =%6.2f\n",l);
printf("圆面积为: =%6.2f\n",s);
printf("圆球表面积为: =%6.2f\n",sq);
printf("圆球体积为: =%6.2f\n",vz);
4.9输入一个华氏温度,要求输出摄氏温度,公式为C=5/9(F-32),输出要有文字说明,取两位小数.
解: main()
float c,f;
printf("请输入一个华氏温度:\n");
scanf("%f",&f);
c=(5.0/9.0)*(f-32);
printf("摄氏温度为:%5.2f\n",c);
第五章 逻辑运算和判断选取结构
5.4有三个整数a,b,c,由键盘输入,输出其中最大的数.
main()
int a,b,c;
printf("请输入三个数:");
scanf("%d,%d,%d",&a,&b,&c);
if(a<b)
if(b<c)
printf("max=%d\n",c);
else
printf("max=%d\n",b);
else if(a<c)
printf("max=%d\n",c);
else
printf("max-%d\n",a);
方法2:使用条件表达式.
main()
int a,b,c,termp,max;
printf(" 请输入 A,B,C: ");
scanf("%d,%d,%d",&a,&b,&c);
printf("A=%d,B=%d,C=%d\n",a,b,c);
temp=(a>b)?a:b;
max=(temp>c)? temp:c;
printf(" A,B,C中最大数是%d,",max);
5.5 main()
int x,y;
printf("输入x:");
scanf("%d",&x);
if(x<1)
y=x;
printf("X-%d,Y=X=%d \n",x,y);
else if(x<10)
y=2*x-1;
printf(" X=%d, Y=2*X-1=%d\n",x,y);
else
y=3*x-11;
printf("X=5d, Y=3*x-11=%d \n",x,y);
5.7给一个不多于5位的正整数,要求:1.求它是几位数2.分别打印出每一位数字3.按逆序打印出各位数字.例如原数为321,应输出123.
main()
long int num;
int indiv,ten,hundred,housand,tenthousand,place;
printf("请输入一个整数(0-99999):");
scanf("%ld",&num);
if(num>9999)
place=5;
else if(num>999)
place=4;
else if(num>99)
place=3;
else if(num>9)
place=2;
else place=1;
printf("place=%d\n",place);
printf("每位数字为:");
ten_thousand=num/10000;
thousand=(num-tenthousand*10000)/1000;
hundred=(num-tenthousand*10000-thousand*1000)/100;
ten=(num-tenthousand*10000-thousand*1000-hundred*100)/10;
indiv=num-tenthousand*10000-thousand*1000-hundred*100-ten*10;
switch(place)
case 5:printf("%d,%d,%d,%d,%d",tenthousand,thousand,hundred,ten,indiv);
printf("\n反序数字为:");
printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,tenthousand);
break;
case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
printf("\n反序数字为:");
printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d\n",hundred,ten,indiv);
printf("\n反序数字为:");
printf("%d%d%d\n",indiv,ten,hundred);
case 2:printf("%d,%d\n",ten,indiv);
printf("\n反序数字为:");
printf("%d%d\n",indiv,ten);
case 1:printf("%d\n",indiv);
printf("\n反序数字为:");
printf("%d\n",indiv);
5.8 1.if语句
main()
long i;
float bonus,bon1,bon2,bon4,bon6,bon10;
/*初始化变量*/
bon1=100000*0.1;
bon2=100000*0.075+bon1;
bon4=200000*0.05+bon2;
bon6=200000*0.03+bon4;
bon10=400000*0.015+bon6;
printf("请输入利润");
scanf("%ld",&i);
/*计算*/
if(i<=le5)
bonus=i*0.1;
else if(i<2e5)
bonus=bon1+(1-100000)*0.075;
else if(i<=4e5)
bonus=bon2+(i-200000)*0.05;
else if(i<=6e5)
bonus=bon4+(i-400000)*0.03;
else if(i<=le6)
bonus=bon6+(i-600000)*0.015;
else
bonus=bon10+(i-1000000)*0.01;
printf("奖金是 %10.2f",bonus);
用switch语句编程序
main()
long i;
float bonus,bon1,bon2,bon4,bon6,bon10;
int branch;
/*初始化变量*/
bon1=100000*0.1;
bon2=bon1+100000*0.075
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
printf("请输入利润:");
scanf("%ld",&i);
branch=i/100000;
if(branch>10)
branch=10;
/*计算*/
switch(branch)
case 0:bonus=i*0.1;break;
case 1:bonus=bon1+(i-100000)*0.075;break;
case 2:
case 3:bonus=bon2+(i-200000)*0.05;break;
case 4:
case 5:bonus=bon4+(i-400000)*0.03;break;
case 6:
case 7:
case 8:
case 9:bonus=bon6+(i-600000)*0.015;break;
case 10:bonus=bon10+(i-1000000)*0.01;
printf(" 奖金是 %10.2f",bonus);
5.9 输入四个整数,按大小顺序输出.
main()
int t,a,b,c,d;
printf("请输入四个数:");
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
printf("\n\n a=%d,b=%d,c=%d,d=%d \n",a,b,c,d);
if(a>b)
t=a;a=b;b=t;
if(a>c)
t=a;a=c;c=t;
if(a>d)
t=a;a=d;d=t;
if(b>c)
t=b;b=c;c=t;
if(b>d)
t=b;b=d;d=t;
if(c>d)
t=c;c=d;d=t;
printf("\n 排序结果如下: \n");
printf(" %d %d %d %d \n",a,b,c,d);
5.10塔
main()
int h=10;
float x,y,x0=2,y0=2,d1,d2,d3,d4;
printf("请输入一个点(x,y):");
scanf("%f,%f",&x,&y);
d1=(x-x0)*(x-x0)+(y-y0)(y-y0);
d2=(x-x0)*(x-x0)+(y+y0)(y+y0);
d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);
d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);
if(d1>1 && d2>1 && d3>1 && d4>1)
h=0;
printf("该点高度为%d",h);
第六章 循环语句
6.1输入两个正数,求最大公约数最小公倍数.
main()
int a,b,num1,num2,temp;
printf("请输入两个正整数:\n");
scanf("%d,%d",&num1,&num2);
if(num1<num2)
temp=num1;
num1=num2;
num2=temp;
a=num1,b=num2;
while(b!=0)
temp=a%b;
a=b;
b=temp;
printf("它们的最大公约数为:%d\n",a);
printf("它们的最小公倍数为:%d\n",num1*num2/2);
6.2输入一行字符,分别统计出其中英文字母,空格,数字和其它字符的个数.
解:
#include <stdio.h>
main()
char c;
int letters=0,space=0,degit=0,other=0;
printf("请输入一行字符:\n");
while((c=getchar())!='\n')
if(c>='a'&&c<='z'||c>'A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
other++;
printf("其中:字母数=%d 空格数=%d 数字数=%d 其它字符数=%d\n",letters,space,
digit,other);
6.3求s(n)=a+aa+aaa+…+aa…a之值,其中工是一个数字.
解:
main()
int a,n,count=1,sn=0,tn=0;
printf("请输入a和n的值:\n");
scanf("%d,%d",&a,&n);
printf("a=%d n=%d \n",a,n);
while(count<=n)
tn=tn+a;
sn=sn+tn;
a=a*10;
++count;
printf("a+aa+aaa+…=%d\n",sn);
6.4 求1+2!+3!+4!+…+20!.
main()
float n,s=0,t=1;
for(n=1;n<=20;n++)
t=t*n;
s=s+t;
printf("1!+2!+…+20!=%e\n",s);
6.5 main()
int N1=100,N2=50,N3=10;
float k;
float s1=0,s2=0,s3=0;
for(k=1;k<=N1;k++)/*计算1到100的和*/
s1=s1+k;
for(k=1;k<=N2;k++)/*计算1到50各数平方和*/
s2=s2+k*k;
for(k=1;k<=N3;k++)
s3=s3+1/k;
printf("总和=%8.2f\n",s1+s2+s3);
6.6水仙开花
main()
int i,j,k,n;
printf(" '水仙花'数是:");
for(n=100;n<1000;n++)
i=n/100;
j=n/10-i*10;
k=n%10;
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
printf("%d",n);
printf("\n");
6.7完数
main()
#include M 1000/*定义寻找范围*/
main()
int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;
int i,j,n,s;
for(j=2;j<=M;j++)
n=0;
s=j;
for(i=1;i<j;i++)
if((j%i)==0)
if((j%i)==0)
n++;
s=s-i;
switch(n)/*将每个因子赋给k0,k1…k9*/
case 1:
k0=i;
break;
case 2:
k1=i;
break;
case 3:
k2=i;
break;
case 4:
k3=i;
break;
case 5:
k4=i;
break;
case 6:
k5=i;
break;
case 7:
k6=i;
break;
case 8:
k7=i;
break;
case 9:
k8=i;
break;
case 10:
k9=i;
break;
if(s==0)
printf("%d是一个‘完数’,它的因子是",j);
if(n>1)
如何用VB语言求水仙花数和求1~1000以内的素数
我和朋友一起发现了一个更简单的方法求1~1000以内的素数之和,VB代码如下:Private Sub Command1_Click()
Dim i, s, n, c As Double
i = 3: s = 0
Do While i <= 1000
n = 1: c = 0
Do While n <= i
If i Mod n = 0 Then
c = c + 1
End If
n = n + 1
Loop
If c = 2 Then
s = s + i
End If
i = i + 2
Loop
s = s + 2
Print s
End Sub
运行结果是对的,就是76127 参考技术A Private Sub Form1_Click()Dim a As Integer, b As Integer, c As IntegerFor a = 1 To 9For b = 0 To 9For c = 0 To 9If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c ThenForm1.Print a * 100 + b * 10 + cEnd IfNext cNext bNext aEnd 参考技术B '求水仙花数
Private Sub Form1_Click()
Dim a As Integer, b As Integer, c As Integer
For a = 1 To 9
For b = 0 To 9
For c = 0 To 9
If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c Then
Form1.Print a * 100 + b * 10 + c
End If
Next c
Next b
Next a
End
'求1~1000以内的素数,
Private Sub Command1_Click()
Dim a As Integer, b As Integer
Text1 = ""
For a = 3 To 999 step 2
For b = 2 To Sqr(a)
If a Mod b = 0 Then
Exit For
End If
If b > Sqr(a) Then
Text1 = Text1 & a & VbNewline
End If
Next b
Next a
End追问
Text1 = ""是什么?
追答Text1是一个多行文本框,上面那一句是先清空其中的内容(以防多次运行结果太乱)。
以上是关于求水仙花数和完数的主要内容,如果未能解决你的问题,请参考以下文章