c语言—冒泡排序求助
大佬们,可以帮我看看我这个代码中冒泡排序为啥没有生效吗,我感觉是作用域的问题,可是我不知道怎么处理
3,5
5.6 7.8 9.4 5.8 8.8
7.8 6.7 8.3 6.9 5.5
6.7 9.2 8.8 8.3 9.0
这个是样例输入
static/image/hrline/line7.png
#include<stdio.h>
int main() {
int n, m, i, j, k, l, t, c = { 0 }, p;
double b = { 0.0 }, q = 0.0, a = { 0.0 }, max = 0.0, min = 0.0;
char y;
scanf("%d", &n);
y = getchar();
scanf("%d", &m);
for (i = 1; i <= n; i++) {
max = 0.0;
min = 10.0;
for (j = 1; j <= m; j++) {//输入每组数据
scanf("%lf", &a);
if (a >= max)
{
max = a;
}
if (j == 1 || a <= min)
{
min = a;
}
}
for (j = 1; j <= m; j++) {//求平均数
b = b + a;
}
b = (b - max - min) / (m - 2.0);
c = i;
}
for (i = 1; i <= n - 1; i++) {
k = i;
for (l = k + 1; l <= m; l++) {
if (b < b) {
k = l;
}
}
if (k != i) {
q = b, b = b, b = q;
p = c, c = c, c = p;
}
}
for (i = 1; i <= n; i++) {
printf("第%d名:%d号选手,得分%.2lf\n", i, c, b);
}
} 本帖最后由 jackz007 于 2022-10-7 20:08 编辑
难道这个结果不正确吗?
3,5
5.6 7.8 9.4 5.8 8.8
7.8 6.7 8.3 6.9 5.5
6.7 9.2 8.8 8.3 9.0
第1名:3号选手,得分8.70
第2名:1号选手,得分7.47
第3名:2号选手,得分7.13
你是根据什么怀疑冒泡程序有问题的? jackz007 发表于 2022-10-7 19:59
难道这个结果不正确吗?
你是根据什么怀疑冒泡程序有问题的?
我。。。。跑下来似乎。。。。不是这样的,我跑下来就是冒泡排序没发挥作用的情况,我又去跑了一下,还不对,我去线上oj试试 航航. 发表于 2022-10-7 20:25
我。。。。跑下来似乎。。。。不是这样的,我跑下来就是冒泡排序没发挥作用的情况,我又去跑了一下,还不 ...
我跑的那个算例结果是否有问题? jackz007 发表于 2022-10-7 20:28
我跑的那个算例结果是否有问题?
没有问题,是对的,大佬可以把你那边改动过的代码发我一下我去试试吗 main.c: In function ‘main’:
main.c:23:19: error: invalid operands to binary + (have ‘double *’ and ‘double’)
23 | b = b + a;
| ~ ^ ~~~~
| | |
| | double
| double *
main.c:25:16: error: invalid operands to binary - (have ‘double *’ and ‘double’)
25 | b = (b - max - min) / (m - 2.0);
| ~ ^
| |
| double *
main.c:26:11: error: assignment to expression with array type
26 | c = i;
| ^
main.c:36:30: error: incompatible types when assigning to type ‘double’ from type ‘double *’
36 | q = b, b = b, b = q;
| ^
main.c:36:35: error: assignment to expression with array type
36 | q = b, b = b, b = q;
| ^
main.c:36:31: warning: left-hand operand of comma expression has no effect [-Wunused-value]
36 | q = b, b = b, b = q;
| ^
main.c:37:28: warning: assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast [-Wint-conversion]
37 | p = c, c = c, c = p;
| ^
main.c:37:35: error: assignment to expression with array type
37 | p = c, c = c, c = p;
| ^
main.c:41:25: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]
41 | printf("第%d名:%d号选手,得分%.2lf\n", i, c, b);
| ~^ ~
| | |
| int int *
| %ls
main.c:41:41: warning: format ‘%lf’ expects argument of type ‘double’, but argument 4 has type ‘double *’ [-Wformat=]
41 | printf("第%d名:%d号选手,得分%.2lf\n", i, c, b);
| ~~~~^ ~
| | |
| double double *
main.c:6:10: warning: variable ‘y’ set but not used [-Wunused-but-set-variable]
6 | char y;
| ^
main.c:4:27: warning: unused variable ‘t’ [-Wunused-variable]
4 | int n, m, i, j, k, l, t, c = {0}, p;
本帖最后由 jackz007 于 2022-10-7 20:34 编辑
航航. 发表于 2022-10-7 20:29
没有问题,是对的,大佬可以把你那边改动过的代码发我一下我去试试吗
应该是没有任何的改动,只是你贴出的代码没有进入代码框,丢失了一些代码成分,丢失部分我是凭借经验猜出来给补上去的。
#include<stdio.h>
int main() {
int n, m, i, j, k, l, t, c = { 0 }, p;
double b = { 0.0 }, q = 0.0, a = { 0.0 }, max = 0.0, min = 0.0;
char y;
scanf("%d", &n);
y = getchar();
scanf("%d", &m);
for (i = 1; i <= n; i++) {
max = 0.0;
min = 10.0;
for (j = 1; j <= m; j++) {//输入每组数据
scanf("%lf", &a);
if (a >= max)
{
max = a;
}
if (j == 1 || a <= min)
{
min = a ;
}
}
for (j = 1 ; j <= m ; j++) {//求平均数
b = b + a;
}
b = (b - max - min) / (m - 2.0);
c = i ;
}
for (i = 1; i <= n - 1; i++) {
k = i;
for (l = k + 1; l <= m; l++) {
if (b < b) {
k = l;
}
}
if (k != i) {
q = b, b = b , b = q;
p = c, c = c , c = p;
}
}
for (i = 1; i <= n; i++) {
printf("第%d名:%d号选手,得分%.2lf\n", i , c , b) ;
}
} jackz007 发表于 2022-10-7 20:32
应该是没有任何的改动,只是你贴出的代码没有进入代码框,丢失了一些代码成分,丢失部分我是凭 ...
嘶,用您这个的话就对了,可能就是丢失部分我哪里写错了,谢谢大佬,我拿去比对比对 航航. 发表于 2022-10-7 20:36
嘶,用您这个的话就对了,可能就是丢失部分我哪里写错了,谢谢大佬,我拿去比对比对
是从 25 行开始有丢失的。你 1 楼的贴出的代码开始出现斜体字的时候,说明,所有的 [ i ] 都被丢掉了。 jackz007 发表于 2022-10-7 20:32
应该是没有任何的改动,只是你贴出的代码没有进入代码框,丢失了一些代码成分,丢失部分我是凭 ...
嘶,我突然发现我代码就粘贴错了,我想发的代码应该这这个{:10_247:}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int n, m;
float sum = 0;
float average;
float grade_list;//成绩暂存
int list; // 成员名单
float final_grade;
memset(grade_list,0,sizeof(grade_list));
scanf("%d,%d", &n, &m);
//初始化成员名单下标
for (int a = 0; a < 45;a++)
{
list = a;
}
//循环输入
for (int i = 0; i < n; i++)// 循环选手
{
for (int j = 0; j < m; j++)// 循环评委
{
scanf("%f", &grade_list);
}
//去除极值
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (grade_list > grade_list)
{
float g = grade_list;
grade_list = grade_list;
grade_list = g;
}
}
}
for (int i = 1; i < m; i++)
{
//printf("%.2f\n", sum);
sum += grade_list;
}
final_grade = sum / (m-2);
}
// 冒泡排序
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (final_grade > final_grade)
{
float t = final_grade , k = list;
final_grade = final_grade;
list = list;
final_grade = t;
list = k;
}
}
}
//输出结果
for (int i = 0; i < n; i++)
{
if (i != n - 1)
{
printf("第%d名:%d号选手,得分%.2f\n", i + 1, list+1, final_grade);
}
else
{
printf("第%d名:%d号选手,得分%.2f", i + 1, list+1, final_grade);
}
}
return 0;
} 航航. 发表于 2022-10-7 20:40
嘶,我突然发现我代码就粘贴错了,我想发的代码应该这这个
#include <stdio.h>
#include <stdlib.h>
#include <memory.h> // 添加此文件,否则,memset() 会用不了
int main(void)
{
int i , j , n , m ;
float min , max, sum ;
float average ;
float grade_list;//成绩暂存
int list; // 成员名单
float final_grade;
memset(grade_list,0,sizeof(grade_list));
scanf("%d,%d", &n, &m);
//初始化成员名单下标
for (int a = 0 ; a < 45 ; a ++)
{
list = a;
}
//循环输入
for (i = 0; i < n; i ++)// 循环选手
{
for (j = 0; j < m; j++)// 循环评委
{
scanf("%f", &grade_list);
}
//【修改】:去除极值的代码完全重写
for(sum = 0 , min = max = grade_list , j = 0 ; j < m ; j ++) {
sum += grade_list ;
if(grade_list < min) min = grade_list ;
if(grade_list > max) max = grade_list ;
}
final_grade = (sum - max - min) / (m - 2) ;
}
// 冒泡排序
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (final_grade < final_grade) // 【修改】:排序结果是反序的,把 > 改为 < 即可
{
float t = final_grade , k = list;
final_grade = final_grade;
list = list;
final_grade = t;
list = k;
}
}
}
//输出结果
for (int i = 0; i < n; i++)
{
if (i != n - 1)
{
printf("第%d名:%d号选手,得分%.2f\n", i + 1, list+1, final_grade);
}
else
{
printf("第%d名:%d号选手,得分%.2f", i + 1, list+1, final_grade);
}
}
return 0;
}
页:
[1]