## 11462 - Age Sort

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11462 - Ages Sort

C/C++ is faster than JAVA.
Check input and AC output for thousands of problems on uDebug!

SyFyKid
New poster
Posts: 26
Joined: Tue May 08, 2012 9:47 am

### Re: 11462 - Ages Sort

I am not talking about C/C++, I am talking about JAVA and better I/O methods.
brianfry713 wrote:C/C++ is faster than JAVA.

jocker1
New poster
Posts: 5
Joined: Sat May 19, 2012 11:10 pm

### Re: 11462 - Ages Sort

How to output this code....... cause i got PE... plz could any one help me...

Code: Select all

``````#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int main(){
vector<int> mvector;
vector<int>::iterator it;
unsigned int ncases;
int numbers;
while(cin>>ncases){
if(ncases==0) break;
while(ncases-- && cin>>numbers){
if(numbers<1) break;
mvector.push_back(numbers);
}
sort(mvector.begin(),mvector.end());
for(it=mvector.begin();it!=mvector.end();it++){
if(it==mvector.end()) cout<<*it<<endl;
cout<<*it<<" ";
}
mvector.erase(mvector.begin(),mvector.end());

}
return 0;
}

``````

SyFyKid
New poster
Posts: 26
Joined: Tue May 08, 2012 9:47 am

### Re: 11462 - Ages Sort

you dont need sort, erase and all that stuff...
jocker1 wrote:How to output this code....... cause i got PE... plz could any one help me...

Code: Select all

``````#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int main(){
vector<int> mvector;
vector<int>::iterator it;
unsigned int ncases;
int numbers;
while(cin>>ncases){
if(ncases==0) break;
while(ncases-- && cin>>numbers){
if(numbers<1) break;
mvector.push_back(numbers);
}
sort(mvector.begin(),mvector.end());
for(it=mvector.begin();it!=mvector.end();it++){
if(it==mvector.end()) cout<<*it<<endl;
cout<<*it<<" ";
}
mvector.erase(mvector.begin(),mvector.end());

}
return 0;
}

``````

sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

### Re: 11462 - Ages Sort

Hello
I've got WA,
Why?????

Here is my code

Code: Select all

``````import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class Main {

public static void main(String[] args) {

String line;
try {
if (line.trim().equals("0")) {
break;
}

int numbersCount = Integer.parseInt(line);
PriorityQueue<Integer> numbers = new PriorityQueue<Integer>(numbersCount);

while (tokenizer.hasMoreTokens()) {
}

StringBuilder builder = new StringBuilder();
for (Iterator<Integer> iterator = numbers.iterator(); iterator.hasNext(); ) {
Integer number = iterator.next();
builder.append(number);
if (iterator.hasNext()) {
builder.append(" ");
}
}
System.out.println(builder.toString());
}
} catch (IOException e) {

}
}
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11462 - Ages Sort

Use poll(), not an iterator.
http://docs.oracle.com/javase/1.5.0/doc ... Queue.html
The iterator does not return the elements in any particular order.
poll() Retrieves and removes the head of this queue, or null if this queue is empty.
Check input and AC output for thousands of problems on uDebug!

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

### Re: 11462 - Ages Sort

why its "TIME LIMIT EXCEEDED""??

import java.io.*;
import java.util.*;
public class Main{
public static void main(String [] args)throws IOException{
PrintWriter z = new PrintWriter(System.out);
int t;
{
int [] y = new int [t];
int c = 0;
while(s.hasMoreTokens())
{
y[c++] = Integer.valueOf(s.nextToken());
}
Arrays.sort(y);
for(c = 0;c<y.length;c++)
{
z.print(y[c]+" ");
z.flush();
}
z.println();
z.flush();
}
}
}

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

### Re: Age Sort

Deleted
Last edited by raj on Tue Mar 05, 2013 11:52 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: Age Sort

This doesn't match the sample I/O for problem 11462.

Try using BufferedWriter and don't flush the output until the end.
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11462 - Ages Sort

Don't double post.
Check input and AC output for thousands of problems on uDebug!

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

### Re: Age Sort

thanks sir......... its now accepted.. by using flush() at the end only...
but sir can u please tell me why i should BufferedWriter instead of using PrintWriter...??

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: Age Sort

I think BufferedWriter is faster.
Check input and AC output for thousands of problems on uDebug!

shaho
New poster
Posts: 1
Joined: Sat Dec 28, 2013 1:26 pm

### Re: 11462 - Ages Sort

Hi
How I can solve this problem using Java?
I used CountingSort , and used System.out.println(); only once.
But got TL

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11462 - Ages Sort

Check input and AC output for thousands of problems on uDebug!

ssavi
New poster
Posts: 28
Joined: Thu Nov 20, 2014 9:57 pm

### Re: 11462 - Age Sort

Hi why i am getting RTE . Its Counting Sort algorithm . please give a suggestion .

Code: Select all

`````` Got AC
``````
I know I am a Failure Guy .