## 11462 - Age Sort

Moderator: Board moderators

ahmed
New poster
Posts: 9
Joined: Tue Mar 17, 2009 6:36 pm

### Re: 11462 - Ages Sort

Code: Select all

``````#include<stdio.h>

int age[2000000];

void counting(int num)
{
int j;
int c[101]={0};
for(j=0;j<num;j++)
c[age[j]]++;
for(j=1;j<100;j++)
{
while(c[j])
{
printf("%d ",j);
c[j]--;
}
}
printf("\b\n");
}

int main(void)
{
int n,i;
scanf("%d",&n);
while(n)
{
for(i=0;i<n;i++)
{
scanf("%d",(age+i));
}
counting(n);
scanf("%d",&n);
}
return 0;
}
``````

joshua.bender
New poster
Posts: 2
Joined: Thu Sep 03, 2009 2:17 am

### Re: 11462 - Ages Sort

Code: Select all

``````import java.util.*;
import java.io.*;

public class Main {

public static void main(String args[]) throws IOException {
while (true) {
int nc = Integer.parseInt(tc);
if (nc == 0) {
break;
}
int arry[] = new int[nc];
StringTokenizer tokenizer = new StringTokenizer(age);
for (int i = 0; i < nc; i++) {
arry[i] = Integer.parseInt(tokenizer.nextToken());
}
Arrays.sort(arry);
for (int i = 0; i < nc; i++) {
System.out.print(arry[i]+" ");
}
System.out.println();

}
}
}
``````

sms.islam
New poster
Posts: 19
Joined: Sat Oct 10, 2009 10:28 am

### Re: 11462 - Ages Sort

"For each case, print a line with n space separated integers"
whats that means? How many space ?

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

### Re: 11462 - Ages Sort

sms.islam wrote:"For each case, print a line with n space separated integers"
whats that means? How many space ?
I think that means..

print n numbers and each number is separated by only one space

masum.shafayat
New poster
Posts: 3
Joined: Wed Nov 11, 2009 1:09 pm

### Re: 11462 - Ages Sort

What is real output format for this problem???????????????????????any body can help me?

receme
New poster
Posts: 17
Joined: Thu Jul 01, 2010 11:55 am

### Re: 11462 - Ages Sort

Thanks Sohel. I got ac now.
Last edited by receme on Mon Nov 08, 2010 8:33 am, edited 1 time in total.

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### Re: 11462 - Ages Sort

printf("\b\n"); --> this won't work.

noor_aub
New poster
Posts: 26
Joined: Sat Aug 22, 2009 12:16 pm

### Re: 11462 - Ages Sort

Why I am getting Runtime error Why

Code: Select all

``````#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
void merge(int a[2000001], long low, long high, long mid);
void mergesort(int a[2000001], long low, long high)
{
long mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
//return(0);
}

void merge(int a[2000001],long low,long high,long mid)
{
int i, j, k, c[50];
i=low;
j=mid+1;
k=low;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=a[j];
k++;
j++;
}
}
while(i<=mid)
{
c[k]=a[i];
k++;
i++;
}
while(j<=high)
{
c[k]=a[j];
k++;
j++;
}
for(i=low;i<k;i++)
{
a[i]=c[i];
}
}
int main()
{
freopen("in.txt","r",stdin);
long cas;
//cout<<"Size = "<<v.size()<<endl;
while(1)
{
long i;
int v[2000000];
scanf("%ld",&cas);
if(cas==0)
break;
for( i=0;i<cas;i++)
{
scanf("%d",&v[i]);
}

long mid= (long ) (0+cas-1)/2;
mergesort(v,0,cas-1);
for(i=0;i<cas;i++)
printf("%d ",v[i]);
printf("\n");
}
return 0;
}

``````

gtcoder
New poster
Posts: 12
Joined: Tue Mar 23, 2010 5:45 am

### Re: 11462 - Ages Sort

Can anyone please tell me about the fast I/O? I know fread() fwrite() is what it is. But can u give me an example of it being done?

iriz7482
New poster
Posts: 15
Joined: Mon Apr 04, 2011 3:18 pm

### Re: 11462 - Ages Sort

I've the same question as gtcoder (post above)

john_cu_cse
New poster
Posts: 5
Joined: Mon Mar 21, 2011 4:53 pm

### Re: 11462 - Ages Sort

#include<stdio.h>
int main()
{
long long int a[2000005],b,l,c,i,j,k,n;
while(scanf("%lld",&n)==1)
{
if(n==0)
break;
for(i=0;i<n;i++)
{
scanf("%lld",&a);
}
for(j=0;j<n-1;j++)
{
for(k=0;k<n-j-1;k++)
{
if(a[k]>=a[k+1])
{
c=a[k];
a[k]=a[k+1];
a[k+1]=c;
}
}
}
for(l=0;l<n;l++)
{
printf("%lld ",a[l]);
}
}
return 0;
}

//...why i got always run time error? Plz give me a solution...//

tzupengwang
New poster
Posts: 36
Joined: Fri Dec 02, 2011 1:30 pm
Location: Kaohsiung, Taiwan

### Re: 11462 - Ages Sort

the following is my AC code
in this situation, even "sort" function in C++ can't work, because of its large data(25MB)
so I use another way because the data "n" is between 1 & 100(1<=n<=100)
wish i could help~

Code: Select all

``````#include<stdio.h>
int str[100];
void a()
{
int k;
for(k=0;k<100;k++)str[k]=0;
}

int main()
{
long long int time;
while(scanf("%lld",&time)==1)
{
int flag=1;
if(time==0)flag=0;
int tim=time;
a();
while(time--)
{
int b;
scanf("%d",&b);
str[b-1]++;
}
if(!flag)break;
int i;
int p=1;
for(i=0;i<100;i++)
{
while(str[i]--){
if(p==tim)printf("%d",i+1);
else {printf("%d ",i+1);p++;}
}
}
puts("");
}
return 0;
}
``````

pollob_28
New poster
Posts: 3
Joined: Thu Apr 19, 2012 6:45 pm

### Age Sort

#include <stdio.h>
#include <math.h>
#include <conio.h>

int array [2000000];

void merge(int* input, int p, int r)
{
int mid = (int)floor((p + r) / 2);
int i1 = 0;
int i2 = p;
int i3 = mid + 1;

int temp[r-p+1];

while ( i2 <= mid && i3 <= r )
if ( input[i2] < input[i3] )
temp[i1++] = input[i2++];
else
temp[i1++] = input[i3++];

while ( i2 <= mid )
temp[i1++] = input[i2++];

while ( i3 <= r )
temp[i1++] = input[i3++];

for ( int i = p; i <= r; i++ )
input = temp[i-p];
}

void merge_sort(int* input, int p, int r)
{
if ( p < r )
{
int mid = (int)floor((p + r) / 2);
merge_sort(input, p, mid);
merge_sort(input, mid + 1, r);
merge(input, p, r);
}
}

int main (){

freopen ("age.txt", "r", stdin);
int test;

scanf ("%d", &test);

while (test !=0){

for (int i =1; i <=test; i++)
scanf ("%d", &array );

merge_sort (array, 1, test);

for (int i =1; i<=test; i++)
printf ("%d ", array );

printf ("\n");

scanf ("%d", &test);
}
getch();
return 0;
}
i don't know why i am getting presentation error

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

### Re: Age Sort

Don't print a space at the end of a line.
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

Solved this problem in JAVA in 1.848

How to solve it faster?

Code: Select all

``````import java.awt.event.PaintEvent;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.awt.geom.*;
import java.util.regex.*;
import java.util.StringTokenizer;

public class Main implements Runnable {
void solve() {
console(true);//System.getProperty("ONLINE_JUDGE") != null);

int n, last;
int[] data = new int[101];

while((n=RI())!=0){
last = 0;
for(int i=1; i<=n; i++) {
int tmp = RI();
data[tmp]++;
last = tmp>last ? tmp : last;
}

for(int i=1; i<=100; i++) {
for(int j=1; j<=data[i]; j++){
out.print((i==last && j==data[i]) ? i+"\n" : i+" ");
}

data[i] = 0;
}
}
}

StringTokenizer st;
PrintWriter out;
boolean eof = false;

public static void main(String[] args) {
}

String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}

String RLine() {
String ret;
try {
} catch (Exception e) {
ret = "";
}
if (ret == null) {
eof = true;
return null;
}
return ret;
}

int RC() {
try {
} catch (Exception e) {
return -1;
}
}

String RS() {
return nextToken();
}

int RI() {
return Integer.parseInt(nextToken());
}

long RL() {
return Long.parseLong(nextToken());
}

double RD() {
return Double.parseDouble(nextToken());
}

void console(boolean f) {
if (f) {
out = new PrintWriter(new OutputStreamWriter(System.out));
} else {
try {
File input = new File("input.txt");
if (!input.exists()) {
input.createNewFile();
}

out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt")));
} catch (Exception e) {
e.printStackTrace();
System.exit(111);
}
}
}

@Override
public void run() {
solve();
out.close();
}
}
``````