【九度OJ合集】P1000-P1049

P1000

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2.   
  3.   
  4. public class Exercise1000 {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         Scanner sc = new Scanner(System.in);  
  8.         while(sc.hasNext())  
  9.         {  
  10.             System.out.println(sc.nextInt()+sc.nextInt());  
  11.         }  
  12.     }  
  13. }  


P1001

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2.   
  3. public class Exercise1001 {  
  4.     public static void main(String[] args) {  
  5.         int[][] a;  
  6.         int[][] b;  
  7.         int[][] c;  
  8.         Scanner sc = new Scanner(System.in);  
  9.   
  10.         while (sc.hasNext()) {  
  11.             int row = sc.nextInt();  
  12.             if (row == 0)  
  13.                 break;  
  14.             int col = sc.nextInt();  
  15.   
  16.             a = new int[row][col];  
  17.             b = new int[row][col];  
  18.             c = new int[row][col];  
  19.   
  20.             for (int i = 0; i < row; i++)  
  21.                 for (int j = 0; j < col; j++)  
  22.                     a[i][j] = sc.nextInt();  
  23.   
  24.             for (int i = 0; i < row; i++)  
  25.                 for (int j = 0; j < col; j++)  
  26.                     b[i][j] = sc.nextInt();  
  27.   
  28.             for (int i = 0; i < row; i++)  
  29.                 for (int j = 0; j < col; j++)  
  30.                     c[i][j] = a[i][j] + b[i][j];  
  31.   
  32.             int sum = 0;  
  33.   
  34.             for (int i = 0; i < row; i++) {  
  35.                 for (int j = 0; j < col; j++) {  
  36.                     if (c[i][j] != 0)  
  37.                         break;  
  38.                     if (j == col - 1)//特别注意  
  39.                         sum++;  
  40.                 }  
  41.   
  42.             }  
  43.   
  44.             for (int j = 0; j < col; j++) {  
  45.                 for (int i = 0; i < row; i++) {  
  46.                     if (c[i][j] != 0)  
  47.                         break;  
  48.                     if (i == row - 1)  
  49.                         sum++;  
  50.                 }  
  51.   
  52.             }  
  53.   
  54.             System.out.println(sum);  
  55.   
  56.         }  
  57.     }  
  58. }  

P1002

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Exercise1002 {
	public static void main(String[] args) {
		Scanner sc =new Scanner(new BufferedInputStream(System.in));
		while(sc.hasNext())
		{
			double p=sc.nextDouble();
			double t=sc.nextDouble();
			double G1 = sc.nextDouble();
			double G2 = sc.nextDouble();
			double G3=sc.nextDouble();
			double GJ=sc.nextDouble();
			
			double grade=0;
			
			if(Math.abs(G1-G2)<=t)
				grade=(G1+G2)/2;
			else if((Math.abs(G1-G3)<=t&&Math.abs(G2-G3)>t)||(Math.abs(G1-G3)>t&&Math.abs(G2-G3)<=t))
			
				grade=((Math.abs(G1-G3)>Math.abs(G3-G2)?G2:G1)+G3)/2;
			else if(Math.abs(G1-G3)<=t&&Math.abs(G2-G3)<=t)
				
				grade=(G1>G2?G1:G2)>G3?(G1>G2?G1:G2):G3;
			else if(Math.abs(G1-G3)>t && Math.abs(G2-G3)>t)
				grade=GJ;
			
			System.out.printf("%.1f", grade);
			System.out.println();
		}
	}
}



P1003

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.math.BigInteger;  
  2. import java.util.Scanner;  
  3.   
  4. public class Exercise1003_2 {  
  5.     public static void main(String[] args) {  
  6.         Scanner sc = new Scanner(System.in);  
  7.         while (sc.hasNext()) {  
  8.             String[] a = sc.next().split(",");  
  9.             String[] b = sc.next().split(",");  
  10.   
  11.             System.out.println(big(a).add(big(b)));  
  12.         }  
  13.     }  
  14.   
  15.     private static BigInteger big(String[] a) {  
  16.         String sa = "";  
  17.         for (String s : a)  
  18.             sa += s;  
  19.         return new BigInteger(sa);  
  20.     }  
  21.   
  22. }  

如果模拟数组实现,一位一位做

P1004

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class Exercise1004 {
	public static void main(String[] args)
	{
		Scanner sc =new Scanner(new BufferedInputStream(System.in));
		while(sc.hasNext())
		{
			ArrayList<Integer> arr=new ArrayList<Integer>();
			
			int m=sc.nextInt();
	
			for(int i=0;i<m;i++)
				arr.add(sc.nextInt());
			
			int n=sc.nextInt();
			for(int i=0;i<n;i++)
				arr.add(sc.nextInt());
			
			
			Collections.sort(arr);
			System.out.println(arr.get((m+n-1)/2));
			
		}
	}
}
排序解

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Exercise1004_2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(new BufferedInputStream(System.in));
		while (sc.hasNext()) {
			int m = sc.nextInt();
			int[] a = new int[m];

			for (int i = 0; i < m; i++)
				a[i] = sc.nextInt();

			int n = sc.nextInt();

			int[] b = new int[m + n];

			for (int i = 0; i < m; i++)
				b[i] = a[i];

			for (int i = m; i < m + n; i++)
				b[i] = sc.nextInt();

			System.out.println(select(b, (m + n - 1) / 2));
		}
	}

	public static int select(int[] a, int k) {
		int lo = 0, hi = a.length - 1;
		while (lo < hi) {
			int j = partition(a, lo, hi);
			if (j == k)
				return a[k];
			else if (j < k)
				lo = j + 1;
			else if (j > k)
				hi = j - 1;

		}
		return a[k];
	}

	public static int partition(int[] a, int lo, int hi) {
		int i = lo, j = hi + 1;
		while (true) {
			while (a[++i] < a[lo])
				if (i == hi)
					break;
			while (a[lo] < a[--j])
			if (j == lo)
				break;
			if (i >= j)
				break;
			exch(a, i, j);
		}
		exch(a, lo, j);
		return j;

	}

	public static void exch(int[] a, int i, int j) {
		a[i] ^= a[j];
		a[j] ^= a[i];
		a[i] ^= a[j];
	}

}

利用快排求中位数(平台过不了异或操作,可以用变量交换)

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Exercise1004_3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(new BufferedInputStream(System.in));
		while (sc.hasNext()) {
			int m = sc.nextInt();
			int[] a = new int[m];

			for (int i = 0; i < m; i++)
				a[i] = sc.nextInt();

			int n = sc.nextInt();

			int[] b = new int[n];

			for (int i = 0; i < n; i++)
				b[i] = sc.nextInt();

			int[] c = new int[m + n];
			int i = 0, j = 0, k = 0;
			while (i != m && j != n) {
				if (a[i] > b[j])
					c[k++] = b[j++];
				else
					c[k++] = a[i++];
			}

			while (k < m + n) {
				if (i == m)
					while (j != n)
						c[k++] = b[j++];
				else
					while (i != m)
						c[k++] = a[i++];
			}

			System.out.println(c[(m + n - 1) / 2]);

		}
	}
}

直接归并



P1008

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package Exercise1008;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.util.LinkedList;  
  5. import java.util.Scanner;  
  6.   
  7. public class Exercise_2 {  
  8.       
  9.     public static void main(String[] args)  
  10.     {  
  11.         Scanner sc = new Scanner(new BufferedInputStream(System.in));  
  12.         while(sc.hasNext())  
  13.         {  
  14.             int n=sc.nextInt();  
  15.             int m=sc.nextInt();  
  16.             if(m==0&&n==0)  
  17.                 System.exit(0);  
  18.             Vertex[] a=new Vertex[n+1];  
  19.             for(int i=1;i<=n;i++)  
  20.                 a[i] =new Vertex(i,Double.MAX_VALUE,Double.MAX_VALUE);  
  21.               
  22.             double[] edgeDis =new double[n+1];  
  23.             double[] edgePri =new double[n+1];  
  24.               
  25.             for(int i=0;i<m;i++)  
  26.             {  
  27.                 int v=sc.nextInt();  
  28.                 int w=sc.nextInt();  
  29.                 int d=sc.nextInt();  
  30.                 int p=sc.nextInt();  
  31.                 a[v].adj.add(new Edge1(w,d,p));  
  32.                 a[w].adj.add(new Edge1(v,d,p));  
  33.                   
  34.                   
  35.             }  
  36.             IndexMinPQ<Vertex> pq=new IndexMinPQ<Vertex>(n);  
  37.             int s=sc.nextInt();  
  38.             int t =sc.nextInt();  
  39.             a[s].dist=0.0;  
  40.             a[s].price=0.0;  
  41.             pq.add(s, a[s]);  
  42.   
  43.             while(!pq.isEmpty()&&!a[t].marked)  
  44.             {  
  45.                 int v = pq.delMin();  
  46.                   
  47.                 a[v].marked=true;  
  48.                 for(Edge1 e:a[v].adj)  
  49.                 {  
  50.                     int w=e.otherVer;  
  51.                       
  52.                     if(a[w].marked)  
  53.                         continue;  
  54.                     if((a[w].dist>a[v].dist+e.edgeDis)||(a[w].dist==a[v].dist+e.edgeDis&&a[w].price>a[v].price+e.edgePri))  
  55.                     {  
  56.                         a[w].dist=a[v].dist+e.edgeDis;  
  57.                         a[w].price=a[v].price+e.edgePri;  
  58.                     }  
  59.   
  60.                     if(pq.contains(w))  
  61.                         pq.change(w,a[w]);  
  62.                     else  
  63.                         pq.add(w, a[w]);  
  64.                   
  65.                           
  66.                 }  
  67.             }  
  68.             System.out.println((int)a[t].dist+" "+(int)a[t].price);   
  69.               
  70.         }  
  71.     }  
  72. }  
  73.   
  74. class Edge1{  
  75.     public int otherVer;  
  76.     public double edgeDis;  
  77.     public double edgePri;  
  78.     public Edge1(int otherVer,double edgeDis,double edgePri)  
  79.     {  
  80.         this.otherVer = otherVer;  
  81.         this.edgeDis = edgeDis;  
  82.         this.edgePri =edgePri;  
  83.     }  
  84.       
  85. }  
  86.   
  87. class Vertex implements Comparable<Vertex>{  
  88.     public double dist;  
  89.     public double price;  
  90.     public LinkedList<Edge1> adj ;  
  91.     public int topNum;  
  92.     public boolean marked;  
  93.       
  94.     public Vertex(int topNum,double dist,double price)  
  95.     {  
  96.         this.topNum=topNum;  
  97.         this.dist =dist;  
  98.         this.price =price;  
  99.         adj =new LinkedList<Edge1>();  
  100.         marked=false;  
  101.     }  
  102.     @Override  
  103.     public int compareTo(Vertex v) {  
  104.         if(this.dist>v.dist)  
  105.             return 1;  
  106.         else if(this.dist <v.dist)  
  107.             return -1;  
  108.         else if(this.price>v.price)  
  109.             return 1;  
  110.         else if(this.price<v.price)  
  111.             return -1;  
  112.         return 0;  
  113.     }  
  114.   
  115.       
  116.       
  117. }  
  118. class IndexMinPQ <E extends Comparable<E>>{  
  119.       
  120.     private int N;  
  121.     private int[] pq;  
  122.     private int[] qp;  
  123.     private E[] keys;  
  124.       
  125.     public IndexMinPQ(int maxN)  
  126.     {  
  127.         keys = (E[]) new Comparable[maxN+1];  
  128.         pq= new int[maxN+1];  
  129.         qp=new int[maxN+1];  
  130.         for(int i=0;i<=maxN;i++)  
  131.             qp[i] =-1;  
  132.     }  
  133.       
  134.     //插入元素  
  135.     public void add(int k,E e)  
  136.     {  
  137.         N++;  
  138.         pq[N]=k;  
  139.         keys[k] =e;  
  140.         qp[k] =N;  
  141.         swim(N);  
  142.     }  
  143.       
  144.     public void change(int k,E e)  
  145.     {  
  146.         keys[k] =e;  
  147.         swim(qp[k]);  
  148.         sink(qp[k]);  
  149.     }  
  150.       
  151.     public boolean contains(int k)  
  152.     {  
  153.         return qp[k]!=-1;  
  154.     }  
  155.       
  156.     public int delMin()  
  157.     {  
  158.         int indexOfMin=pq[1];  
  159.         exch(1,N--);//如果不先减小N,则元素会沉到N  
  160.         sink(1);  
  161.         keys[pq[N+1]]=null;//help GC  
  162.         qp[pq[N+1]]=-1;  
  163.           
  164.         return indexOfMin;  
  165.     }  
  166.       
  167.     public boolean isEmpty()  
  168.     {  
  169.         return N==0;  
  170.     }  
  171.       
  172.     private boolean less(int i,int j)  
  173.     {  
  174.         return keys[pq[i]].compareTo(keys[pq[j]])<0;  
  175.           
  176.     }  
  177.       
  178.     private void exch(int i,int j)  
  179.     {  
  180.           
  181.         int temp=pq[i];       
  182.         pq[i]=pq[j];  
  183.         pq[j] =temp;  
  184.           
  185.         qp[pq[i]] =i;  
  186.         qp[pq[j]] =j;  
  187.           
  188.                   
  189.     }  
  190.       
  191.     private void swim(int k)  
  192.     {  
  193.   
  194.         while(k>1&&less(k,k/2))  
  195.         {  
  196.             exch(k/2,k);  
  197.             k=k/2;  
  198.         }  
  199.     }  
  200.       
  201.     private void sink(int k)  
  202.     {  
  203.         while(2*k<=N)  
  204.         {  
  205.             int j=2*k;  
  206.             while(j<N&&less(j+1,j))  
  207.                 j++;  
  208.             if(less(k,j))  
  209.                 break;  
  210.             exch(k,j);  
  211.             k=j;  
  212.         }  
  213.     }  
  214.       
  215.     public E min()  
  216.     {  
  217.         return keys[pq[1]];  
  218.     }  
  219.       
  220.     public int size()  
  221.     {  
  222.         return N;  
  223.     }  
  224.   
  225. }  

prim算法+索引优先队列


P1009

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2.   
  3. public class Exercise1009 {  
  4.   
  5.     // public static void main(String[] args) {  
  6.     // Scanner sc = new Scanner(System.in);  
  7.     // int n;  
  8.     // while ((n = Integer.parseInt(sc.nextLine())) != 0) {  
  9.     //  
  10.     // String s = sc.nextLine();  
  11.     //  
  12.     // for (int i = 0; i < n; i++) {  
  13.     // String a = sc.nextLine();  
  14.     //  
  15.     // if (isSameTree(s, a))  
  16.     // System.out.println("Yes");  
  17.     // else  
  18.     // System.out.println("NO");  
  19.     //  
  20.     // }  
  21.     // }  
  22.     // }  
  23.     //  
  24.     // private static boolean isSameTree(String s, String a) {  
  25.     // if (s.equals(a))  
  26.     // return true;  
  27.     //  
  28.     // char[] chars = s.toCharArray();  
  29.     // char[] chara = a.toCharArray();  
  30.     //  
  31.     // if (s.length() != a.length() || chars[0] != chara[0]) {  
  32.     // return false;  
  33.     // } else {  
  34.     //  
  35.     // String[] subTrees = subTree(s);  
  36.     // String[] subTreea = subTree(a);  
  37.     //  
  38.     // return isSameTree(subTrees[0], subTreea[0])  
  39.     // && isSameTree(subTrees[1], subTreea[1]);  
  40.     // }  
  41.     // }  
  42.     //  
  43.     // private static String[] subTree(String str) {  
  44.     // String[] a = { "", "" };  
  45.     // for (int i = 1; i < str.length(); i++) {  
  46.     // char j = str.charAt(i);  
  47.     // if (j > str.charAt(0))  
  48.     // a[0] += j;  
  49.     // else  
  50.     // a[1] += j;  
  51.     //  
  52.     // }  
  53.     // return a;  
  54.     // }  
  55.   
  56.     public static void main(String[] args) {  
  57.         Scanner sc = new Scanner(System.in);  
  58.         int n;  
  59.           
  60.         while ((n = Integer.parseInt(sc.nextLine())) != 0) {  
  61.   
  62.             String s = sc.nextLine();  
  63.   
  64.             int[] s1 = creatTree(s);  
  65.   
  66.             for (int i = 0; i < n; i++) {  
  67.                   
  68.                 boolean flag = true;  
  69.                 String a = sc.nextLine();  
  70.                 int[] a1 = creatTree(a);  
  71.   
  72.                 for (int j = 1; j < s1.length; j++)  
  73.                     if (s1[j] != a1[j]) {  
  74.                         flag = false;  
  75.                         break;  
  76.                     }  
  77.   
  78.                 if (flag)  
  79.                     System.out.println("YES");  
  80.                 else  
  81.                     System.out.println("NO");  
  82.             }  
  83.         }  
  84.     }  
  85.   
  86.     private static int[] creatTree(String s) {  
  87.         int[] a = new int[1025];  
  88.         for (int i = 0; i < a.length; i++)  
  89.             a[i] = -1;  
  90.         a[1] = Integer.parseInt(s.charAt(0) + "");  
  91.         for (int i = 1; i < s.length(); i++) {  
  92.             int k = Integer.parseInt(s.charAt(i) + "");  
  93.             int j = 1;  
  94.             while (a[j] != -1) {  
  95.                 if (k > a[j])  
  96.                     j = j * 2 + 1;  
  97.                 else  
  98.                     j = j * 2;  
  99.             }  
  100.             a[j] = k;  
  101.   
  102.         }  
  103.         return a;  
  104.     }  
  105.   
  106. }  

第一种方法为什么错未知,第二种没有构造二叉树,用了静态数组

注意flag定义的位置,如果定义在while外面,记得加else


P1010

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Hashtable;  
  2. import java.util.Scanner;  
  3.   
  4. public class Exercise1010 {  
  5.   
  6.     Hashtable<String, String> ht;  
  7.   
  8.     public Exercise1010() {  
  9.         ht = new Hashtable<String, String>();  
  10.         ht.put("zero""0");  
  11.         ht.put("one""1");  
  12.         ht.put("two""2");  
  13.         ht.put("three""3");  
  14.         ht.put("four""4");  
  15.         ht.put("five""5");  
  16.         ht.put("six""6");  
  17.         ht.put("seven""7");  
  18.         ht.put("eight""8");  
  19.         ht.put("nine""9");  
  20.     }  
  21.   
  22.     public static void main(String[] args) {  
  23.         Exercise1010 ex = new Exercise1010();  
  24.         Scanner sc = new Scanner(System.in);  
  25.   
  26.         while (sc.hasNext()) {  
  27.             String str = sc.nextLine();  
  28.             int j = str.indexOf('+');  
  29.             int k = str.indexOf('=');  
  30.             String[] sub1 = str.substring(0, j).split(" ");  
  31.             String[] sub2 = str.substring(j + 2, k).split(" ");  
  32.             int sum = ex.value(sub1) + ex.value(sub2);  
  33.             if (sum == 0)  
  34.                 System.exit(0);  
  35.             System.out.println(ex.value(sub1) + ex.value(sub2));  
  36.   
  37.         }  
  38.     }  
  39.   
  40.     private int value(String[] sub) {  
  41.         String str = "";  
  42.         for (String s : sub)  
  43.             str += this.ht.get(s);  
  44.   
  45.         return Integer.parseInt(str);  
  46.     }  
  47. }  

P1011

import java.util.Scanner;

public class Exercise1011 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int k = sc.nextInt();
			if (k == 0)
				System.exit(0);
			int[] a = new int[k];
			for (int i = 0; i < k; i++)
				a[i] = sc.nextInt();

			int sum = 0;
			int b = 0;
			int lo = 0;
			int lob = 0;
			int hi = 0;
			boolean flag = false;

			for (int i = 0; i < k; i++) {
				if (a[i] >= 0)
					flag = true;
				if (b > 0) {
					b += a[i];

				} else {
					b = a[i];
					lob = i;
				}
				if (sum <= b) {
					sum = b;
					hi = i;
					lo = lob;
				}
			}

			if (!flag) {
				lo = 0;
				hi = k - 1;
				sum = 0;
			}
			while (a[hi] == 0 && hi >= 0 && sum > 0)
				hi--;
			System.out.println(sum + " " + a[lo] + " " + a[hi]);
		}
	}
}

动态规划解最大子序列


P1014

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. import java.util.Scanner;  
  4.   
  5. public class Exercise1014 {  
  6.     public static void main(String[] args) {  
  7.         Scanner sc = new Scanner(System.in);  
  8.         while (sc.hasNext()) {  
  9.             int n = sc.nextInt();  
  10.             if (0 == n)  
  11.                 System.exit(0);  
  12.             int m = sc.nextInt();  
  13.             int g = sc.nextInt();  
  14.   
  15.             int[] score = new int[m + 1];  
  16.             int count = 0;  
  17.             score[0] = 0;  
  18.             for (int i = 1; i <= m; i++)  
  19.                 score[i] = sc.nextInt();  
  20.             sc.nextLine();  
  21.   
  22.             ArrayList<Student> arr = new ArrayList<Student>();  
  23.   
  24.             for (int i = 0; i < n; i++) {  
  25.   
  26.                 String[] s = sc.nextLine().split(" ");  
  27.                 int sum = 0;  
  28.   
  29.                 for (int j = 2; j < s.length; j++)  
  30.   
  31.                     sum += score[Integer.parseInt(s[j])];  
  32.   
  33.                 if (sum >= g) {  
  34.                     count++;  
  35.                     Student student = new Student(s[0], sum);  
  36.                     arr.add(student);  
  37.                 }  
  38.             }  
  39.   
  40.             Collections.sort(arr);  
  41.             System.out.println(count);  
  42.             for (Student student : arr)  
  43.                 System.out.println(student);  
  44.   
  45.         }  
  46.     }  
  47.   
  48. }  
  49.   
  50. class Student implements Comparable {  
  51.     private String id;  
  52.     private int score;  
  53.   
  54.     public Student(String id, int score) {  
  55.         this.id = id;  
  56.         this.score = score;  
  57.     }  
  58.   
  59.     @Override  
  60.     public int compareTo(Object o) {  
  61.         Student t = (Student) o;  
  62.         if (this.score < t.score)  
  63.             return 1;  
  64.         else if (this.score > t.score)  
  65.             return -1;  
  66.         else  
  67.             return this.id.compareTo(t.id);  
  68.     }  
  69.   
  70.     public String toString() {  
  71.         return this.id + " " + this.score;  
  72.     }  
  73. }  

注意nextInt后调用nextLine,要先跳过本行

注意重写compareTo方法


P1016

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2.   
  3.   
  4. public class Exercise1016 {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};  
  8.         Scanner sc=new Scanner(System.in);  
  9.         while(sc.hasNext())  
  10.         {  
  11.             String input1=sc.next();  
  12.             String input2=sc.next();  
  13.             if("0".equals(input1)&&"0".equals(input2))  
  14.                 System.exit(0);  
  15.               
  16.             int[] a=str(input1.split(","));  
  17.             int[] b=str(input2.split(","));  
  18.             int[] c=new int[26];  
  19.             for(int i=0;i<25;i++)  
  20.             {  
  21.                 c[i]+=a[i]+b[i];  
  22.                 if(c[i]>=prime[i])  
  23.                 {  
  24.                     c[i+1]=c[i]/prime[i];  
  25.                     c[i]=c[i]%prime[i];  
  26.                       
  27.                 }  
  28.             }  
  29.             boolean flag=false;  
  30.             for(int i=25;i>0;i--)  
  31.             {  
  32.                 if(c[i]!=0)  
  33.                     flag=true;  
  34.                 if(flag)  
  35.                 System.out.print(c[i]+",");  
  36.             }  
  37.             System.out.print(c[0]);  
  38.             System.out.println();  
  39.               
  40.               
  41.         }  
  42.     }  
  43.       
  44.     public static int[] str(String[] s)  
  45.     {  
  46.         int[] str=new int[26];  
  47.         for(int i=0;i<s.length;i++)  
  48.             str[i]=Integer.parseInt(s[s.length-1-i]);  
  49.         return str;  
  50.     }  
  51. }  



P1020

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. import java.util.Scanner;  
  4.   
  5. public class Exercise1020 {  
  6.   
  7.     public static void main(String[] args) {  
  8.         ArrayList xarr;  
  9.         ArrayList yarr;  
  10.         Scanner sc = new Scanner(System.in);  
  11.   
  12.         while (sc.hasNext()) {  
  13.             int a = sc.nextInt();  
  14.             int b = sc.nextInt();  
  15.             if (a == 0 && b == 0)  
  16.                 System.exit(0);  
  17.             xarr = new ArrayList();  
  18.             yarr = new ArrayList();  
  19.             xarr.add(a);  
  20.             yarr.add(b);  
  21.             while (true) {  
  22.                 a = sc.nextInt();  
  23.                 b = sc.nextInt();  
  24.                 if (a == 0 && b == 0) {  
  25.                     Collections.sort(xarr);  
  26.                     Collections.sort(yarr);  
  27.                     System.out.println(xarr.get(0) + " " + yarr.get(0) + " "  
  28.                             + xarr.get(xarr.size() - 1) + " "  
  29.                             + yarr.get(yarr.size() - 1));  
  30.                     break;  
  31.                 }  
  32.                 xarr.add(a);  
  33.                 yarr.add(b);  
  34.             }  
  35.   
  36.         }  
  37.     }  
  38. }  

P1034

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.ArrayDeque;  
  2.   
  3. import java.util.Deque;  
  4. import java.util.PriorityQueue;  
  5.   
  6. import java.util.Scanner;  
  7.   
  8. public class Exercise1034 {  
  9.     public static void main(String[] args) {  
  10.         Scanner sc = new Scanner(System.in);  
  11.         while (sc.hasNext()) {  
  12.             int n = sc.nextInt();  
  13.             int m = sc.nextInt();  
  14.             if (n == 0 && m == 0)  
  15.                 System.exit(0);  
  16.   
  17.             int j;  
  18.             PriorityQueue<Integer> pq = new PriorityQueue<Integer>(m);  
  19.             for (int i = 0; i < n; i++) {  
  20.                 j = sc.nextInt();  
  21.                 if (pq.size() < m)  
  22.                     pq.offer(j);  
  23.                 else  
  24.   
  25.                 if (j > pq.peek()) {  
  26.                     pq.poll();  
  27.                     pq.offer(j);  
  28.                 }  
  29.   
  30.             }  
  31.   
  32.             Deque<Integer> a = new ArrayDeque<Integer>();  
  33.             while (!pq.isEmpty())  
  34.                 a.push(pq.poll());  
  35.   
  36.             while (a.size() > 1)  
  37.                 System.out.print(a.pop() + " ");  
  38.             System.out.println(a.pop());  
  39.         }  
  40.     }  
  41. }  

求最大m个,用小顶堆。

如果要大顶堆,则需要构造比较器,代码如下

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //固定容量的优先队列,模拟大顶堆,用于解决求topN小的问题  
  2. public static class FixSizedPriorityQueue>{  
  3.     private PriorityQueue queue;  
  4.     private int  maxSize; //堆的最大容量  
  5.   
  6.     public FixSizedPriorityQueue(int maxSize){  
  7.         if(maxSize <= 0throw new IllegalArgumentException();             
  8.         this.maxSize = maxSize;  
  9.         this.queue = new PriorityQueue(maxSize, new Comparator() {  
  10.   
  11.             @Override  
  12.             public int compare(E o1, E o2) {  
  13.                 return o2.compareTo(o1);  
  14.             }  
  15.         });  
  16.     }  
  17.   
  18.     public void add(E e){  
  19.         if(queue.size() < maxSize){ //未达到最大容量,直接添加  
  20.             queue.add(e);  
  21.         }else//队列已满  
  22.             E peek = queue.peek();  
  23.             if(e.compareTo(peek) < 0){ //将新元素与当前堆顶元素比较,保留较小的元素  
  24.                 queue.poll();  
  25.                 queue.add(e);  
  26.             }  
  27.         }  
  28.     }  
  29.   
  30.     public List sortedList(){  
  31.         List list = new ArrayList(queue);  
  32.         Collections.sort(list); //PriorityQueue本身的遍历是无序的,最终需要对队列中的元素进行排序  
  33.         return list;  
  34.     }  
  35.   
  36. }  



P1045

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2.   
  3. <span style="white-space:pre">  </span>public static void main(String[] args) {  
  4. <span style="white-space:pre">      </span>Scanner sc = new Scanner(System.in);  
  5.   
  6.   
  7. <span style="white-space:pre">      </span>double n;  
  8.   
  9.   
  10. <span style="white-space:pre">      </span>while (sc.hasNext()) {  
  11. <span style="white-space:pre">          </span>n = sc.nextDouble();  
  12.   
  13.   
  14. <span style="white-space:pre">          </span>for (int x = 0; x <= n / 5; x++)  
  15. <span style="white-space:pre">              </span>for (int y = 0; y <= n / 3; y++) {  
  16. <span style="white-space:pre">                  </span>int z = 100 - y - x;  
  17. <span style="white-space:pre">                  </span>if (5 * x + 3 * y + z / 3.0 <= n)  
  18. <span style="white-space:pre">                      </span>System.out.println("x=" + x + ",y=" + y + ",z=" + z);  
  19. <span style="white-space:pre">              </span>}  
  20. <span style="white-space:pre">      </span>}  
  21. <span style="white-space:pre">  </span>}  
  22. }  

1.注意输入描述中的“ 测试数据有多组

2.注意当n被定义为整形时n/3将自动舍弃小数部分


P1046

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.*;  
  2.   
  3. public class Exercise1046 {  
  4.     public static void main(String[] args) {  
  5.         Scanner sc = new Scanner(System.in);  
  6.         while (sc.hasNextLine()) {  
  7.             String str = sc.nextLine();  
  8.             String[] a = str.split(" ");  
  9.             ArrayList arr = new ArrayList();  
  10.             for (int i = 0; i < a.length; i++)  
  11.                 arr.add(Integer.parseInt(a[i]));  
  12.   
  13.             Collections.sort(arr);  
  14.             System.out.println("max=" + arr.get(arr.size() - 1));  
  15.         }  
  16.     }  
  17. }  

简单题,懒得自己写找最大值。没要求实现泛型,就不写了


P1047

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2.   
  3. public class Exercise1047 {  
  4.     public static void main(String[] args) {  
  5.         Scanner sc = new Scanner(System.in);  
  6.         while (sc.hasNext()) {  
  7.             int n = sc.nextInt();  
  8.             if (isPrime(n))  
  9.                 System.out.println("yes");  
  10.             else  
  11.                 System.out.println("no");  
  12.         }  
  13.     }  
  14.   
  15.     private static boolean isPrime(int n) {  
  16.         if (n < 2)  
  17.             return false;  
  18.         else if (n == 2)  
  19.             return true;  
  20.         else if ((n & 1) == 0)  
  21.             return false;  
  22.         else {  
  23.             int j = (int) Math.sqrt(n);  
  24.             for (int i = 3; i <= j; i = i + 2)  
  25.                 if (n % i == 0)  
  26.                     return false;  
  27.             return true;  
  28.         }  
  29.   
  30.     }  
  31. }  


P1048

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2.   
  3. public class Exercise1048 {  
  4.     public static void main(String[] args) {  
  5.         Scanner sc = new Scanner(System.in);  
  6.         while (sc.hasNext()) {  
  7.             int a = sc.nextInt();  
  8.             int b = sc.nextInt();  
  9.             int c = sc.nextInt();  
  10.   
  11.             if (a < b) {  
  12.                 a ^= b;  
  13.                 b ^= a;  
  14.                 a ^= b;  
  15.             }  
  16.   
  17.             if (a < c) {  
  18.                 a ^= c;  
  19.                 c ^= a;  
  20.                 a ^= c;  
  21.             }  
  22.   
  23.             if (foo(a, b, c) > 0)  
  24.                 System.out.println("钝角三角形");  
  25.             else if (foo(a, b, c) == 0)  
  26.                 System.out.println("直角三角形");  
  27.             else  
  28.                 System.out.println("锐角三角形");  
  29.         }  
  30.     }  
  31.   
  32.     private static int foo(int a, int b, int c) {  
  33.         return a * a - b * b - c * c;  
  34.     }  
  35.   
  36. }  

题目明确指明三角形,就没做判断


P1049 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2.   
  3.   
  4. public class Exercise1049 {  
  5.         public static void main(String[] args)  
  6.         {  
  7.             Scanner sc = new Scanner(System.in);  
  8.             while(sc.hasNext())  
  9.             {  
  10.                 String str = sc.nextLine();  
  11.                 char c = sc.nextLine().charAt(0);  
  12.                 char[] s = str.toCharArray();  
  13.                 str="";  
  14.                 for(int i=0;i<s.length;i++)  
  15.                 {  
  16.                     if(s[i]!=c)  
  17.                         str += s[i];  
  18.                 }  
  19.                 System.out.println(str);  
  20.             }  
  21.         }  
  22. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值