10327 - Flip Sort
Moderator: Board moderators
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2002 1:18 pm
10327 - Flip Sort
i send my programe tothe judge !!but it got WA and I don't know why!!
is there anyone can help me to find the bug ???
[cpp]
#include<stdio.h>
int in[5000],input,time=0,ins;
main()
{
while(scanf(" %d",&input)==1)
{
time=0;
for(int i=0;i<input;i++)
scanf(" %d",&in);
for(int i=0;i<input;i++)
{
for(int j=i+1;j<input;j++)
{
if(in>in[j])
{
ins=in;
in=in[j];
in[j]=ins;
time++;
}
}
}
printf("Minimum exchange operations : %d\n",time);
}
}[/cpp]
is there anyone can help me to find the bug ???
[cpp]
#include<stdio.h>
int in[5000],input,time=0,ins;
main()
{
while(scanf(" %d",&input)==1)
{
time=0;
for(int i=0;i<input;i++)
scanf(" %d",&in);
for(int i=0;i<input;i++)
{
for(int j=i+1;j<input;j++)
{
if(in>in[j])
{
ins=in;
in=in[j];
in[j]=ins;
time++;
}
}
}
printf("Minimum exchange operations : %d\n",time);
}
}[/cpp]
-
- New poster
- Posts: 17
- Joined: Fri May 24, 2002 4:24 am
- Location: Taiwan
- Contact:
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2002 1:18 pm
i fix my code... but ....
i fix my code but it still WA!!
can you say more clear???
i find the smallest number and change
Is there any thing i didn't consider???
[cpp]
#include<stdio.h>
int in[5000],input,time=0,ins,min=0;
main()
{
int i,j;
while(scanf(" %d",&input)==1)
{
time=0;
for(i=0;i<input;i++)
scanf(" %d",&in);
for(i=0;i<input;i++)
{
min=i;
for(j=i+1;j<input;j++)
if(in>in[j])
min=j;
if(in>in[min])
{
ins=in;
in=in[min];
in[min]=ins;
time++;
}
}
printf("Minimum exchange operations : %d\n",time);
}
}[/cpp]
can you say more clear???
i find the smallest number and change
Is there any thing i didn't consider???
[cpp]
#include<stdio.h>
int in[5000],input,time=0,ins,min=0;
main()
{
int i,j;
while(scanf(" %d",&input)==1)
{
time=0;
for(i=0;i<input;i++)
scanf(" %d",&in);
for(i=0;i<input;i++)
{
min=i;
for(j=i+1;j<input;j++)
if(in>in[j])
min=j;
if(in>in[min])
{
ins=in;
in=in[min];
in[min]=ins;
time++;
}
}
printf("Minimum exchange operations : %d\n",time);
}
}[/cpp]
Re: i fix my code... but ....
mystical_liu wrote:i fix my code but it still WA!!
can you say more clear???
i find the smallest number and change
Is there any thing i didn't consider???
[cpp]
#include<stdio.h>
int in[5000],input,time=0,ins,min=0;
main()
{
int i,j;
while(scanf(" %d",&input)==1)
{
time=0;
for(i=0;i<input;i++)
scanf(" %d",&in);
for(i=0;i<input;i++)
{
min=i;
for(j=i+1;j<input;j++)
if(in>in[j]) <-------- in[min]>in[j] i think is what you want
min=j;
if(in>in[min])
{
ins=in;
in=in[min];
in[min]=ins;
time++;
}
}
printf("Minimum exchange operations : %d\n",time);
}
}[/cpp]
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2002 1:18 pm
OH!!!!I got it!!
OH!!!
I got it !!!!!
thinks a lot!!~!~
I know where am I wrong!!
thinks your remide!!!
I got it !!!!!
thinks a lot!!~!~
I know where am I wrong!!
thinks your remide!!!
10327
hI why i am getting tle for this problem? is there any efficient algorithm for this problem. Will anybody kindly see my code and tell me where i am doing too much.Here is my code:
#include <stdio.h>
main()
{
int a[1001],r,n,i,j,temp,cnt,count;
while(1)
{
r=scanf("%d",&n);
if(r==EOF) break;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
a[i]=0;
cnt=0;
for(i=0;i<n;i++)
{
count=0;
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
cnt++;
temp=a[j];
a[j]=a[i];
a[i]=temp;
i++;
if(i>=n-1) {i=-1;break;}
}
else if(a[i]<a[j])
{
count++;
if (count==n-1) {i=n;break;}
i++;
if(i>=n-1) {i=-1;break;}
}
}
}
printf("Minimum exchange operations : %d\n",cnt);
}
return 0;
}
#include <stdio.h>
main()
{
int a[1001],r,n,i,j,temp,cnt,count;
while(1)
{
r=scanf("%d",&n);
if(r==EOF) break;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
a[i]=0;
cnt=0;
for(i=0;i<n;i++)
{
count=0;
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
cnt++;
temp=a[j];
a[j]=a[i];
a[i]=temp;
i++;
if(i>=n-1) {i=-1;break;}
}
else if(a[i]<a[j])
{
count++;
if (count==n-1) {i=n;break;}
i++;
if(i>=n-1) {i=-1;break;}
}
}
}
printf("Minimum exchange operations : %d\n",cnt);
}
return 0;
}
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
10327 help
can i use number of swapping in bubble sort to calculate the min. operation ?
If yes, why there is still WA?
If yes, why there is still WA?
-
- Experienced poster
- Posts: 169
- Joined: Wed Oct 31, 2001 2:00 am
- Location: Singapore
10327 help
[pascal]
Program flip;
var i, j, cnt, n, temp:longint;
m:array[1..1000] of longint;
begin
while not eof do
begin
readln(n);
for i:=1 to n do
read(m);
cnt:=0;
for i:=1 to n-1 do
for j:=n downto i+1 do
If m[j] < m[j-1] then
begin
temp:=m[j-1]; (*this sentence is omitted*)
m[j-1]:=m[j]; (*this sentence is omitted*)
m[j]:=temp; (*this sentence is omitted*)
inc(cnt);
end;
writeln('Minimum exchange operations : ', cnt);
end;
end.
[/pascal]
This is my program ...
and I try to omit some sentences after you've told swapping is not necessary...
I think there is no bug....
but i still got WA
can you tell me what the problem is ?
Thanks
[/pascal]
Program flip;
var i, j, cnt, n, temp:longint;
m:array[1..1000] of longint;
begin
while not eof do
begin
readln(n);
for i:=1 to n do
read(m);
cnt:=0;
for i:=1 to n-1 do
for j:=n downto i+1 do
If m[j] < m[j-1] then
begin
temp:=m[j-1]; (*this sentence is omitted*)
m[j-1]:=m[j]; (*this sentence is omitted*)
m[j]:=temp; (*this sentence is omitted*)
inc(cnt);
end;
writeln('Minimum exchange operations : ', cnt);
end;
end.
[/pascal]
This is my program ...
and I try to omit some sentences after you've told swapping is not necessary...
I think there is no bug....
but i still got WA
can you tell me what the problem is ?
Thanks
[/pascal]
-
- Experienced poster
- Posts: 169
- Joined: Wed Oct 31, 2001 2:00 am
- Location: Singapore