算法日记(Java实现)第20160715(1)期——POJ1001/POJ1002

今日题目:POJ1001、POJ1002

(一)POJ1001

1.首先考虑内置类型是否能用,显然不能用。发现内置double类型最多能保证小数点后15位精度,第16位将会有问题。

2.上网查找到与BigInteger类型类似的BigDecimal类型。该类可保存任意精度的实数,并可表示成指数形式、非指数形式。

3.主要出现的问题是忽略了题干中的“结尾不能出现零”。受内置double类型的影响,忽略了BigDecimal的运算不会将末位的0丢弃。调用其内置的stripTrailingZeros函数完美解决此问题。

源代码如下:

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

//POJ 1001

/*Date: 2016.07.15
 * 1st:	 Wrong Answer
 * 2nd:	 Wrong Answer
 * 3rd:	 Wrong Answer
 * 4th:	 Accepted
 */

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			BigDecimal t = sc.nextBigDecimal();
			int ti = sc.nextInt();
			String ts = t.pow(ti).toPlainString();
			if (ts.charAt(0) == '0' && ts.charAt(1) == '.')
				ts = ts.substring(1);
			System.out.println(ts);
		}
	}

}


(二)POJ1002

1.思路较为简单,就是读入字符串,将字符串的每个字符逐一操作、转换。然后进行排序、查重。
2.主要问题是在读入字符串的时候,如果采用sc.next()则最后一个输入被抛弃,提示用户再读入一个字符串,当输入一个新字符串后,那个被抛弃的字符串被“找回”。若采用sc.nextLine(),则第一个输入读入的是换行符。
3. 上述问题浪费较多的时间(包括上一题也是)。后来发现是在复制示例输入数据的时候,由于最后一行没有换行符,导致输入时就会出现输入格式问题。不过在此期间,却弄懂了next,nextLine的区别,并对相关输入流有了更清晰的认识。弄懂了所谓分隔符的意义、用法。
源代码如下:
import java.util.Arrays;
import java.util.Scanner;

//POJ 1002

/*Date: 2016.07.15
 * 1st:	Wrong Answer	missing "No duplicates."
 * 2nd:	Wrong Answer	missing Hyphens
 * 3rd:	Wrong Answer	One less output
 * 4th: Accepted
 */

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String []sa = new String[n];
		for(int i = 0; i < n; i++){
			StringBuilder ts = new StringBuilder(sc.next());
			//to phone number
			StringBuilder ss = new StringBuilder();
			for(int j = 0; j < ts.length(); j++){
				char ch = ts.charAt(j);
				if(ch == '-')
					continue;
				int ti = ch - 'A';
				//it's a number
				if(ti < 0)
					ss.append(ch);
				else if(ti <= 2){
					ss.append(2);
				}
				else if(ti <= 5){
					ss.append(3);
				}
				else if(ti <= 8){
					ss.append(4);
				}
				else if(ti <= 11){
					ss.append(5);
				}
				else if(ti <= 14){
					ss.append(6);
				}
				else if(ti <= 18){
					ss.append(7);
				}
				else if(ti <= 21){
					ss.append(8);
				}
				else if(ti <= 24){
					ss.append(9);
				}
			}
			//to sort the arrays (String array can use Arrays.sort, but StringBuilder cannot)
			sa[i] = ss.toString();
		}
		Arrays.sort(sa);
		boolean flag = false;
		for(int i = 0; i < n; i++){
			int times = 1;
			for(int j = i + 1; j < n; j++){
				if(!sa[j].equals(sa[i]))
					break;
				times++;
				i++;
			}
			if(times != 1){
				System.out.println(sa[i].substring(0, 3) + "-" + sa[i].substring(3, 7) + " "  + times);
				flag = true;
			}
		}
		if(!flag){
			System.out.println("No duplicates. ");
		}
	}
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. 输入说明 The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9. 输出说明 The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 If you don't know how to determine wheather encounted the end of input: s is a string and n is an integer C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... } 来源 East Central North America 1988 北大OJ平台(代理

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值