Java 解决一些ACM中大数问题
大数中算术运算结果的首选标度
运算 | 结果的首选标度 |
---|---|
加 | max(addend.scale(), augend.scale()) |
减 | max(minuend.scale(), subtrahend.scale()) |
乘 | multiplier.scale() + multiplicand.scale() |
除 | dividend.scale() - divisor.scale() |
Problem D: Integer Inquiry
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 59 Solved: 18
[Submit][Status][Web Board]
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.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
HINT
大数相加,遇到输入0时输出结果,直到读到EOF(文件结尾)为止
代码:
import java.math.BigInteger;
import java.util.Scanner; public class Main{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
BigInteger a,b=new BigInteger("0");
while(s.hasNext()){
a=s.nextBigInteger(); //接收输入
if(!(a.toString()==("0"))) //判定是否等于0
b=b.add(a);
if(a.toString()==("0")){
System.out.println(b);
b=new BigInteger("0");
}
}
}
}
Problem E: Product
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 38 Solved: 25
[Submit][Status][Web Board]
Description
The problem is to multiply two integers X, Y. (0<=X,Y<10250)
Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
Output
For each input pair of lines the output line should consist one integer the product.
Sample Input
12
12
2
222222222222222222222222
Sample Output
144
444444444444444444444444
HINT
两个大数相乘,直到读到EOF(文件结尾)为止
代码:
import java.math.BigInteger;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
while(s.hasNext()){
BigInteger a=s.nextBigInteger();
BigInteger b=s.nextBigInteger();
BigInteger c=a.multiply(b);
System.out.println(c);
}
}
}
Problem F: Exponentiation
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 9 Solved: 6
[Submit][Status][Web Board]
Description
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999) and n is an integer such that $0 < n \le 25$.
Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
Output
The output will consist of one line for each line of input giving the exact value of Rn. Leading zeros and insignificant trailing zeros should be suppressed in the output.
Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
HINT
大数a的n次幂,直到读到EOF(文件结尾)为止,其中忽略小数后面的0
代码:
import java.math.BigDecimal;
import java.util.Scanner; public class Problem_F_Exponentiation { public static void main(String[] args) {
Scanner s=new Scanner(System.in);
BigDecimal a;
int b;
String c;
while(s.hasNext()){
a=s.nextBigDecimal();
b=s.nextInt();;
a=a.pow(b).stripTrailingZeros(); //stripTrailingZeros将所得结果小数部分尾部的0去除
c=a.toPlainString(); //toPlainString将所得大数结果不以科学计数法显示,并转化为字符串
if(c.charAt(0)=='0') //用charAt()判定首位字符是否为0
c=c.substring(1);
System.out.println(c);
}
}
}
Problem G: If We Were a Child Again
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 7 Solved: 6
[Submit][Status][Web Board]
Description
The Problem
The first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.
But like many other university students he doesn’t like to do any project by himself. He just wants to collect programs from here and there. As you are a friend of him, he asks you to write the program. But, you are also intelligent enough to tackle this kind of people. You agreed to write only the (integer) division and mod (% in C/C++) operations for him.
Input
Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign (division or mod). Again spaces. And another input number. Both the input numbers are non-negative integer. The first one may be arbitrarily long. The second number n will be in the range (0 < n < 231).
Output
A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.
Sample Input
110 / 100
99 % 10
2147483647 / 2147483647
2147483646 % 2147483647
Sample Output
1
9
1
2147483646
HINT
大数求余与整除运算
代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
BigInteger a,b,t=new BigInteger("1");
String c;
while(s.hasNext()){
a=s.nextBigInteger();
c=s.next();
b=s.nextBigInteger();
if(c.equals("%")) t=a.mod(b);
if(c.equals("/")) t=a.divide(b);
System.out.println(t);
}
}
}
Java 解决一些ACM中大数问题的更多相关文章
- IBM Thread and Monitor Dump Analyzer for Java解决生产环境中的性能问题
这个工具的使用和 HeapAnalyzer 一样,非常容易,同样提供了详细的 readme 文档,这里也简单举例如下: #/usr/java50/bin/java -Xmx1000m -jar jca ...
- ACM中java的使用
ACM中java的使用 转载自http://www.cnblogs.com/XBWer/archive/2012/06/24/2560532.html 这里指的java速成,只限于java语法,包括输 ...
- [原创]浅谈JAVA在ACM中的应用
由于java里面有一些东西比c/c++方便(尤其是大数据高精度问题,备受广大ACMer欢迎),所以就可以灵活运用这三种来实现编程,下面是我自己在各种大牛那里总结了一些,同时加上自己平时遇到的一些jav ...
- ACM中java的使用 (转)
ACM中java的使用 这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner ...
- Java在ACM中的应用
Java在ACM中的应用 —. 在java中的基本头文件(java中叫包) import java.io.*; import java.util.*; //输入Scanner import java. ...
- ACM中Java的应用
先说一下Java对于ACM的一些优点吧: (1) 对于熟悉C/C++的程序员来说Java 并不难学,两周时间基本可以搞定一般的编程,再用些时间了解一下Java库就行了.Java的语法和C++非常类似, ...
- ACM中使用 JAVA v2. 1
ACM中使用JAVA v2.1 严明超 (Blog:mingchaoyan.blogbus.com Email:mingchaoyan@gmail.com) 0.前 言 文前声明:本文只谈java用于 ...
- Java中的BigInteger在ACM中的应用
Java中的BigInteger在ACM中的应用 在ACM中的做题时,常常会遇见一些大数的问题.这是当我们用C或是C++时就会认为比較麻烦.就想有没有现有的现有的能够直接调用的BigInter,那样就 ...
- ACM 中JAVA的应用
原文地址:http://www.cppblog.com/vontroy/archive/2010/05/24/116233.html 先说一下Java对于ACM的一些优点吧: (1) 对于熟悉C/C+ ...
随机推荐
- 【剑指Offer】32、把数组排成最小的数
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. ...
- 61.index CUD
主要知识点 索引CUD 一.创建索引的语法 PUT /my_index { "settings": { ... any settings ... }, " ...
- 1、dubbo的概念
Dubbo是什么? Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点. Dubbo[]是 ...
- 7.IDEA创建Web项目和Tomcat配置
IntelliJ IDEA Tomcat配置 详解 Tomcat 7.0 和jdk1.8 一起使用 一.创建web项目 1.1 创建工程 1.2 创建java web项目并创建web.xml文件 1 ...
- Atcoder AGC031C Differ By 1 Bit (构造、二进制)
哎呀这个C怎么比B还水....(我现在大概也就会做点这种水题了吧) 题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_c 题目大意 符号约定: ...
- 洛谷 P3275 BZOJ 2330 [SCOI2011]糖果
题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配 ...
- C++ - 部分STL容器如何去除重复元素
如果元素被保存在vector中,可先对vector里面的元素排序,然后调用unique函数去重,unique(起始迭代器,终止迭代器),返回的是去重以后vector中没有重复元素的下一个位置的迭代器. ...
- Spring深入理解(三)
Spring 中 AOP 特性详解 动态代理的实现原理 要了解 Spring 的 AOP 就必须先了解动态代理的原理,因为 AOP 就是基于动态代理实现的.动态代理还要从 JDK 本身说起. 在 Jd ...
- HDU 4502
直接贪心就好. #include <iostream> #include <cstdio> #include <algorithm> #include <cs ...
- vbs 脚本2
一些很恶作剧的vbs程序代码 作者: 字体:[增加 减小] 类型:转载 时间:2013-01-16我要评论 恶作剧的vbs代码,这里提供的都是一些死循环或导致系统死机的vbs对机器没坏处,最多关机重启 ...