Gyh's blog

vuePress-theme-reco gyh    2022
Gyh's blog Gyh's blog

Choose mode

  • dark
  • auto
  • light
Home
Category
  • Algorithm
  • CSS
  • JavaScript
  • Others
  • Server
  • Utils
  • Article
  • Note
  • Git
  • Npm
  • Standard
  • Summary
Tag
Timeline
About
GitHub
author-avatar

gyh

91

Article

11

Tag

Home
Category
  • Algorithm
  • CSS
  • JavaScript
  • Others
  • Server
  • Utils
  • Article
  • Note
  • Git
  • Npm
  • Standard
  • Summary
Tag
Timeline
About
GitHub

大数问题

vuePress-theme-reco gyh    2022

大数问题

gyh 2018-09-09

常见的有棋盘覆盖和 A+B 问题,这类问题牵扯到的数值都比较大,如果用一般的数值类型,肯定输出不了,所以就要想一个办法,怎么把大数转换一下输出。


A+B Problem II 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. A,B must be positive. 输入 The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000. 输出 For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. 样例输入

2
1 2
112233445566778899 998877665544332211
1
2
3

样例输出

Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
1
2
3
4

偷懒的我,用 java 很简单

import java.util.*;
import java.math.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner (System.in);
        int l=sc.nextInt();
        for(int i=1;i<=l;i++){
            if(i!=1) System.out.println();
            BigInteger a,b;
            a=sc.nextBigInteger();
            b=sc.nextBigInteger();
            System.out.println("Case "+i+":");
            System.out.println(a+" + "+b+" = "+a.add(b));
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

java.math 提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。

棋盘覆盖 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在一个 2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图 1(k=2 时),现用一缺角的 2×2 方格(图 2 为其中缺右下角的一个),去覆盖 2k×2k 未被覆盖过的方格,求需要类似图 2 方格总的个数 s。如 k=1 时,s=1;k=2 时,s=5 输入 第一行 m 表示有 m 组测试数据; 每一组测试数据的第一行有一个整数数 k; 输出 输出所需个数 s; 样例输入

3
1
2
3
1
2
3
4

样例输出

1
5
21
1
2
3
import java.util.*;
import java.math.*;
public class Main {
		public static void main(String[] args) {
			Scanner sc=new Scanner (System.in);
			int l=sc.nextInt();
			BigInteger m=BigInteger.valueOf(4);
			BigInteger n=BigInteger.valueOf(-1);
			BigInteger o=BigInteger.valueOf(3);
			for(int i=1; i<=l; i++) {
				if(i!=1) System.out.println();
				BigInteger s;

				int k=sc.nextInt();
				s=((m.pow(k)).add(n)).divide(o);
				System.out.println(s);
			}
		}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

强大的 java 大数问题。

Edit on GitHub~
LastUpdated: 5/17/2022, 8:59:22 AM