zoukankan      html  css  js  c++  java
  • USACO部分试题答案1.2.4;1.2.5;1.3.1

    package Karl.Water;
    
    /*
    LANG: JAVA
    PROG: dualpal
    */
    
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.StringTokenizer;
    
    public class dualpal {
    	public static void main(String[] args) throws IOException {
    		BufferedReader in = new BufferedReader(new FileReader("dualpal.in"));
    		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("dualpal.out")), true);
    		StringTokenizer st = new StringTokenizer(in.readLine());
    		int N = Integer.parseInt(st.nextToken());
    		int S = Integer.parseInt(st.nextToken());
    		do {
    			if (dualPal(++S)) {
    				out.println(S);
    				if (--N == 0)
    					break;
    			}
    		} while (S != Integer.MAX_VALUE);
    		System.exit(0);
    	}
    
    	private static char digitToChar(int digit) {
    		return (char) (digit + '0');
    	}
    
    	private static boolean dualPal(int num) {
    		int count = 0;
    		int base = 2;
    		do {
    			if (isPal(toBaseForm(num, base))) {
    				if (++count >= 2)
    					return true;
    			}
    		} while (++base <= 10);
    		return false;
    	}
    
    	private static String toBaseForm(int num, int base) {
    		StringBuilder sb = new StringBuilder();
    		do {
    			char curChar = digitToChar(num % base);
    			sb.append(curChar);
    		} while ((num /= base) != 0);
    		sb.reverse();
    		return sb.toString();
    	}
    
    	private static boolean isPal(String s) {
    		int low = 0, high = s.length() - 1;
    		while (low < high) {
    			if (s.charAt(low) != s.charAt(high))
    				return false;
    			low++;
    			high--;
    		}
    		return true;
    	}
    }
    
    第二题
    package Karl.Water;
    
    /*
    LANG: JAVA
    TASK: palsquare
    */
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class palsquare {
    	static char[] a ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K'};
    	public static boolean judgehuiwen(String s){
    		for(int i=0;i<s.length()/2;i++){
    			if(s.charAt(i)!=s.charAt(s.length()-1-i))
    				return false;
    		}
    		return true;
    	}
    	public static String judge(Integer i,Integer j){
    		if(i/j==0){
    			return String.valueOf(a[i%j]);
    		}else{
    			return judge(i/j,j)+String.valueOf(a[i%j]);
    		}
    	}
    	public static void judgepingfang(int j) throws IOException{
    		PrintWriter pw = new PrintWriter (new FileWriter("palsquare.out"));
    		for(int i=1;i<=300;i++){
    			if(judgehuiwen(judge(i*i,j)))
    				pw.println(judge(i,j)+" "+judge(i*i,j));
    		}
    		pw.close();
    	}
    	public static void main(String[] args) throws IOException {
    		BufferedReader br = new BufferedReader(new FileReader("palsquare.in"));
    		judgepingfang(Integer.parseInt(br.readLine()));
    		br.close();
    	}
    }
    第三题:
    package Karl.Water;
    
    /*
    LANG:JAVA
    TASK:milk
    */
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.StringTokenizer;
    
    public class milk {
    	public static void quick_sort(int[][] arr,int begin,int end){
    		for(int x=0;x<arr.length-1;x++){
    			for(int y=0;y<arr.length-x-1;y++){
    				if(arr[y][0]>arr[y+1][0]){
    					int temp=0;
    					int temper=0;
    					temp=arr[y][0];
    					temper=arr[y][1];
    					arr[y][0]=arr[y+1][0];
    					arr[y][1]=arr[y+1][1];
    					arr[y+1][0]=temp;
    					arr[y+1][1]=temper;
    				}
    			}
    		}
    	}
    	public static void fun(int[][] a,int needNum,int lineNum) throws IOException {
    		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("milk.out")), true);
    		int k = 0;
    		int money = 0;
    		if(a.length==0){
    			out.println(0);
    		}
    		for(int i=0;i<lineNum;i++){
    			k = k + a[i][1];
    			money = money + a[i][0]*a[i][1];
    			if(k==needNum){
    				out.println(money);
    				break;
    			}
    			if(k>needNum){
    				money=(a[i][1]-(k-needNum))*a[i][0]+(money-a[i][0]*a[i][1]);
    				out.println(money);
    				break;
    			}
    		}
    		out.close();
    	}
    	public static void agg() throws IOException {
    		BufferedReader in = new BufferedReader(new FileReader("milk.in"));
    		StringTokenizer st = new StringTokenizer(in.readLine());
    		int need = Integer.parseInt(st.nextToken());
    		int farmerNum = Integer.parseInt(st.nextToken());
    		int[][] a = new int[farmerNum][2];
    		for(int i=0;i<farmerNum;i++){
    			st = new StringTokenizer(in.readLine());
    			a[i][0] = Integer.parseInt(st.nextToken());
    			a[i][1] = Integer.parseInt(st.nextToken());
    		}
    		quick_sort(a,0,farmerNum-1);
    		fun(a,need,farmerNum);
    	}
    	public static void main(String[] args) throws IOException {
    		agg();
    	}
    }
    


  • 相关阅读:
    Leetcode 1711. 大餐计数(桶)
    macOS下安装powerline-status失败(报错Could not install packages due to an EnvironmentError)
    macOS下安装oh my zsh失败(报错Connection refused)
    Chapter 10 Operator Overloading
    牛客IOI周赛26-普及组 B. 子序列(int128)
    关于状压DP中子集状态的枚举
    2021蓝桥杯省赛第一场C/C++A组 试题E:回路计数(状压DP)
    AcWing 2879. 画中漂流(简单DP)
    Leetcode 525. 连续数组(前缀和性质/map)
    牛客小白月赛34 B. dd爱探险(状压DP)
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3061779.html
Copyright © 2011-2022 走看看