编写程序实现算法

编写程序实现算法,第1张

#include <stdio.h>

#define false 0

#define true 1

#define maxsize 20typedef struct{

int r[maxsize+1] //r[0]闲置或用作中转、哨兵单元

int length

}sqlistvoid Output(sqlist &L)

{

int i

for(i=1i<=L.lengthi++)

{

printf("%d ",L.r[i])

}

printf("\n")

}void BubbleSort(sqlist &L) {

int i,j, Exchange

for (i=1i<L.lengthi++){

Exchange = false

for (j = 1 j <= L.length-i j++)

if (L.r[j+1] <L.r[j]) {

L.r[0] =L.r[j]

L.r[j] =L.r[j+1]

L.r[j+1] =L.r[0]

Output(L)

Exchange = true

}

if (Exchange == false) return

}

} // BubbleSortint Partition (sqlist &L, int low, int high)

{

int pivotkey

L.r[0]=L.r[low]

pivotkey=L.r[low]

while(low<high)

{ while(low<high&&L.r[high]>=pivotkey)--high

L.r[low]=L.r[high]

while(low<high&&L.r[low]<=pivotkey)++low

L.r[high]=L.r[low]

}

L.r[low]=L.r[0]

return low

}

void QSort (sqlist &L,int low,int high)

{

int pivotloc

if (low<high)

{pivotloc=Partition(L,low,high)Output(L) QSort(L,low,pivotloc-1) QSort(L,pivotloc+1,high) }

}

int main()

{

sqlist L

int i

scanf("%d",&L.length)

for(i=1i<=L.lengthi++)

{

scanf("%d",&L.r[i])

}

printf("BubbleSort:\n")

BubbleSort(L)

printf("QSort:\n")

QSort(L,1,L.length)

return 0

}

#include<stdio.h>

void main(){

int i,j,score[10],count=0,temp,sum=0

double avg

for(i=0i<10i++){ //输入10个学生成绩,并求着10个学生的成绩总和

printf("请输入第%d个学生的成绩:",(i+1))

scanf("%d",&score[i])

sum+=score[i]

}

avg=sum*1.0/10 //求着这10个学生成绩的平均

for(i=0i<10i++){ //统计小于平均分的学生人数

if(score[i]<avg){

count++

}

}

for(i=0i<10i++){ //使用冒泡排序对这10个学生的成绩逆序排序

for(j=0j<9-ij++){

if(score[j]<score[j+1]){

temp=score[j]

score[j]=score[j+1]

score[j+1]=temp

}

}

}

printf("最高成绩:%d分,平均成绩:%.2f分,低于平均成绩的人数是:%d人!\n",score[0],avg,count)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11736443.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存