1 条题解

  • 0
    @ 2021-6-15 1:39:21

    C :

    #include <stdio.h>
    #include <stdlib.h>
    
    int a[201000];
    int cmp(const void *a,const void *b)
    {
        return *(int *)a-*(int *)b;
    }
    int main()
    {
    	int i,n;
        scanf("%d",&n);
        for (i=0;i<n;i++)
    	{
            scanf("%d",&a[i]);
        }
        qsort(a,n,sizeof(int),cmp);
        int count=0,s=0;
        for (i=0;i<n;i++)
    	{
            if (i==0)
    		{
                 s=a[i];
                 count++;
                 continue;
            }
            if (s!=a[i])
    		{
                 printf("%d %d\n",s,count);
                 s=a[i];
                 count=1;
            }
    		else if (s==a[i]) 
    			count++;
        }
        printf("%d %d\n",s,count);
        return 0;
    }
    

    C++ :

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    long long a[200010];
    int main()
    {
    	int n,i,count;
    	long long t;
    	while(scanf("%d",&n)!=EOF)
    	{
    		for(i=0;i<n;i++)
    			scanf("%lld",&a[i]);
    		sort(a,a+n);
    		t=a[0],count=0;
    		for(i=0;i<n;i++)
    		{
    			if(a[i]==t)
    			{
    				count++;
    			}
    			else
    			{
    				printf("%lld %d\n",t,count);
    				t=a[i];
    				count=1;
    			}
    			if(i==(n-1))
    				printf("%lld %d\n",t,count);
    		}
    	}
    	return 0;
    }
    

    Pascal :

    var
      n,i,t,k:longint;
      a:array[1..200000] of longint;
      procedure qsort(l,r:longint);
      var
        i,j,mid,tmp:longint;
      begin
        i:=l;
        j:=r;
        mid:=a[(l+r) div 2];
        repeat
          while a[i]<mid do inc(i);
          while a[j]>mid do dec(j);
          if i<=j then
          begin
            tmp:=a[i];
            a[i]:=a[j];
            a[j]:=tmp;
            inc(i);
            dec(j);
          end;
        until i>j;
        if i<r then qsort(i,r);
        if l<j then qsort(l,j);
      end;
    begin
      read(n);
      for i:=1 to n do readln(a[i]);
      qsort(1,n);
      i:=1;
      while i<=n do
      begin
        write(a[i],' ');
        t:=0;k:=a[i];
        while (i<=n)and(a[i]=k) do
        begin
          inc(t);inc(i);
        end;
        writeln(t);
      end;
    end.
    
    

    Java :

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Scanner;
    import java.util.Set;
    
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner s = new Scanner(System.in);
    		int count = s.nextInt();
    		HashMap<Long, Integer> map = new HashMap<Long, Integer>();
    		for(int i=0;i<count;i++){
    			long num = s.nextLong();
    			if(map.containsKey(num)){//已经有该数据了
    				map.put(num, map.get(num)+1);
    			}
    			else{
    				map.put(num, 1);
    			}
    		}
    		//已经完成了记数,接下来排序
    		Set set=map.keySet();
            Object[] arr=set.toArray();
            Arrays.sort(arr);
            for(Object key:arr){
                System.out.println(key+" "+map.get(key));
            }
    		
    	}
    }
    
    
    • 1

    信息

    ID
    257
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者