java高精度,大数
package 高精度幂; import java.math.BigDecimal; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); while (cin.hasNext()) { BigDecimal bd = new BigDecimal(cin.next()); BigDecimal result = bd.pow(cin.nextInt()); String s = result.stripTrailingZeros().toPlainString(); //去掉尾零 if(s.startsWith("0")) //去掉首零 s=s.substring(1); System.out.println(s); } } }
2.HDU1042 N!
import java.math.*; import java.util.*; public class Main { public static BigInteger num; public static void main(String args[]) { Scanner cin = new Scanner(System.in); while (cin.hasNext()) { num = BigInteger.ONE; int n = cin.nextInt(); num = BigInteger.ONE; for (int i = 1; i <= n; i++) { num= num.multiply(BigInteger.valueOf(i)); } System.out.println(num); } } }
4.HDU1753 加法
import java.math.BigDecimal; import java.util.Scanner; import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); while(cin.hasNext()){ BigDecimal a=cin.nextBigDecimal(); BigDecimal b=cin.nextBigDecimal(); String s = a.add(b).stripTrailingZeros().toPlainString(); System.out.println(s); } } }
比较函数
if(a.compareTo(b)<0)//此时由于a<b,则返回值小于0; { System.out.println("a<b"); } if(b.equals(c))//此时由于b==c,则返回值等于0; { System.out.println("b==c"); } if(d.compareTo(c)>0)//此时由于d>c,则返回值大于0; { System.out.println("d>c"); }
(4)
对于输出浮点数保留几位小数的问题,可以使用DecimalFormat类,
import java.text.*;
DecimalFormat f = new DecimalFormat("#.00#");
DecimalFormat g = new DecimalFormat("0.000");
double a = 123.45678, b = 0.12;
System.out.println(f.format(a));
System.out.println(f.format(b));
System.out.println(g.format(b));
这里0指一位数字,#指除0以外的数字。
string中的字符串不可变,stringbuild或者stringbuffer中的可变,还有insert与append方法
reverse函数
package test1;
public class TestReverse {
public static void main(String[] args) {
String str2 = "Hello";
str2 = new StringBuffer(str2).reverse().toString();
System.out.println(str2);
String message = "Hello";
StringBuilder rev = new StringBuilder();
for (int i = message.length() - 1; i >= 0; i--)
rev.append(message.charAt(i));
System.out.println(rev.toString());
}
}
java中字符串String是不可以修改的,要修改只能转换为字符数组.
例程:
- import java.io.*;
- import java.math.*;
- import java.util.*;
- import java.text.*;
- public class Main
- {
- public static void main(String[] args)
- {
- int i;
- Scanner cin = new Scanner (new BufferedInputStream(System.in));
- String st = "abcdefg";
- System.out.println(st.charAt(0)); // st.charAt(i)就相当于st[i].
- char [] ch;
- ch = st.toCharArray(); // 字符串转换为字符数组.
- for (i = 0; i < ch.length; i++) ch[i] += 1;
- System.out.println(ch); // 输入为“bcdefgh”.
- if (st.startsWith("a")) // 如果字符串以'0'开头.
- {
- st = st.substring(1); // 则从第1位开始copy(开头为第0位).
- }
- }
- }
函数:Arrays.sort();
例程:
- import java.io.*;
- import java.math.*;
- import java.util.*;
- import java.text.*;
- public class Main
- {
- public static void main(String[] args)
- {
- Scanner cin = new Scanner (new BufferedInputStream(System.in));
- int n = cin.nextInt();
- int a[] = new int [n];
- for (int i = 0; i < n; i++) a[i] = cin.nextInt();
- Arrays.sort(a);
- for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
- }
- }
1、Arrays.sort()
(1)数字排序
int[] num =newint[]{4,2,3,5,-8,5,2,3};
Arrays.sort(num);
for(int i = 0; i < 8;i++)
System.out.print(num[i]+”,”);
输出结果:-8,2,2,3,3,4,5,5,
(2)字符串排序(先大写后小写原则)
String[] s = new String[]{"Z","a","A","z"};
Arrays.sort(s);
for(int i = 0; i < 4;i++)
System.out.print(s[i] +",");
输出结果:A,Z,a,z,
(3)严格按照字母表排序(即忽略大小写)
String[] s = new String[]{"Z","a","A","z"};
Arrays.sort(s,String.CASE_INSENSITIVE_ORDER);
for(int i = 0; i < 4;i++)
System.out.print(s[i] +",");
输出结果:a,A,Z,z,
(4)忽略大小写反向排序
String[] s = new String[]{"Z","a","A","z"};
Arrays.sort(s,String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(s));
for(int i = 0; i < 4;i++)
System.out.print(s[i] +",");
输出结果:z,Z,A,a,
(5)对象的排序(类似于C中的结构体的排序)
package zoj3380; import java.io.*; import java.lang.reflect.Array; import java.text.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); student[] students = new student[10]; for (int i = 0; i < n; i++) { String s = scanner.next(); int a = scanner.nextInt(); int b = scanner.nextInt(); students[i] = new student(a, b, s); } // int len = students.; Arrays.sort(students,0, n, new Acomparator());
//排序必须保证每个里面都有元素,要不然会报错。 // Arrays.sort(students, new Acomparator()); for(int i = 0; i < n; i++) { System.out.println(students[i].getA()); } } } class student { private int a, b; private String s; public student(int a, int b, String s) { this.a = a; this.b = b; this.s = s; } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public String getS() { return s; } public void setS(String s) { this.s = s; } } class Acomparator implements Comparator { @Override public int compare(Object o1, Object o2) { int a = ((student)o1).getA(); int b = ((student)o2).getA(); int c = a - b; if(c <= 0) return -1; else return 1; } }