PAT-1073. Scientific Notation(Java StringBuild)

其实这篇也是PAT的题目,不过主要是用来学习Java字符串的操作的。

今天看了Java编程思想字符串的章节,原来java里面String对象是永远不会变的,因为没有任何方法是修改字符串里面的数据的。

如果要操作字符串对象例如:

String str = "de";
String s = "abc" + str + "fgh" + "00"; 

理论上讲,第二个操作在执行过程中,每执行一次加法,就会生成一个String对象。

其实不然,java编程思想一书里面,用反编译过程查看发现,编译器自动生成了一个StringBuilder对象,来实现第二个操作。

这个是Java设计师通过优化了,而作者要告诉我们的是如果有大量类似第二个操作的语句,建议我们自定义StringBuilder来实现,可以大大提高效率。

下面这个是书上的例子(我抄一下好了):

String result;
for(int i = 0; i < fields.length; i ++)
    result += fields[i];

StringBuilder result = new StringBuilder();
for(int i = 0; i < fields.length; i++)
    result.append(fields[i]);
String res = result.toString()

以上两种方法获得fields字符串数组的所有字符,但是第一种方法看似只有创建了String一个对象,其实每一次循环编译器都要创建一个StringBuilder对象,效率会大大降低。

以上内容都来自《Java编程思想》,我下面的题目只是用了一下StringBuilder而已,。也是我第一个用java通过的题目。


Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent's signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:

Each input file contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent's absolute value is no more than 9999.

Output Specification:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros,

Sample Input 1:
+1.23400E-03
Sample Output 1:
0.00123400
Sample Input 2:
-1.2E+10
Sample Output 2:
-12000000000

package acm;

import java.util.Scanner;

public class StringTest2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc  = new Scanner(System.in);
		String strIn = sc.next();
		StringBuilder strBul = new StringBuilder(strIn);
		
		StringBuilder strResBu =new StringBuilder(strBul.substring(0, strBul.indexOf("E")));
		StringBuilder strNumBu = new StringBuilder(strBul.substring(strBul.indexOf("E") + 1));
		int num = Integer.parseInt(strNumBu.toString());
		if(num > 0)
		{
			int leftNum = strResBu.length()-3;
			strResBu.deleteCharAt(2);
			if(num >= leftNum)
			{
				for(int i = 0; i < num-leftNum; i ++)
					strResBu.append('0');
			}
			else
			{
				strResBu.insert(2+num, '.');
			}
		}
		else
		{
			num = 0 - num;
			if(num > 0)
			{
				strResBu.deleteCharAt(2);
				strResBu.insert(1, '0');
				strResBu.insert(2, '.');
				for(int i = 0; i < num-1; i ++)
					strResBu.insert(3, 0);
			}
		}
		
		if(strResBu.charAt(0) == '+')
			strResBu.deleteCharAt(0);
		String strRes = strResBu.toString();
		
		System.out.print(strRes);
		
		sc.close();
	}
	

}

稍微有点乱,只是做个记录,学会了String,StringBuilder的关系。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值