sort(水题)

sort

Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 39400    Accepted Submission(s): 11450

Problem Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。
 
Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
 
Output
对每组测试数据按从大到小的顺序输出前m大的数。
 
Sample Input
5 3 3 -35 92 213 -644
 
Sample Output
213 92 3
Hint
Hint
请用VC/VC++提交
 
Author
LL
 
Source
题解:好疑惑,自己的代码有毒,在hdoj能过,virtrual judge 就不行,c#也老是wa。。。
c代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 1000010;
int a[MAXN];
int cmp(int x,int y){
    return x > y;
}
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        for(int i = 0;i < n;i++){
            scanf("%d",&a[i]);
        }
        sort(a,a + n, cmp);
        for(int i = 0; i < m;i++){
            if(i)printf(" ");
            printf("%d",a[i]);
        }
        puts("");
    }
    return 0;
}

C#  wa了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace sort
{
    class Program
    {
        static int input()
        {
            int x = 0, k = 1;
            int temp;
            while (true)
            {
                temp = Console.Read();
                if (temp >= '0' && temp <= '9')
                    break;
                else if(temp == '-')
                {
                    k = -1;
                    temp = '0';
                    break;
                }
            }
            while (true)
            {
                x = x * 10 + temp - '0';
                temp = Console.Read();
                if (temp < '0' || temp > '9') break;
            }
          //  Console.WriteLine("{0}",x * k);
            return x * k;
        }
        static void Main(string[] args)
        {
            int n, m;
            n = input();
            m = input();
            ArrayList arr = new ArrayList();
            arr.Clear();
            while (n-- > 0)
            {
                int x;
                x = input();
                arr.Add(x);
            }
            arr.Sort();
            for(int i = 1; i <= m; i++)
            {
                if (i != 1) Console.Write(" ");
                Console.Write("{0}",arr[arr.Count - i]);
            }
            Console.WriteLine();
            //while (true) ;
        }
    }
}
原文地址:https://www.cnblogs.com/handsomecui/p/5355302.html