Poj 1504 Adding Reversed Numbers(用字符串反转数字)
一、题目大意
反转两个数字并相加,所得结果崽反转。反转规则:如果数字后面有0则反转后前面不留0.
二、题解
反转操作利用new StringBuffer(s).reverse().toString();来实现,去0则利用while循环对10取余判断,对数取整。多次用到字符串和整数之间的互换,字符串转整数用到了
int num=Integer.parseInt(s);,整数转字符串则s= ""+a1;即可。
三、java代码
import java.util.Scanner;
public class Main {
public static String change(String s){
int num=Integer.parseInt(s);
while(num % 10==0){
num=num /10;
}
return new StringBuffer(num+"").reverse().toString();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,a1,b1;
String a,b;
n=sc.nextInt();
while(n--!=0){
a=sc.next();
b=sc.next();
a=change(a);
b=change(b);
a1=Integer.parseInt(a);
b1=Integer.parseInt(b);
a1=a1+b1;
a=""+a1;
System.out.println(change(a));
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Poj 1504 Adding Reversed Numbers(用字符串反转数字)的更多相关文章
- POJ 1504 Adding Reversed Numbers
/*Sample Input 3 24 1 4358 754 305 Sample Output 34 1998 */ 值得总结的几点就是: 1.atoi函数将字符串转化为整型数字(类似于强制转换) ...
- POJ 1504 Adding Reversed Numbers (水题,高精度整数加法)
题意:给两个整数,求这两个数的反向数的和的反向数,和的末尾若为0,反向后则舍去即可.即若1200,反向数为21.题目给出的数据的末尾不会出现0,但是他们的和的末尾可能会出现0. #include &l ...
- zoj 2001 Adding Reversed Numbers
Adding Reversed Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB The Antique Comedians of M ...
- ACM Adding Reversed Numbers(summer2017)
The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancien ...
- poj1504 Adding Reversed Numbers
Adding Reversed Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17993 Accepted: 9 ...
- zoj2001 Adding Reversed Numbers
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2001 Adding Reversed Numbers Time ...
- POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误
------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况 ...
- Java算法之字符串反转分析
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 在基本的工作内容开发中,算法不会显得那么重要,而在百万级别的时候,差距非常大,今天带大家研究下常见的 ...
- 字符串反转C#的实现
字符串反转是面试过程中出现频率较高的算法题,今天一个牛同事让我用C#帮他实现这个算法,前提当然是不能使用类库. 例如: how are you 的反转结果为 you are how. 算法1: 是我当 ...
随机推荐
- Win10 Edge浏览器 应用商店 IE浏览器 无法访问页面 0x8000FFFF 问题解决
- iOS11 push控制器tabbar上移问题
解决方法 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { // 如果有大 ...
- bilingual evaluation understudy
BLEU is designed to approximate human judgement at a corpus level, and performs badly if used to eva ...
- Token Fund到底是什么?
相比传统的VC基金,作为新生事物的Token Fund从一出场便吸引着行业目光的追随.行业早期ICO促使区块链行业野蛮生长,大量区块链项目涌现,同时也带动了大量“代投”朝Token Fund方向发展. ...
- 代替print输出的PY调试库:PySnooper
PySnooper¶ Github:https://github.com/lotapp/PySnooper pip install pysnooper 使用:分析整个代码 @pysnooper.s ...
- iOS8 with Swift
Ref:iOS8 Day-by-Day Ref:iOS8-day-by-day source Ref:Let's Swift Ref:Swift 代码库 Ref:iOS Apprentice Thir ...
- CAS的实现Atomic类库
atomic 原子(atomic)本意是"不能被进一步分割的最小粒子",而原子操作(atomic operation)意为"不可被中断的一个或一系列操作".在多 ...
- html ---- web sql 例子
<!doctype html> <html> <head> <meta charset="utf-8"> <script> ...
- iOS category 类别 和 extension 扩展
category 类别 又称为 分类 在ios项目开发中允许使用类别为现有的类添加新的方法,并不需要创建子类.通过类别我们可以动态地为现有的类添加新的方法,可以将类的定义模块化地布局到多个相关文件中 ...
- 详谈 MySQL Online DDL
作为一名DBA,对数据库进行DDL操作非常多,如添加索引,添加字段等等.对于MySQL数据库,DDL支持的并不是很好,一不留心就导致了全表被锁,经常搞得刚入门小伙伴很郁闷又无辜,不是说MySQL支持O ...