习题一和上机答案Word格式.doc
- 文档编号:14176135
- 上传时间:2022-10-19
- 格式:DOC
- 页数:4
- 大小:38KB
习题一和上机答案Word格式.doc
《习题一和上机答案Word格式.doc》由会员分享,可在线阅读,更多相关《习题一和上机答案Word格式.doc(4页珍藏版)》请在冰豆网上搜索。
%d%d%d"
x,y,z);
}//print_descending
1.2试编写算法,计算i!
*2i的值并存入数组a[size]的第i-1个分量中(i=1,2,….n)。
假设计算机中允许的整数最大值为max。
Statusalgo119(inta[ARRSIZE])//求i!
*2^i序列的值且不超过max
last=1;
for(i=1;
i<
=ARRSIZE;
i++)
a[i-1]=last*2*i;
if((a[i-1]/last)!
=(2*i))reurnOVERFLOW;
last=a[i-1];
returnOK;
}
}//algo119
分析:
当某一项的结果超过了max时,它除以前面一项的商会发生异常.
1.3设n为正整数。
试确定下列各程序段中前置以记号#的语句的频度(次数)和时间复杂度。
(1)i=1;
k=0;
While(i<
=n-1){
#k+=10*i;
i++;
答:
n-1
(2)i=1;
do{
}While(i<
=n-1)
(3)i=1;
i++;
}
(4)k=0;
for(i=1;
i<
=n;
i++){
for(j=i;
j<
j++)
#k++;
}
(n+1)*n/2
(5)i=1;
j=0;
while(i+j<
=n){
#if(i>
j)j++;
elsei++;
n
(6)for(i=1;
i<
i++)
for(j=1;
j<
=i;
j++)
for(k=1;
k<
=j;
k++)
#x++;
1/6*n*(n+1)*(n+2)
第一章上机内容
1.1、输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。
写3个自定义函数:
输入10个数、进行处理、输出10个数。
#defineN10
inta[N];
voidinput(inta[])
inti;
for(i=0;
N;
scanf("
%d"
a[i]);
voidoutput(inta[])
printf("
%3d"
a[i]);
voiddeal(inta[])
inti,min,pmin,max,pmax;
min=max=a[0];
pmin=pmax=0;
{
if(a[i]<
min){min=a[i];
pmin=i;
if(a[i]>
max){max=a[i];
pmax=i;
}
a[pmin]=a[0];
a[0]=min;
a[pmax]=a[N-1];
a[N-1]=max;
main()
input(a);
deal(a);
output(a);
getch();
1.2、设计一个可进行复数运算的演示程序。
要求:
实现下列六种基本运算:
1)由输入的实部和虚部生成一个复数;
2)两个复数求和;
3)两个复数求差;
4)两个复数求积;
5)从已知复数中分离出实部;
6)从已知复数中分离出虚部。
运算结果以相应的复数或实数的表示形式显示。
#include"
stdio.h"
voidtecreat(structfu*t);
voidinput(structfu*t);
structfu{
longshi;
longxu;
};
structfus[3],*t=s;
charc;
do{
input(t);
tecreat(t);
printf("
Doyouwanttocontinue?
(y/n)"
);
getchar();
c=getchar();
}while(c=='
y'
getch();
return0;
voidtecreat(structfu*t)
intc;
printf("
Changethekindoftecreat\n"
1=generate2=+3=-4=*5=decathshi6=decathxu\n"
scanf("
c);
switch(c)
{
case1:
(t+2)->
shi=t->
shi;
xu=t->
xu;
break;
case2:
shi+(t+1)->
(t+2)->
xu+(t+1)->
break;
case3:
shi-(t+1)->
xu-(t+1)->
break;
case4:
shi*(t+1)->
shi-t->
xu*(t+1)->
(t+2)->
xu+t->
break;
case5:
break;
case6:
}
if((t+2)->
xu>
0)printf("
Itis%ld+%ldi.\n"
(t+2)->
shi,(t+2)->
xu);
elseprintf("
Itis%ld%ldi.\n"
voidinput(structfu*t)
Inputtwofushu(form:
xy)\n"
Inputfirstone:
"
%ld%ld"
t->
shi,&
Inputsecondone:
(t+1)->
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 习题 上机 答案