欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

java棋盘问题_0x03大数问题(JAVA解决棋盘覆盖,A+B Problem II)

发布时间:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java棋盘问题_0x03大数问题(JAVA解决棋盘覆盖,A+B Problem II) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

常见的有棋盘覆盖和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

样例输出

Case 1:

1 + 2 = 3

Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110

偷懒的我,用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));

}

}

}

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

5

21

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);

}

}

}

强大的java大数问题。

总结

以上是生活随笔为你收集整理的java棋盘问题_0x03大数问题(JAVA解决棋盘覆盖,A+B Problem II)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。