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

}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值