JOJ1202。重新操刀ACM,一天一练!做个简单的题目温习。

http://ac.jobdu.com/problem.php?pid=1202

题目描述:

    对输入的n个数进行排序并输出。

输入:

    输入的第一行包括一个整数n(1<=n<=100)。
    接下来的一行包括n个整数。

输出:

    可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格。
    每组测试数据的结果占一行。

样例输入:
4
1 4 3 2
样例输出:
1 2 3 4 
来源:
2006年华中科技大学计算机保研机试真题
#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
    freopen("in.txt","r",stdin);
    int n;
    while(scanf("%d", &n)!=EOF && n)
    {
        int a[102];
        for (int i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
        }
        sort(a,a + n);
        for (int j=0; j<n; j++)
        {
            printf("%d ",a[j]);
        }
        printf("
");
    }

    return 0;
}

in.txt

4
1 4 3 2
6
3 5 7 2 9 4
8
12 34 67 89 54 67 89 32

java实现提交。想要Accept,必须把类名改为Main,去掉包package

package j1;//需要注释

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class J1202 {//类名改为Main

    public static void main(String[] args) throws FileNotFoundException {
        //输入重定向,以下两行注释
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1202.txt"));
        System.setIn(bis);
        
        Scanner in = new Scanner(System.in);
        
        int n;
        while (in.hasNext()) {
            n = in.nextInt();
            List<Integer> list = new ArrayList<Integer>();
            for (int i=0; i<n; i++){
                list.add(in.nextInt());
            }
            Collections.sort(list);
            for (int e : list) {
                System.out.print(e + " ");
            }
            System.out.println();
        }
    }

}
原文地址:https://www.cnblogs.com/549294286/p/3178883.html