清大佬帮我看看哪里错了
题目是这样的:输入十个整型数,从小到大排序取中间八个求均值我的代码
#include<stdio.h>
int main()
{
int a;
int n, j, num, flag = 0;
for (n = 0;n <= 9;n++)
{
printf("Please enter ten number which you want to calculate:(n-n)");
scanf("%d", &a);
if(a < a)
{
a = j;
a = a;
a = j;
}
}
for(flag = 1;flag <= 8;flag++)
{
num = num + a;
}
printf("The result is:%d\n", num/8);
return 0;
}
#include <stdio.h>
int main() {
int a;
//int n, j, num, flag = 0;
int n, num, flag = 0;
for(n = 0; n <= 9; n++) {
printf("Please enter ten number which you want to calculate:(n-n): ");
scanf("%d", &a);
/*
if(a < a) {
a = j;
a = a;
a = j;
}
*/
}
for(size_t i = 0; i < 10; ++i) {
for(size_t j = i + 1; j < 10; ++j) {
if(a > a) {
int temp = a;
a = a;
a = temp;
}
}
}
num = 0; // ************
for(flag = 1; flag <= 8; flag++) {
num = num + a;
}
printf("The result is: %d\n", num / 8);
return 0;
}
本帖最后由 jackz007 于 2022-9-29 22:08 编辑
#include<stdio.h>
int main(void)
{
int a , n , j , num , flag = 0 ;
for(n = 0 ; n <= 9 ; n ++) {
printf("Please enter ten number which you want to calculate:(n-n)") ;
scanf("%d", &a) ;
if(a < a) { // 这个排序不彻底
a = j ;// j = ?这一句不是在砸场子吗
a = a ;
a = j ;
}
}
for(flag = 1 ; flag <= 8 ; flag ++) num = num + a ;// num 没有初始化为 0
printf("The result is:%d\n" , num / 8) ;
return 0 ;
}
请参考我修改的代码:
#include<stdio.h>
int main(void)
{
int a , i , j , n , num , t ;
for(n = 10 , i = 0 ; i < n ; i ++) {
printf("Please enter ten number which you want to calculate : (n-n) ");
scanf("%d", & a) ;
for(j = i ; j && a < a ; j --) {
t = a ;
a = a ;
a = t ;
}
}
for(num = 0 , i = 1 ; i < n - 1 ; i ++) num += a ;
printf("The result is : %.2f\n" , 1.0 * num / 8.0) ;
}
编译、运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
Please enter ten number which you want to calculate : (n-n) 7
Please enter ten number which you want to calculate : (n-n) 4
Please enter ten number which you want to calculate : (n-n) 8
Please enter ten number which you want to calculate : (n-n) 2
Please enter ten number which you want to calculate : (n-n) 5
Please enter ten number which you want to calculate : (n-n) 9
Please enter ten number which you want to calculate : (n-n) 10
Please enter ten number which you want to calculate : (n-n) 5
Please enter ten number which you want to calculate : (n-n) 7
Please enter ten number which you want to calculate : (n-n) 3
The result is : 6.00
D:\\C> 本帖最后由 桃花飞舞 于 2022-9-29 22:41 编辑
#include <stdio.h>
void bubble_sort(int *a,int n);
int main()
{
int a = {0};
int n,num = 0, flag = 0;
for (n = 0;n <= 9; n++)
{
printf("Please enter ten number which you want to calculate:(10-%d)",n);
scanf("%d", &a);
// if(a < a)
// {
// a = j;
// a = a;
// a = j;
// }
}
bubble_sort(a,10);
for(flag = 1;flag <= 8;flag++)
{
num = num + a;
}
printf("The result is:%d\n", num/8);
return 0;
}
void bubble_sort(int *a,int n)
{
int i,j,k,tmp = 0;
//遍历的次数n-1
for(i=1; i < n; i++)
{ //比较次数
for(j=1,k=0;j < n-i; j++,k++)
{
//前后比较
if(a > a)
{
tmp = a;
a = a;
a = tmp;
}
}
}
}
第一 :你的结果是由于int num;这里一开始没有给num 初始值,num里面是随机值,造成的。可以改为int num = 0;
第二:排序也有错误,楼上都有说 桃花飞舞 发表于 2022-9-29 22:37
第一 :你的结果是由于int num;这里一开始没有给num 初始值,num里面是随机值,造成的。可以改为int num ...
哦哦哦好的 人造人 发表于 2022-9-29 21:45
你好,学生,膜拜,谢谢 jackz007 发表于 2022-9-29 22:01
请参考我修改的代码:
编译、运行实况:
谢谢杰克{:10_254:} 本帖最后由 jhq999 于 2022-9-30 15:55 编辑
单链表插入排序
#include <stdio.h>
int main(void)
{
int num={0},i=0,j=0,sum=0;
num=-1;
for(i=1;i<=10;i+=1)
{
scanf("%d",*(num+i));
for(j=0;num>=0&&num>num];j=num);
num=num;
num=i;
}
sum=0;
for(i=num;num]>=0;i=num)
{
printf("%4d",num]);
sum+=num];
}
printf("\n%d %.2f",sum,(float)sum/8);
return 0;
}
如果不用排序的话
#include <stdio.h>
int main(void)
{
int num={0},i=0,j=0,sum=0;
scanf("%d",num);
sum=num=num=num;
for(i=1;i<10;i+=1)
{
scanf("%d",num+i);
if(num>num)
num=num;
else if(num<num)
num=num;
sum+=num;
}
printf("\n%d %.2f",sum-num-num,(float)(sum-num-num)/8);
return 0;
}
页:
[1]