一、题目大意

反转两个数字并相加,所得结果崽反转。反转规则:如果数字后面有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(用字符串反转数字)的更多相关文章

  1. POJ 1504 Adding Reversed Numbers

    /*Sample Input 3 24 1 4358 754 305 Sample Output 34 1998 */ 值得总结的几点就是: 1.atoi函数将字符串转化为整型数字(类似于强制转换) ...

  2. POJ 1504 Adding Reversed Numbers (水题,高精度整数加法)

    题意:给两个整数,求这两个数的反向数的和的反向数,和的末尾若为0,反向后则舍去即可.即若1200,反向数为21.题目给出的数据的末尾不会出现0,但是他们的和的末尾可能会出现0. #include &l ...

  3. zoj 2001 Adding Reversed Numbers

    Adding Reversed Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB The Antique Comedians of M ...

  4. ACM Adding Reversed Numbers(summer2017)

    The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancien ...

  5. poj1504 Adding Reversed Numbers

    Adding Reversed Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17993 Accepted: 9 ...

  6. zoj2001 Adding Reversed Numbers

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2001 Adding Reversed Numbers Time ...

  7. POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误

    ------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况 ...

  8. Java算法之字符串反转分析

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 在基本的工作内容开发中,算法不会显得那么重要,而在百万级别的时候,差距非常大,今天带大家研究下常见的 ...

  9. 字符串反转C#的实现

    字符串反转是面试过程中出现频率较高的算法题,今天一个牛同事让我用C#帮他实现这个算法,前提当然是不能使用类库. 例如: how are you 的反转结果为 you are how. 算法1: 是我当 ...

随机推荐

  1. Unable to determine IP address from host name

  2. Frobenius Norm

    http://mathworld.wolfram.com/FrobeniusNorm.html

  3. cocos2dx使用cocostudio导出的animation

    local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...

  4. hdoj 1455 Sticks 【dfs】

    题意:找最短的木棍可以组成的长度, hdoj  1518 的加强版 代码: #include <stdio.h> #include <string.h> #include &l ...

  5. [原创]Scala学习:函数的定义

    方式一:标准的定义函数 def 函数名(参数1: 参数类型,参数2: 参数类型): 返回值类型 = { 函数体 } 例子 def max(x: Int,y: Int): Int ={ if(x > ...

  6. 【leetcode刷题笔记】Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  7. 一步一步带你分析 requirejs

    详细源代码一共就2000多行,来看我这篇分析的同学应该都下载下来了,好了,话不多说,开始: 代码的开头就出现3个全局变量: requirejs, require, define var require ...

  8. Delphi 中关闭指定进程的方法

    Uses Windows, SysUtils, Tlhelp32 ; Function KillTask( ExeFileName: String ): Integer ; //关闭进程 Functi ...

  9. QT MVC 模型/视图

    1. 模型视图实例一, QFileSystemModel  QTreeView ,model/view示例. #include <QApplication> #include <QF ...

  10. 大话设计模式--职责连模式 Chain of Resposibility -- C++实现实例

    1. 职责链模式: 使多个对象都有机会处理请求,从而避免请求发送者和接受者之间的耦合关系,将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它. 当客户提交一个请求时,请求是沿着链传递直 ...