1 条题解

  • 0
    @ 2025-2-14 20:58:59

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	int i,j,n,t,x[2][105];
    	char x1[105][20];
    	while(scanf("%d",&n)==1&&n!=0)
    	{
    		memset(x,0,sizeof(x));
    		memset(x1,0,sizeof(x1));
    		for(i=1;i<=n;i++)
    			scanf("%s%d%d",x1[i],&x[0][i],&x[1][i]);
    		for(i=1;i<n;i++)\
    			for(j=i+1;j<=n;j++)
    			{
    				if(x[0][i]>x[0][j])
    				{
    					t=x[0][i];x[0][i]=x[0][j];x[0][j]=t;
    					t=x[1][i];x[1][i]=x[1][j];x[1][j]=t;
    					strcpy(x1[0],x1[i]);
    					strcpy(x1[i],x1[j]);
    					strcpy(x1[j],x1[0]);
    				}
    				else
    				if(x[0][i]==x[0][j]&&x[1][i]<x[1][j])
    				{
    					t=x[0][i];x[0][i]=x[0][j];x[0][j]=t;
    					t=x[1][i];x[1][i]=x[1][j];x[1][j]=t;
    					strcpy(x1[0],x1[i]);
    					strcpy(x1[i],x1[j]);
    					strcpy(x1[j],x1[0]);
    				}
    			}
    		for(i=1;i<=n;i++)
    			printf("%s:%d %d\n",x1[i],x[0][i],x[1][i]);
    	}
    	return 0;
    }
    

    C++ :

    #include<iostream>
    #include <algorithm>
    using namespace std;
    #define N 100
    struct student
    {
    	string name;
    	int cpp;
    	int math;
    }stu[N];
    bool cmp(struct student x,struct student y)
    {
    	if(x.cpp!=y.cpp) 
    		return x.cpp<y.cpp;
    	return x.math>y.math;
    }
    int main()
    {
    	int n,i;
    	cin>>n;
    	for(i=0;i<n;i++)
    	{
    		cin>>stu[i].name>>stu[i].cpp>>stu[i].math;
    	}
    	sort(stu,stu+n,cmp);
    	for(i=0;i<n;i++)
    	{
    		cout<<stu[i].name<<":"<<stu[i].cpp<<' '<<stu[i].math<<endl;
    	}
    	return 0;                    
    }
    
    

    Pascal :

    program hehe;
      var
        n,i,j,s:longint;
        b,c:array[1..1001]of longint;
        a:array[1..1001]of string;
        l:array[0..1001]of char;
        t:string; 
    
    procedure swap(var a,b:longint);
      var
        t:longint;
      begin
        t:=a;
        a:=b;
        b:=t;
      end;
    
      begin
      while not eof do
      begin
        readln(n);
        for i:=1 to n do
          begin
            s:=0;
            while l[s]<>' ' do
              begin
                inc(s);
                read(l[s]);
              end;
            readln(b[i],c[i]);
            for j:=1 to s-1 do
              a[i]:=a[i]+l[j];
          end;
        for i:=1 to n-1 do
          for j:=1 to n-i do
            if c[j]<c[j+1]then
              begin
                t:=a[j];
                a[j]:=a[j+1];
                a[j+1]:=t;
                swap(b[j],b[j+1]);
                swap(c[j],c[j+1]);
              end;
        for i:=1 to n-1 do
          for j:=1 to n-i do
            if b[j]>b[j+1]then
              begin
                t:=a[j];
                a[j]:=a[j+1];
                a[j+1]:=t;
                swap(b[j],b[j+1]);
                swap(c[j],c[j+1]);
              end;
        {for i:=1 to n-1 do
          for j:=1 to n-i do
            if a[j]>a[j+1]then
              begin
                swap(a[j],a[j+1]);
                swap(b[j],b[j+1]);
                swap(c[j],c[j+1]);
              end;}
        for i:=1 to n do
          writeln(a[i],':',b[i],' ',c[i]);
      end;
      end.
    
    

    Java :

    import java.util.*;
    
     class k implements Comparable<k>{
       public  String s;
       public Integer a;
      public   Integer b;
        k(String s1,int a1,int b1)
        {
            s=s1;a=a1;b=b1;
        }
        public int compareTo(k o) {          //实现接口
              int x=a.compareTo(o.a);
              if(x>0)
                  return 1;
              else if(x<0)
                  return -1;
              else
              return -b.compareTo(o.b);
          }   
        public void tostring()
        {
            System.out.println(s+":"+a+" "+b);
        }
    }
    public  class Main {
       public static void  main(String[] args)
       {
           java.util.Scanner in=new java.util.Scanner(System.in);
           while(in.hasNext())
           {
               int n=in.nextInt();
               k[] w=new k[n];
               for(int i=0;i<n;i++)
               {
                   String s=in.next();
                   int a=in.nextInt();
                   int b=in.nextInt();
                   w[i]=new k(s,a,b);
               }
               Arrays.sort(w);
               for(int i=0;i<n;i++)
               {
                   w[i].tostring();
               }
           }
       }
    }
    
    • 1

    信息

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