首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

新手用if来写10个数字的冒泡程序,不知道逻辑异常错在哪里

2013-03-22 
新手用if来写10个数字的冒泡程序,不知道逻辑错误错在哪里?65 -76 -98 -23 67 32 4 7 98 11-98,-76,-23,4,7

新手用if来写10个数字的冒泡程序,不知道逻辑错误错在哪里?
65 -76 -98 -23 67 32 4 7 98 11
-98,-76,-23,4,7,32,65,11,67,98
Press any key to continue


上面是运行的结果:出错在h这个变量。

下面是代码:
#include<stdio.h>
main()
{
int a,b,c,d,e,f,g,h,i,j,t;
scanf("%d%d%d%d%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j);
if (a>b) {t=a,a=b,b=t;}
if (a>c) {t=a,a=c,c=t;}
if (a>d) {t=a,a=d,d=t;}
if (a>e) {t=a,a=e,e=t;}
if (a>f) {t=a,a=f,f=t;}
if (a>g) {t=a,a=g,g=t;}
if (a>h) {t=a,a=h,h=t;}
if (a>i) {t=a,a=i,i=t;}
if (a>j) {t=a,a=j,j=t;}

if (b>c) {t=b,b=c,c=t;}
if (b>d) {t=b,b=d,d=t;}
if (b>e) {t=b,b=e,e=t;}
if (b>f) {t=b,b=f,f=t;}
if (b>g) {t=b,b=g,g=t;}
if (b>h) {t=b,b=h,h=t;}
if (b>i) {t=b,b=i,i=t;}
if (b>j) {t=b,b=j,j=t;}

if (c>d) {t=c,c=d,d=t;}
if (c>e) {t=c,c=e,e=t;}
if (c>f) {t=c,c=f,f=t;}
if (c>g) {t=c,c=g,g=t;}
if (c>h) {t=c,c=h,h=t;}
if (c>i) {t=c,c=i,i=t;}
if (c>j) {t=c,c=j,j=t;}

if (d>e) {t=d,d=e,e=t;}
if (d>f) {t=d,d=f,f=t;}
if (d>g) {t=d,d=g,g=t;}
if (d>h) {t=d,d=h,h=t;}
if (d>i) {t=d,d=i,i=t;}
if (d>j) {t=d,d=j,j=t;}

if (e>f) {t=e,e=f,f=t;}
if (e>g) {t=e,e=g,g=t;}
if (e>h) {t=e,e=h,h=t;}
if (e>i) {t=e,e=i,i=t;}
if (e>j) {t=e,e=j,j=t;}
  
if (f>g) {t=f,f=g,g=t;}
if (f>h) {t=f,f=h,h=t;}
if (f>i) {t=f,f=i,i=t;}
if (f>j) {t=f,f=j,j=t;}

if (g>h) {t=g,g=h,h=t;}
if (g>i) {t=f,f=i,i=t;}
if (g>j) {t=f,f=j,j=t;}

if (h>i) {t=h,h=i,i=t;}
if (h>j) {t=h,h=j,j=t;}

if (i>j) {t=i,i=j,j=t;}

 printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",a,b,c,d,e,f,g,h,i,j);
 
}

c 冒泡?if?逻辑错误
[解决办法]


if (g>i) {t=f,f=i,i=t;}
if (g>j) {t=f,f=j,j=t;}

这里写错了
ctrl+c  ctrl+v的吧


我发现我真无聊的可以了。。。。
[解决办法]

if (g>h) {t=g,g=h,h=t;}
if (g>i) {t=f,f=i,i=t;}
if (g>j) {t=f,f=j,j=t;}


显然这里出错了

热点排行