是时候学一下java了,然后就刷了几道题,贴上来以备以后忘记,要是能过方便大家更好不过了。
Scanner cin = new Scanner(System.in);//读入流
while(cin.hasNext())//cin.hasNext()返回bool型,如果流中还有数据返回真,类似于读到文件末尾
如果读入一个T代表组数 应该写成while(T-->0)
a = cin.nextInt(); //读入整数a = cin.nextBigInteger();//读入大数
a = cin.nextBigDecimal();//读入高精度大数
BigDecimal a; //高精度大数的定义
BigInteger a; //大数的定义BigInteger fib[] = new BigInteger[501];//大数数组定义
大数之间可以直接运算、赋值,但是大数和其他类型的数则这不行。
所以大数初始化的时候一般是
a = BigInteger.valueOf(0); 或者 a = new BigInteger("0");
大数乘以非大数也是将其转换为大数再运算
eg:ans = ans.multiply(BigInteger.valueOf(i));
大数之间的比较:一般都是用compareTo
if(a.compareTo(BigDecimal.ZERO) == 0)
高精度大数除去后导0, 用stripTrailingZeros,但是还需要用 toPlainString,原因如下:
stripTrailingZeros 去除后导0 但是如果是数字大于10的话会转换成(eg:12500 会转换成 1.25E+4)
toPlainString 将1.25E+4转换成字符串 1.25E+4->12500
System.out.println(a.stripTrailingZeros().toPlainString());
1.A + B Problem
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int a,b;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a+b);
}
}
}
2.A + B Problem II
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int n, i;
Scanner cin = new Scanner(System.in);
BigInteger a,b;
n = cin.nextInt();
for(i = 1; i <= n; i++)
{
a = cin.nextBigInteger();
b = cin.nextBigInteger();
System.out.println("Case "+ i +":");
System.out.println(a + " + " + b + " = " + a.add(b));
if(i != n) System.out.println();
}
}
}
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNextInt())
{
int n = cin.nextInt();
BigInteger ans = BigInteger.valueOf(1);
for(int i = 1; i <= n; i++)
ans = ans.multiply(BigInteger.valueOf(i));
System.out.println(ans);
}
}
}
4.Integer Inquiry
http://acm.hdu.edu.cn/showproblem.php?pid=1047
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for(int i = 1; i <= n; i++)
{
BigInteger sum = BigInteger.valueOf(0);
while(cin.hasNext())
{
BigInteger num = cin.nextBigInteger();
if(num.equals(BigInteger.ZERO)) break;
sum = sum.add(num);
}
System.out.println(sum);
if(i != n) System.out.println();
}
}
}
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigDecimal a,b;
while(cin.hasNext())
{
a = cin.nextBigDecimal();
b = cin.nextBigDecimal();
a = a.add(b);
if(a.compareTo(BigDecimal.ZERO) == 0)
System.out.println("0");
else
System.out.println(a.stripTrailingZeros().toPlainString());
}
}
}
6.Octal Fractions
http://poj.org/problem?id=1131
http://poj.org/problem?id=1131
stripTrailingZeros 去除后导0 但是如果是数字大于10的话会转换成(eg:12500 会转换成 1.25E+4)
toPlainString 将1.25E+4转换成字符串 1.25E+4->12500
import java.math.*;import java.util.*;public class Main {
public static void main(String[] args)
{
String s;
BigDecimal eight = new BigDecimal(8);
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
s = cin.nextLine();
BigDecimal ans = new BigDecimal(0);
BigDecimal mul = new BigDecimal(1);
for(int i = 2; i < s.length(); i++)
{
mul = mul.divide(eight);
ans = ans.add(new BigDecimal(s.charAt(i) - '0').multiply(mul));
}
System.out.println(s + " [8] = " + ans.stripTrailingZeros().toPlainString() + " [10]");
}
}
}
7.大菲波数 http://acm.hdu.edu.cn/showproblem.php?pid=1715
读入一个T代表组数
while(T--)应该写成while(T-->0)
import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { BigInteger fib[] = new BigInteger[1001]; fib[0] = BigInteger.valueOf(0); fib[1] = BigInteger.valueOf(1); fib[2] = BigInteger.valueOf(1); for(int i = 3; i <= 1000; i++) { fib[i] = fib[i-1].add(fib[i-2]); } Scanner cin = new Scanner(System.in); int n; n = cin.nextInt(); while(n-->0) { int num; num = cin.nextInt(); System.out.println(fib[num]); } } }
8.How Many Fibs?
http://acm.hdu.edu.cn/showproblem.php?pid=1316
http://acm.hdu.edu.cn/showproblem.php?pid=1316
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args)
{
BigInteger fib[] = new BigInteger[501];
fib[0] = BigInteger.valueOf(0);
fib[1] = BigInteger.valueOf(1);
fib[2] = BigInteger.valueOf(2);
for(int i = 3; i <= 480; i++)
{
fib[i] = fib[i-1].add(fib[i-2]);
}
Scanner cin = new Scanner(System.in);
int l = 0, r = 0, i;
BigInteger a, b;
a = cin.nextBigInteger();
b = cin.nextBigInteger();
while(a.compareTo(fib[0]) != 0 || b.compareTo(fib[0]) != 0)
{
for(i = 1;; i++)
{
if(a.compareTo(fib[i]) <= 0)
{
l = i;
break;
}
}
for(;; i++)
{
if(b.compareTo(fib[i]) < 0)
{
r = i;
break;
}
}
System.out.println(r-l);
a = cin.nextBigInteger();
b = cin.nextBigInteger();
}
}
}
9.Exponentiation
http://acm.hdu.edu.cn/showproblem.php?pid=1063
http://acm.hdu.edu.cn/showproblem.php?pid=1063
s.startsWith("0.") //startsWith 是判断s字符串开头是不是("0.") 分号中这个字符串
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
BigDecimal a, ans;
int b;
a = cin.nextBigDecimal();
b = cin.nextInt();
ans = a;
for(int i = 2; i <= b; i++)
ans = ans.multiply(a);
String s = ans.stripTrailingZeros().toPlainString();//去掉后导0
if(s.startsWith("0.")) //startsWith 是判断开头是不是("str") 分号中这个字符串
s = s.substring(1);
System.out.println(s);
}
}
}
如果读入一个T代表组数
while(T--)应该写成while(T-->0)