-
import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String num; num = scanner.next(); BigInteger n=new BigInteger(num); BigInteger a=BigInteger.valueOf(1); BigInteger b=BigInteger.valueOf(0); BigInteger ans; while(true){ if(n.compareTo(BigInteger.valueOf(0))==0){ ans=b; break; } if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0))!=0){ b=a.add(b); n=n.divide(BigInteger.valueOf(2)); } else if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0))==0){ a=a.add(b); n=n.divide(BigInteger.valueOf(2)); } } System.out.println(ans); } }
第二题就是循环加Biginteger
-
import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String num; num=sc.next(); BigInteger b=new BigInteger(num); BigInteger c=BigInteger.valueOf(1); BigInteger ans=BigInteger.valueOf(1); while(b.compareTo(c)==1){ ans=ans.multiply(c); c=c.add(BigInteger.valueOf(1)); } ans=ans.multiply(b); System.out.println(ans); } }
第三题就是字符串处理加biginterger
· import java.math.BigInteger;
· import java.util.Scanner;
·
· public class Main {
· static int[] coefficient = new int[100];
· static int[] index = new int[100];
· static int max_index = 0;
· public static void fun (String poly){
· String xi = "", zhi = "";
· boolean count_x = false;
· char[] item = poly.toCharArray();
·
· if (poly.compareTo("-x") == 0) {
· coefficient[1] = -1;
· index[1] = 1;
· return;
· } else if (poly.compareTo("x") == 0) {
· coefficient[1] = 1;
· index[1] = 1;
· return;
· }
· for (int i = 0; i < item.length; i++) {
· if(item[i] == 'x' && i == item.length-1) {
· coefficient[1] = Integer.parseInt(xi);
· index[1] = 1;
· return;
· } else if (item[i] == 'x') {
· count_x = true;
· i++;
· } else if (count_x == false) {
· xi += item[i];
· } else if (count_x == true) {
· zhi += item[i];
· }
· }
·
· int zhi_temp = 0;
· if (zhi != "") {
· zhi_temp = Integer.parseInt(zhi);
· } else if (zhi == "" && count_x == false) {
· zhi_temp = 0;
· }
·
· index[zhi_temp] = zhi_temp;
· if (xi.length() == 0) {
· coefficient[zhi_temp] = 1;
· } else if (xi.equals("-")){
· coefficient[zhi_temp] = -1;
· } else {
· coefficient[zhi_temp] = Integer.parseInt(xi);
· }
· max_index = Math.max(max_index, zhi_temp);
· }
·
· public static void main (String[] args) {
· Scanner sc = new Scanner(System.in);
· String temp = sc.nextLine();
· BigInteger n = sc.nextBigInteger();
· char[] item = temp.toCharArray();
·
· String poly = "";
· for (int i = 0; i < item.length; i++) {
· if ((item[i] == '+' || item[i] == '-') && i != 0) {
· fun (poly);
· poly = "";
· if (item[i] == '-') {
· poly += '-';
· }
·
· } else {
· poly += item[i];
· }
· }
· fun (poly);
· /*
· for (int i = 0; i <= max_index; i++){
· System.out.print(coefficient[i]);
· System.out.print(" ");
· System.out.println(index[i]);
· }
· */
·
· for (int i = 1; i <= max_index; i++) {
· if(index[i] != 0) {
· coefficient[i] *= i;
· index[i] --;
· }
· }
· /*
· for (int i = 1; i <= max_index; i++){
· System.out.print(coefficient[i]);
· System.out.print(" ");
· System.out.println(index[i]);
· }
· */
·
· BigInteger ans = BigInteger.valueOf(0);
· for (int i = 1; i <= max_index; i++) {
· BigInteger coe1, coe2;
· coe1 = n.pow(index[i]);
· coe2 = coe1.multiply(BigInteger.valueOf(coefficient[i]));
· ans = ans.add(coe2);
· }
· System.out.println(ans);
·
·
· }
·
· }