Page 4 of 6

Please Help! 299 WA

Posted: Tue Apr 19, 2005 10:02 am
by triker
i searched the forum but i can't find what's wrong with my code

Code: Select all

#include <iostream>

using namespace std;

int main()
{
    int testCase, nCase=0;
    int length;
    int temp;
    
    cin >> testCase;
     
    while(nCase < testCase)
    {
        cin >> length;
        int train[length];
        
        for(int i=0;i<length;i++)
        {
                cin >> train[i];
        }
        
        int x=0;
        int swap=0;
        for(int i=0;i<length;i++,x++)
        {
                for(int j=0;j<length-x;j++)
                {
                    if(train[j]>train[j+1])
                    {
                        temp = train[j];
                        train[j]=train[j+1];
                        train[j+1]=temp;
                        swap++;
                    }
                }            
        }
        cout << "Optimal train swapping takes " << swap << " swaps." << endl;
        nCase++;
    }   

    return 0;
}

ooopsiii daisy..

Posted: Tue Apr 19, 2005 3:56 pm
by sohel
assign the initial value of x as 1 and not 0..

ie
x = 1; // correct
x = 0; // incorrect.

Posted: Tue Apr 19, 2005 7:08 pm
by triker
thanks :D
i missed a very little mistake :-?

Posted: Wed May 11, 2005 11:35 pm
by Jemerson
Hi everyone, ive posted in all 299 related topics about one interesting thing that bored me a lot, and gave me some WA'S! For sure there are EMPTY lines between the number of test cases and the first train length, dont know about between each test case, but it is also valid an attention on this too.

Posted: Wed May 11, 2005 11:36 pm
by Jemerson
Hi everyone, ive posted in all 299 related topics about one interesting thing that bored me a lot, and gave me some WA'S! For sure there are EMPTY lines between the number of test cases and the first train length, dont know about between each test case, but it is also valid an attention on this too.

299 problem compile error

Posted: Mon Oct 24, 2005 12:45 pm
by Artikali
Please help me compile error, but in VC++.NEt compiled OK
here is my code:

Code: Select all

#include <stdio.h>
#include <iostream>
using namespace std;
int bubblesort(int *a,int tsize)
{
	int k=0;
	for (int i=tsize-1;i>0;i--)
		for (int j=0;j<i;j++)
			if (a[j]>a[j+1]) {
				swap(a[j],a[j+1]);
				k++;
			}
	return k;
}
int main(){
	int t,n, a[50],p;

	scanf("%d",&t);
	for (int i=0; i<t;i++)
	{
		scanf("%ld",&n);
		for (int j=0;j<n;j++) scanf("%ld",&a[j]);
		p=bubblesort(a,n);
		printf("Optimal train swapping takes %d swaps.\n",p);
	}
	return 0;
}
Thanks

Posted: Mon Oct 24, 2005 5:47 pm
by chunyi81
You need to #include <algorithm> if you want to use the swap function.

These are the errors I got from compiling your program:
p299.cpp: In function `int bubblesort(int *, int)':
p299.cpp:10: implicit declaration of function `int swap(...)'

Visual C++ .NET automatically includes the necessary headers for you but for g++ compilier in the UVA, it doesn't. You need to explicitly include the header libraries you need.

"911" : Problem with 299 (freaking judge shows WA)

Posted: Sun Oct 30, 2005 12:54 pm
by saifposinc
:-? What's the problem with my code ?
Why show WA ?
Plz help!!


Here's the code :


#include<iostream>
#define MAX 50
using namespace std;
int srt(int a[],int n);
main(){
int i,j,num,x,a[MAX];
cin>>x;
for(j=0;j<x;j++){
cin>>num;
for(i=0;i<num;i++)
cin>>a[i];
cout<<"Optimal train swapping takes "<<srt(a,num)<<" swaps\n";
}
return 0;
}
int srt(int a[],int n){
int i,j,c=0;
for(i=0;i<n-1;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
c++;
}
}
}
return c;
}[/b]

Posted: Thu Nov 10, 2005 12:39 am
by Jan
Change this line...

Your code...

Code: Select all

cout<<"Optimal train swapping takes "<<srt(a,num)<<" swaps\n"; 
Should be..

Code: Select all

cout<<"Optimal train swapping takes "<<srt(a,num)<<" swaps.\n"; 
Missing the full stop.. :)

risky?

Posted: Tue Nov 29, 2005 5:56 am
by worm3959
saifposinc wrote:Change this line.
:wink: But is that not a bit risky?

299. Compile error. why? This is OK whith g++.

Posted: Sat Feb 04, 2006 3:12 pm
by wes
Why I'm getting CE, with this code??

Code: Select all

/* @JUDGE_ID: ******* 299 C++ */ 
#include <iostream>
using namespace std;

int Swaps(int L, int carriages[]){
  int count(0);
  
  for(int i=L-1; i>0; i--){
    for(int j = i; j>0; j--){
      if(carriages[j] < carriages[j-1]){	
	int temp = carriages[j-1];
	carriages[j-1] = carriages[j];
	carriages[j] = temp;
	count++;		
      }
    }
  }
  for(int i=0; i<L-1; i++){
    for(int j = i; j<L-1; j++){
      if(carriages[j] > carriages[j+1]){	  
	int temp = carriages[j+1];
	carriages[j+1] = carriages[j];
	carriages[j] = temp;
	count++;		
      }
    }
  }
  return count;
}

int main () {
  int tests;
  cin >> tests;
  
  while(tests != 0){
    int L, swaps;
    int train[50];
    cin >> L;
    for(int i = 0,; i < L; i++)
      cin >> train[i];
    swaps = Swaps(L, train);
    cout << "Optimal train swapping takes " << swaps <<  " swaps." << endl;
    tests--;
  }
  return 0;
}

 
Thanks in advance!

Posted: Sat Feb 04, 2006 3:32 pm
by mamun
wes wrote:This is OK whith g++
Don't tell me you passed any compilation with this line (in main())

Code: Select all

for(int i = 0,; i < L; i++)

Posted: Sat Feb 04, 2006 3:42 pm
by wes
mamun wrote:
wes wrote:This is OK whith g++
Don't tell me you passed any compilation with this line (in main())

Code: Select all

for(int i = 0,; i < L; i++)
i really don't know how but...
:~$ g++ -o 299 299.cpp
:~$ ./299 <299.in
Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.
thanks!

299 wa help!plz

Posted: Tue Feb 28, 2006 7:48 pm
by yogeshgo05
hey guys ...
i dont no i get wa plz help here's my code ...
is there a better optimised code ...

Code: Select all

DELETED

Re: 299 wa help!plz

Posted: Tue Feb 28, 2006 8:54 pm
by TISARKER
yogeshgo05
You did not swap value after comparing. :lol:
This is only and only your mistake. :D
if(a[j]>a[j+1)then
1.increase counter
2.Swap a[j] and a[j+1]
Hope u have understood