| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 496 人关注过本帖
标题:请叫下,下面这个程序,怎样用冒泡排序法排列,就差最后一步了
只看楼主 加入收藏
紫禁
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2011-12-7
结帖率:100%
收藏
 问题点数:0 回复次数:1 
请叫下,下面这个程序,怎样用冒泡排序法排列,就差最后一步了
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>


#define M 5
#define N 5

void input(int scores[][3]);
void cal(int scores[][3], double avgR[], double avgC[]);
void sort(double avgR[], int idx[]);
void printstring(char *s, int n); 

int main(void)
{
    char *ke[N] = {"yuwen","shuxue","yingyu","pingjun","zongfen"};
    char *names[M] = {"qin", "wu", "yu", "sa", "er"};
    int scores[M][3] = {0}, idx[M] = {0}, i, j;
    double avgR[M] = {0}, avgC[3] = {0};
    input(scores);
    cal(scores, avgR, avgC);
    sort(avgR, idx);
    for(j=0;j<N;j++)
    {
        if(j==0)
            printstring(" ",8);
        printstring(ke[j],8);

 
    }
    printf("\n");
    for (i = 0; i < M; i++)
    {
        printstring(names[i],8);
        for (j = 0; j < 3; j++)
        {
            printf("%3d     ", scores[i][j]);
        }
        printf("%3.1lf    ", avgR[i]);
        printf("%3.0f     \n", avgR[i]*3);
    }
    getchar();
    return 0;
}

void input(int score[][3])
{
    int i, j;
    srand((unsigned) time(NULL));
    for (i = 0; i < M; i++)
        for (j = 0; j < 3; j++)
            score[i][j] = 40 + rand() % 61;
    return;
}
void cal(int scores[][3], double avgR[], double avgC[])
{
    int i, j;
    for (i = 0; i < M; i++)
    {
        for (j = 0; j < 3; j++)
        {
            avgR[i] += scores[i][j];
            avgC[j] += scores[i][j];
        }
        avgR[i] /= 3;
    }
    for (j = 0; j < 3; j++)
        avgC[j] /= M;
    return;
}
void sort(double avgR[], int idx[])
{
    int i, j,temp;
    for (i = 0; i < M; i++)
        idx[i] = i;
    for (i = 0; i < M - 1; i++)
    {
        for (j = i + 1; j < M; j++)
            if (avgR[i] < avgR[j])
            {
               temp=idx[i], idx[i] = idx[j], idx[j] = temp;
            }
    }
    return;
}               

void printstring(char *s, int n)
{
    int i=strlen(s),j;
    if(i<=n)
        {
            printf("%s",s);
            for(j=0;j<(n-i);j++)
                printf(" ");
        }
    else
        printf("%8s",s);
}

现在输出的成绩每次都不一样,但要按总分排序,怎样用冒泡排序法排序啊,请教下各位
搜索更多相关主题的帖子: 冒泡 
2011-12-14 15:13
紫禁
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2011-12-7
收藏
得分:0 
回复 楼主 紫禁
嘿嘿,自己想办法弄好了,发给大家看下,
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>


#define M 5
#define N 5

void input(int scores[][3]);
void cal(int scores[][3], double avgR[], double avgC[]);
void sort(double avgR[], int idx[]);
void printstring(char *s, int n); 

int main(void)
{
    char *ke[N] = {"yuwen","shuxue","yingyu","pingjun","zongfen"};
    char *names[M] = {"qin", "wu", "yu", "sa", "er"};
    int scores[M][3] = {0}, idx[M] = {0}, i, j;
    double avgR[M] = {0}, avgC[3] = {0};
    input(scores);
    cal(scores, avgR, avgC);
    sort(avgR, idx);
    for(j=0;j<N;j++)
    {
        if(j==0)
            printstring(" ",8);
        printstring(ke[j],8);

 
    }
    printf("\n");
    for (i = 0; i < M; i++)
    {
        printstring(names[i],8);
        for (j = 0; j < 3; j++)
        {
            printf("%3d     ", scores[i][j]);
        }
        printf("%3.1lf    ", avgR[i]);
        printf("%3.0f     \n", avgR[i]*3);
    }
    getchar();
    return 0;
}

void input(int score[][3])
{
    int i, j;
    srand((unsigned) time(NULL));
    for (i = 0; i < M; i++)
        for (j = 0; j < 3; j++)
            score[i][j] = 40 + rand() % 61;
    return;
}
void cal(int scores[][3], double avgR[], double avgC[])
{
    int i, j;
    for (i = 0; i < M; i++)
    {
        for (j = 0; j < 3; j++)
        {
            avgR[i] += scores[i][j];
            avgC[j] += scores[i][j];
        }
        avgR[i] /= 3;
    }
    for (j = 0; j < 3; j++)
        avgC[j] /= M;
    return;
}
void sort(double avgR[], int idx[])
{
    int i, j;
    double temp;
    for (i = 0; i < M; i++)
        idx[i] = i;
    for (i = 0; i < M - 1; i++)
    {
        for (j = i + 1; j < M; j++)
            if (avgR[idx[i]] < avgR[idx[j]])
            {
               temp = avgR[idx[i]], avgR[idx[i]] = avgR[idx[j]], avgR[idx[j]] = temp;/*改了这里*/
            }
    }
    return;
}               

void printstring(char *s, int n)
{
    int i=strlen(s),j;
    if(i<=n)
        {
            printf("%s",s);
            for(j=0;j<(n-i);j++)
                printf(" ");
        }
    else
        printf("%8s",s);
}

2011-12-14 15:29
快速回复:请叫下,下面这个程序,怎样用冒泡排序法排列,就差最后一步了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051430 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved