【HDOJ】1314 Numerically Speaking
学了几天的Java了,终于独立A了一道大数计算。感觉还得练Java啊。
import java.util.Scanner;
import java.math.BigInteger;
import java.lang.StringBuilder; public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNextLine()) {
String line = cin.nextLine();
char l0 = line.charAt();
if (l0 == '*')
break;
if (l0>='' && l0<='') {
StringBuilder builder = new StringBuilder();
BigInteger x = new BigInteger(line);
BigInteger div = new BigInteger("");
BigInteger[] vals = new BigInteger[];
while (x.compareTo(BigInteger.ZERO) != ) {
vals = x.divideAndRemainder(div);
char ch = (char)(vals[].intValue()+);
builder.append(ch);
x = vals[];
}
builder.reverse();
String ans = new String(builder.toString());
System.out.print(ans);
int l = ans.length();
while (l < WIDTH) {
System.out.print(' ');
++l;
}
} else {
int length = line.length();
System.out.print(line);
int l = length;
while (l < WIDTH) {
System.out.print(' ');
++l;
}
BigInteger div = new BigInteger("");
BigInteger sum = new BigInteger(String.valueOf((int)(line.charAt(length-))-));
for (int i=length-; i>=; --i) {
// System.out.print("current sum = ");
// System.out.println(sum.toString());
BigInteger tmp = div.multiply(new BigInteger(String.valueOf((int)(line.charAt(i))-)));
sum = sum.add(tmp);
div = div.multiply(new BigInteger(""));
}
line = new String(sum.toString());
}
int length = line.length();
int x = length%;
x = (x==) ? :x;
int j = ;
for (int i=; i<length; ++i) {
if (i < x) {
System.out.print(line.charAt(i));
} else {
if ((j+)<length && (j%)==) {
System.out.print(',');
}
++j;
System.out.print(line.charAt(i));
}
}
System.out.println();
}
}
public static void reverse(byte[] bytes, int l) {
byte tmp;
for (int i=; i+i<l; ++i) {
tmp = bytes[i];
bytes[i] = bytes[l--i];
bytes[l--i] = tmp;
}
}
static final int WIDTH = ;
}
【HDOJ】1314 Numerically Speaking的更多相关文章
- HDU 1314 Numerically Speaking(大数加减乘除+另类二十六进制互相转换)
原题代号:HDU 1314 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1314 Numerically Speaking Time Limit: 2 ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
随机推荐
- Android自动化测试之Monkey工具
前言:Android自动化测试工具.方法和框架,包括android测试框架.CTS.Monkey.Monkeyrunner.benchmark.test tool等. 一. 什么是MonkeyMonk ...
- [转] Java中的访问控制
可见/访问性 在同一类中 同一包中 不同包中 同一包子类中 不同包子类中 public yes yes yes yes yes protected yes yes no ...
- Unix文件操作
一.概述 Unix文件操作常用函数包括open.close.creat.lseek.dup.dup2.fcntl等, 其中open.creat. fcntl函数需要包含头文件<fcntl.h&g ...
- BestCoder冠军赛 - 1005 Game 【DP】
[题意] 给出一个set,set中有几个数. 现在给出n个人,环成一圈搞约瑟夫... 开始时从第1号报数,每次从set中随机选出一个数s,等报数到s后,报s的人出圈,其他人继续报数. 最后只剩1人时, ...
- SQL Server强制删除发布
今日发现SQL Server 中 存在以前(系统还原前)的发布内容,使用鼠标->右键,选择删除,失败. 可使用语句: EXEC SP_REMOVEDBREPLICATION '发布数据库名称 ...
- WMI概述
关于wmi的定义可以在网上和msdn中查询,我在这里想说说自己对wmi的理解.Wmi是Windows Management Instrumentation(windows管理方法)的缩写.在wmi中微 ...
- 31.Spring-开发流程.md
[toc] 1.简单开发流程 1.1引用类库 基本类库: ## 1.2创建spring配置文件,文件的名称为固定格式:applicationContext.xml或者bean.xml: <?xm ...
- 使用C++11 实现的线程池
最近打算做一个服务器端程序,每来一个客户端请求新开一个线程进行处理.在网上查了一些资料后,准备使用线程池来做这个东西.使用C++11新的库处理想线程问题比以前简单了许多,在网上找到一份线程池的实现,h ...
- 跟我学android-Android应用基本组件介绍(五)
Activity activity 是最基本的模块,我们成为活动,一个activity通常就是一个单独的屏幕,每一个活动都被实现为一个独立的类,且都继承活动的基类.在activity的实现类里显示用户 ...
- Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知
Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...