- 69
- 0
- 约8.89万字
- 约 156页
- 2021-02-21 发布于天津
- 举报
精品文档
精品文档
随意编辑
随意编辑
精品文档
精品文档
随意编辑
随意编辑
1.16
void print_descending(int x,int y,int z)// 按从大到小顺序输出三个数
{
scanf(%d,%d,%d,x,y,z);
if(xy) x-y; //- 为表示交换的双目运算符 ,以下同
if(yz) y-z;
if(xy) x-y; // 冒泡排序
printf(%d %d %d,x,y,z);
}//print_descending
1.17
Status fib(int k,int m,int f)// 求 k 阶斐波那契序列的第 m 项的值 f
{
int tempd;
if(k2||m0) return ERROR;
if(mk-1) f=0;
else if (m==k-1) f=1;
else
{
for(i=0;i=k-2;i++) temp=0;
temp[k-1]=1; // 初始化
for(i=k;i=m;i++) // 求出序列第 k 至第 m 个元素的值
{
sum=0;
for(j=i-k;ji;j++) sum+=temp[j];
temp=sum;
}
f=temp[m];
}
return OK;
}//fib
分析:通过保存已经计算出来的结果 ,此方法的时间复杂度仅为 0(m^2).如果采用递归编程(大
多数人都会首先想到递归方法),则时间复杂度将高达 0(3m).
1.18
typedef struct{
char *sport;
enum{male,female} gender;
char school name; // 校名为A,B,C,D或E
char *result;
int score;
} resulttype;
typedef struct{
int malescore;
int femalescore;
int totalscore;
} scoretype;
result[ ]void summary(resulttype result[ ])// 求各校的男女总分和团体总分 ,假设结果已经储存在 数组中
result[ ]
{
scoretype score;
i=0;
while(result.sport!=NULL)
{
switch(result.schoolname)
{
case A:
score[ 0 ].totalscore+=result.score;
if(result.gender==0) score[ 0 ].malescore+=result.score;
else score[ 0 ].femalescore+=result.score;
break;
case B:
score.totalscore+=result.score;
if(result.gender==0) score.malescore+=result.score;
else score.femalescore+=result.score;
break;
}
i++;
}
for(i=0;i5;i++)
{
printf(School %d:\n,i);
printf(Total score of male:%d\n,score.malescore);
printf(Total score of female:%d\n,score.femalescore);
printf(Total score of all:%d\n\n,score.totalscore);
}
}//summary
1.19
Status algo119(int a[ARRSIZE])〃 求 i!*2Ai 序列的值且不超过 maxint
{
last=1;
for(i=1;i=ARRSIZE;i++)
{
a[i-1]=last*2*i;
if((a[i-1]/last)!=(2*i)) reurn OVERFLOW;
last=a[i-1];
return OK;
}
}//algo119
分析:当某一项的结果超过了 maxint 时,它除以前面一项的商会发生异常
1.20 void polyvalue()
{
float ad;
float *p=a;
printf(Input number of terms:); scanf(%d,n);
printf(Input the %d coefficients from a0 to a%d:\n,n,n); for(i=0;i=n;i++) scanf(%f,p++);
printf(Input value of x:);
scanf(%f,x);
p=a;xp=1;sum=0; //xp 用于存放 x 的 i 次方 for(i=
原创力文档

文档评论(0)