首页 > 编程语言 > 详细

POJ题目Java代码(一)

时间:2014-06-15 17:58:35      阅读:406      评论:0      收藏:0      [点我收藏+]

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 = sc.nextInt();
			BigDecimal result = bigDecimal.pow(powValue);
			//stripTrailingZeros():如果小数点后面都是0的话,则把小数点和后面的0都去掉;
			//toPlainString()是什么样子的小数就显示什么样子,toString()将小数用科学计数法表示。
			String str = result.stripTrailingZeros().toPlainString();
			
			//如果是小数,并且个位是0,则将0去掉
			if(str.startsWith("0")){
				str = str.substring(1);
			}
			
			System.out.println(str);
			
		}
		
	}

}

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;
	}

}


POJ题目Java代码(一),布布扣,bubuko.com

POJ题目Java代码(一)

原文:http://blog.csdn.net/bear_huangzhen/article/details/30452253

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!