主题:帮我解决下问题在那里谢谢啦!
63755095
[专家分:0] 发布于 2010-07-26 13:35:00
#include <cstdlib>
#include <iostream>
using namespace std;
int xuexe(int a[],int size)
{
int i,t=0,min=a[0];
while(t!=4)
{
for(i=t;i<=4;i++)
{if (a[i]<min)
min=a[i];
}
a[i]=a[t]; a[t]=min;
cout<<min<<endl; //这里输出不对的
t++;//还有这里t++貌似没用一样的
}
}
int main(int argc, char *argv[])
{
const int n=5;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
xuexe(a,n);
for(int i=0;i<n;i++)
cout<<a[i];
system("PAUSE");
return EXIT_SUCCESS;
}
用选择排序方法排序 在数组中找出最小的放在第一个 然后不管地一个 在后面的数组中找出最小的放在地个 ......然后输出
帮我想出办法 谢谢!
回复列表 (共14个回复)
沙发
酱爆鱼虾蟹 [专家分:40] 发布于 2010-07-26 16:32:00
int i,t=0,min=a[0],j,temp;
while(t!=5)
{for(i=t;i<=4;i++)
{if(a[i]<min)
min=a[i];
j=i;
}
temp=a[t];a[t]=a[j];a[j]=temp;
t++;
}
现在没办法调试。你看看这样写对不?
板凳
yxqyrh [专家分:1070] 发布于 2010-07-27 11:38:00
程序很简单,给你说下你的问题
你的函数传进来的参数是size,但是你确在函数体内用4,这个4是什么?
当你a[i]<min的时候,你要记录此时的i,因为你的循环还在继续
排序的函数没返回值
3 楼
63755095 [专家分:0] 发布于 2010-07-27 15:03:00
本来开始的时候我也用size的 因为有很多问题 我在调试 ,觉得这样直接用数字方便点才改成4的 ,要记录i,谢谢啦
4 楼
63755095 [专家分:0] 发布于 2010-07-27 15:05:00
用你的方法我调试了下 结果是15432;我在看看毛 谢谢啦
5 楼
63755095 [专家分:0] 发布于 2010-07-27 15:17:00
#include <cstdlib>
#include <iostream>
using namespace std;
void xuexe(int a[],int size)
{
int i,t=0,min=a[0],j,temp;
while(t!=size)
{
for(i=t;i<=size-1;i++)
{if (a[i]<min)
min=a[i];
j=i;
}
temp=a[t];a[t]=a[j];a[j]=temp;
t++;
}
}
int main(int argc, char *argv[])
{
const int n=5;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
xuexe(a,n);
for(int i=0;i<n;i++)
cout<<a[i];
system("PAUSE");
return EXIT_SUCCESS;
}
程序运行后 我输入的是 54321,然后输出的是 15432
貌似程序就执行了一次毛 ,难道这个t++没用的么 ?上面的说排序的美返回值的 我现在改为void函数毛 这样可以的么?
6 楼
xingxing01 [专家分:0] 发布于 2010-07-27 15:51:00
I'm sure <A href="http://www.toppowerlevel.net">wow power leveling</A> this will come as no surprise. What with all the talk about security holes, and the advanced age of the browser (what, nine years now?), <A href="http://www.mogxe.com/PowerLevel.php?gid=1">wow power leveling</A> major websites are dropping IE6 support left <A href="http://www.toppowerlevel.net/powerlist.php?fid=688">wow power leveling</A> and right—and that's not to mention the specific concern about the security <A href="http://www.toppowerlevel.net/buy.php">wow gold</A> of WoW accounts.
So let us say officially: We will shortly be dropping support <A href="http://www.toppowerlevel.net/powerlist.php?fid=7422">aion power leveling</A> for Internet Explorer 6 <A href="http://www.toppowerlevel.net/gamelist.php?fid=7656">aion gold</A> (IE6).
Now for those of you who are stuck with this <A href="http://www.toppowerlevel.net">cheap wow power leveling</A> geriatric browser for whatever reason, don't despair! This isn't quite as harsh as <A href="http://www.toppowerlevel.net">buy wow power leveling</A> it sounds—it doesn't mean the page is suddenly going to stop working completely <A href="http://www.toppowerlevel.net">cheap wow gold</A> in IE6. It only means that if we make a change or an update that makes the page look funny in <A href="http://www.mogxe.com/PowerLevel.php?gid=21">aion power leveling</A>IE6...well, we're not going to lose any sleep over it.
No longer worrying about maintaining support for this old browser will enable us to spend more time developing new features, and to make sure we're taking advantage of all the technological advances newer browsers have to offer.
7 楼
xingxing01 [专家分:0] 发布于 2010-07-27 15:52:00
Starting [url=http://www.toppowerlevel.net]wow power leveling[/url] now, and running until the 17th of March, you can submit a picture of yourself sporting a Wowhead rocket logo [url=http://www.mogxe.com/PowerLevel.php?gid=1]wow power leveling[/url] to our contest page for a chance to win some [url=http://www.toppowerlevel.net/powerlist.php?fid=688]wow power leveling[/url] awesome prizes. We're also offering discounts on Wowhead products in [url=http://www.toppowerlevel.net/buy.php]wow gold[/url] the Wowhead [url=http://www.toppowerlevel.net/powerlist.php?fid=7422]aion power leveling[/url] Store during this contest, [url=http://www.toppowerlevel.net/gamelist.php?fid=7656]aion gold[/url] so now's the perfect time to pick up a t-shirt!Those [url=http://www.toppowerlevel.net]cheap wow power leveling[/url] of you who use the Mac version of the Wowhead Client may have been having some trouble [url=http://www.toppowerlevel.net]buy wow power leveling[/url] uploading your data lately. Well, we've been having a bit of trouble with the [url=http://www.toppowerlevel.net]cheap wow gold[/url] framework that the Mac Client uses to handle updating itself. We've updated the Client so that it works properly, [url=http://www.mogxe.com/PowerLevel.php?gid=21]aion power leveling[/url] but I'm afraid you'll need to download it again.
8 楼
yxqyrh [专家分:1070] 发布于 2010-07-27 17:42:00
j没初始化,会出现数组下标越界的情况的
第二个循环改成
for(i=t,j=t,min=a[i];i<=size-1;i++)
{if (a[i]<min)
min=a[i];
j=i;
}
9 楼
yxqyrh [专家分:1070] 发布于 2010-07-27 18:01:00
你要学一下调试程序的啊
没有哪个程序员不会调试的。。。
10 楼
63755095 [专家分:0] 发布于 2010-07-29 13:55:00
我也在学调试的 不过是新手 很多地方还是发现不了问题的 只能请教高手了
我来回复