Java大数类题型训练

网友投稿 295 2022-09-04

Java大数类题型训练

Fibonacci Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1. f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)

Your task is to take a number as input, and print that Fibonacci number.

Sample Input

100

Sample Output

354224848179261915075

Note:

No generated Fibonacci number in excess of 1000 digits will be in the test data, i.e. f(20) = 6765 has 4 digits. 这是第一题,真是恶心,到头来是少了while(cin.hasNext()) 需要导的包看来就这两个util.* 和math.*

import java.util.*;import java.math.*;public class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); while(cin.hasNext()){ int n = cin.nextInt(); if(n==1) System.out.println(n); else { BigInteger f[]=new BigInteger[n]; f[0]=f[1]=new BigInteger("1"); for(int i=2;i

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 276033 Accepted Submission(s): 53273

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input 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.

Output 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. Output a blank line between two test cases.

Sample Input 2 1 2 112233445566778899 998877665544332211

Sample Output Case 1: 1 + 2 = 3

Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110

hdu上的几道大数,还PE了,回车处理的问题,所以哪一道题都不能粗心大意,要像做比赛一样平时练题。

import java.util.*;import java.math.*;public class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); int t=cin.nextInt();int kase=1; for(int i=1;i<=t;i++) { System.out.println("Case "+ (i) + ":"); BigInteger a=cin.nextBigInteger(),b=cin.nextBigInteger(); System.out.println(a+" + "+b+" = "+a.add(b)); if(i!=t) System.out.println(""); } }}

N!

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 67586 Accepted Submission(s): 19372

Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!

Input One N in one line, process to the end of file.

Output For each N, output N! in one line.

Sample Input 1 2 3

Sample Output 1 2 6

用到了把int强制转换成BigInteger 先把int变成string string.ValueOf(it) 然后new一个BigInteger

import java.util.*;import java.math.*;public class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); while(cin.hasNext()){ int n=cin.nextInt(); BigInteger ans =new BigInteger("1"); for(int i=1;i<=n;i++){ ans = ans.multiply(new BigInteger(String.valueOf(i))); } System.out.println(ans); } }}

Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16021 Accepted Submission(s): 4134

Problem Description One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. ​​​This supercomputer is great,'' remarked Chip.​​I only wish Timothy were here to see these results.” (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.

Output Your program should output the sum of the VeryLongIntegers given in the input.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input 1

123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0

Sample Output 370370367037037036703703703670

import java.util.*;import java.math.*;public class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); int t=cin.nextInt(); for(int i=0;i

Try your best

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:hdu Bone Collector(背包)
下一篇:思路
相关文章

 发表评论

暂时没有评论,来抢沙发吧~