UVA-424 - Integer Inquiry
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=365
没啥好说的,代码如下:
import java.util.*;
import java.math.*;
public class Main{
public static void main(String [] args){
Scanner scan=new Scanner(System.in);
BigInteger sum=BigInteger.valueOf(0);
while(scan.hasNext())
{
BigInteger a=scan.nextBigInteger();
if(a.equals(BigInteger.ZERO))
break;
else
{
sum=sum.add(a);
}
}
System.out.println(sum);
}
}
UVA-10106 - Product
没啥好说的,简单乘法,代码如下:
import java.util.*;
import java.math.*;
public class Main{
public static void main(String [] args){
Scanner scan=new Scanner(System.in);
while(scan.hasNext())
{
BigInteger a=scan.nextBigInteger();
BigInteger b=scan.nextBigInteger();
BigInteger sum=a.multiply(b);
System.out.println(sum);
}
}
}
UVA-465 - Overflow
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=406貌似看别人代码有更好的做法,俺这是比较笨的人的做法= =!
额,题目的坑在于输出的时候会有前导0!!!
代码如下:
import java.util.*;
import java.math.*;
public class Main{
public static void main(String [] args){
Scanner scan=new Scanner(System.in);
while(scan.hasNext())
{
String woca1=scan.next();
BigInteger a=new BigInteger(woca1);
String haha=scan.next();
char hehe=haha.charAt(0);
String woca2=scan.next();
BigInteger b=new BigInteger(woca2);
System.out.print(woca1);
System.out.print(" "+hehe+" ");
System.out.println(woca2);
if(a.compareTo(BigInteger.valueOf(2147483647))>0)
System.out.println("first number too big");
if(b.compareTo(BigInteger.valueOf(2147483647))>0)
System.out.println("second number too big");
if(hehe=='+')
{
BigInteger ans=a.add(b);
if(ans.compareTo(BigInteger.valueOf(2147483647))>0)
System.out.println("result too big");
}
else if(hehe=='*')
{
BigInteger ans=a.multiply(b);
if(ans.compareTo(BigInteger.valueOf(2147483647))>0)
{
System.out.println("result too big");
}
}
}
}
}
UVA-748 - Exponentiation
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=689依稀记得还是POJ的1001。。。
没啥好说的,注意前导0.和后面无用的0
PS:JAVA太强大了= =!
import java.util.*;
import java.math.*;
public class Main{
public static void main(String [] args){
Scanner scan=new Scanner(System.in);
while(scan.hasNext())
{
BigDecimal a;
a=scan.nextBigDecimal();
int n;
n=scan.nextInt();
a=a.pow(n);
a=a.stripTrailingZeros();
String str=a.toPlainString();
if(str.startsWith("0."))
str=str.substring(1);
System.out.println(str);
}
}
}
UVA-10494 - If We Were a Child Again
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1435额。。。。依旧不懂为啥java没有读入char 的机制,要先读string才能读char!
代码如下:
import java.util.*;
import java.math.*;
public class Main{
public static void main(String [] args){
Scanner scan=new Scanner(System.in);
while(scan.hasNext())
{
BigInteger a=scan.nextBigInteger();
String temp=scan.next();
//next()的用法要牢记,java按照空格读入就靠它了
//查找并返回来自此扫描器的下一个完整标记。完整标记的前后是与分隔模式匹配的输入信息。
char op=temp.charAt(0);
BigInteger b=scan.nextBigInteger();
//Java有趣的是dicideAndRemainder返回的是一个商加余数的数组,而非就是一个余数= =#
BigInteger []c=a.divideAndRemainder(b);
if(op=='/')
System.out.println(a.divide(b));
else if(op=='%')
System.out.println(c[1]);
}
}
}