首页 > 代码库 > POJ题目Java代码(一)

POJ题目Java代码(一)

POJ 1001 Exponentiation

import java.math.BigDecimal;
import java.util.Scanner;

public class Poj1001 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		while(sc.hasNext()){
			BigDecimal bigDecimal = new BigDecimal(sc.next());
			int powValue = http://www.mamicode.com/sc.nextInt();>

POJ 1002 487-3279

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Poj1002 {

	static char[] map = "22233344455566677778889999".toCharArray();
	static char[] str = new char[80];
	static char[][] telNumbers = new char[100000][8];

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		for (int i = 0; i < n; i++) {
			String s = sc.next();
			str = s.toCharArray();
			initTel(i);
		}
		
		ArrayList<String> list = new ArrayList<String>();
		
		//将二维char数组转换为ArrayList<String>
		for(int i=0; i<n; i++){
			list.add(String.valueOf(telNumbers[i]));
		}
		
		//对数据进行排序
		Collections.sort(list, new MyComparator());
		
		//搜索重复的电话号码,并进行输出
		boolean noDuplicate = true;
		int i = 0, j;
		while(i<n){
			j = i;
			i++;
			while(i<n && list.get(i).equals(list.get(j))){
				i++;
			}
			if(i-j > 1){
				System.out.println(list.get(j) + " " + (i-j));
				noDuplicate = false;
			}
		}
		
		if(noDuplicate){
			System.out.println("No duplicates.");
		}

	}
	
	/**
	 * 将第n行的电话号码转换成标准形式(全数字形式带连接符)
	 */
	static void initTel(int n) {
		int j = -1, k = -1;

		while (k < 8) {
			j++;
			
			//如果是连接符‘-’,跳过
			try {
				if (str[j] == '-') {
					continue;
				}
			} catch (ArrayIndexOutOfBoundsException e) {
				break;
			}

			k++;
			//在第三到第四个位置之间插入一个连接符‘-’
			if (k == 3) {
				telNumbers[n][k] = '-';
				k++;
			}
			//如果是字母,则将其转换为数字
			if (str[j] >= 'A' && str[j] <= 'Z') {
				telNumbers[n][k] = map[str[j]-'A'];
				continue;
			}
			//如果是数字,直接存起来
			telNumbers[n][k] = str[j];
		}
		
		
	}
	
	/**
	 * 设置比较规则
	 */
	private static class MyComparator implements Comparator<String>{

		@Override
		public int compare(String o1, String o2) {
			return o1.compareTo(o2);
		}
		
	}

}

POJ 1003 HangOver

import java.util.Scanner;

public class Poj1003 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		float f;
		while(sc.hasNext()){
			f = sc.nextFloat();
			if(f==0){
				break;
			}
			
			System.out.println(getNumber(f) + " card(s)");
		}
		
	}
	
	//利用循环找出需要多少块卡片才能伸出value的长度
	private static int getNumber(float value){
		float temp = 0F;
		int n = 2;
		while(temp < value){
			temp += 1.0/n;
			n++;
		}
		
		return n-2;
	}

}

POJ 1004 Financial Management

import java.text.DecimalFormat;
import java.util.Scanner;

public class Poj1004 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		double sum = 0.0;
		
		for(int i=0; i<12; i++){
			double d = sc.nextDouble();
			sum += d;
		}
		
		double result = sum/12.0;
		
		//对结果小数进行格式化
		DecimalFormat df = new DecimalFormat(".00");
		String str = df.format(result);
		
		System.out.println("$" + str);
	}
	
}

POJ 1005 I Think I Need a Houseboat

import java.util.Scanner;

public class Poj1005 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			double x = sc.nextDouble();
			double y = sc.nextDouble();
			System.out.println("Property " + (i + 1)
					+ ": This property will begin eroding in year "
					+ getYear(x, y) + ".");
		}
		
		System.out.println("END OF OUTPUT.");
	}

	static int getYear(double x, double y) {
		int year = 0;
		double area = 0.0;
		while (area < (Math.PI * (x * x + y * y)) / 2) {
			year++;
			area = year * 50.0;
		}

		return year;
	}

}