Java面向物件6 (AA - AE)
阿新 • • 發佈:2018-11-19
整理音樂(SDUT 2053)
import java.util.*; public class Main { public static void main(String[] args) { Scanner reader=new Scanner(System.in); int n=reader.nextInt(); String string[] =new String[10001]; int A[]=new int [10001]; int flag=0; for(int i=1;i<=n;i++) { int m=reader.nextInt(); for(int j=1;j<=m;j++) { string[flag]=reader.next(); A[flag]=reader.nextInt(); flag++; } } for(int i=0;i<flag-1;i++) { for(int j=0;j<flag-1-i;j++) { if(A[j]<A[j+1]||(A[j]==A[j+1]&&string[j].compareTo(string[j+1])>0)) { int t=A[j]; A[j]=A[j+1]; A[j+1]=t; String t1; t1=string[j]; string[j]=string[j+1]; string[j+1]=t1; } } } for(int i=0;i<flag;i++) { if(i!=flag-1) System.out.print(string[i]+" "); else { System.out.println(string[i]); } } reader.close(); } }
AD 來淄博旅遊(SDUT 2055)
import java.util.Scanner; class List { String from[][] = new String[105][2105]; String to[][] = new String[105][2105]; int len1 = 0, len2 = 0; int lens1[] = new int[105]; int lens2[] = new int[105]; List(int x) { to[0][0] = "zichuan"; to[1][0] = "linzi"; to[2][0] = "zhoucun"; to[3][0] = "boshan"; len2 = 4; lens2[0] = lens2[1] = lens2[2] = lens2[3] = 1; } int fin(String a[][], int len, String x) { int i; for(i = 0; i < len; i++) { if(a[i][0].equals(x) == true)break; } if(i < len)return i; else return -1; } void SetData(String nam, String fro, String ti) { int p; p = fin(from, len1, fro); if(p == -1) { from[len1][0] = fro; from[len1][1] = nam; lens1[len1] = 2; len1++; } else { from[p][lens1[p]] = nam; lens1[p]++; } p = fin(to, len2, ti); if(p != -1) { to[p][lens2[p]] = nam; lens2[p]++; } } void Print() { int i, j; for(i = 0; i < len1; i++) { System.out.print(from[i][0] + " :"); for(j = 1; j < lens1[i]; j++) { System.out.print(" " + from[i][j]); } System.out.println(); } for(i = 0; i < len2; i++) { System.out.print(to[i][0] + " :"); for(j = 1; j < lens2[i]; j++) { System.out.print(" " + to[i][j]); } System.out.println(); } } } public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n, i, j, k; String nam, fro, to; List li = new List(1); n = cin.nextInt(); nam = cin.nextLine(); for(i = 0; i < n; i++) { nam = cin.next(); fro = cin.next(); to = cin.next(); li.SetData(nam, fro, to); } li.Print(); cin.close(); } }