ZOJ 2001
Adding Reversed Numbers
The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.
Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.
ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.
Output
For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.
Sample Input
3
24 1
4358 754
305 794
Sample Output
34
1998
1
package data20180311;
import java.util.Scanner;
public class AddReversedNum {
static int[] num1 = new int[100];
static int[] num2 = new int[100];
static String c1,c2;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int num = cin.nextInt();
while(num-->0){
c1 = cin.next();
c2 = cin.next();
for(int i=0;i<100;i++){
num1[i]=0;
num2[i]=0;
}
c1=getReversedNum(c1);
c2=getReversedNum(c2);
String rs = Add(c1,c2);
rs = getReversedNum(rs);
System.out.println(rs);
}
}
//24 1000 0001 24
public static String getReversedNum(String s){//得到相反数
int nZero=0;
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)=='0'){
nZero++;
}else{
break;
}
}
StringBuffer sb = new StringBuffer();
if(nZero==0){
for(int i=0;i<s.length();i++){
sb.append(s.charAt(s.length()-1-i));
}
}else{
for(int i=0;i<s.length()-nZero;i++){
sb.append(s.charAt(s.length()-1-nZero-i));
}
}
return sb.toString();
}
//高精度加法
public static String Add(String s1,String s2){
for(int i=0;i<s1.length();i++){
num1[s1.length()-i-1]=s1.charAt(i)-'0';//必须减‘0’ 否则为ASSII码值
}
for(int i=0;i<s2.length();i++){
num2[s2.length()-i-1]=s2.charAt(i)-'0';
}
int max = s1.length()>s2.length()?s1.length():s2.length();
for(int i=0;i<max;i++){
num1[i]=num1[i]+num2[i];
if(num1[i]>9){
num1[i]-=10;
num1[i+1]++;
}
}
int nZero=0;
for(int i=num1.length-1;i>=0;i--){
if(num1[i]==0){
nZero++;
}else{
break;
}
}
StringBuffer sb = new StringBuffer();
for(int i=0;i<num1.length-nZero;i++){
sb.append(num1[num1.length-nZero-1-i]);
}
return sb.toString();
}
}
昨天在先用c写的时候一直出错,原因就是c中字符串指针使用的不好。。
char * c1,*c2;
此时并不能直接进行输入操作 scanf(“%s %s”,c1,c2);
因为c1 c2并指向对应的内存,并不知道存储的位置。
可以使用:char str1[100],str2[100];
c1 =str1 ,c2=str2;
这样可以进行输入操作 scanf(“%s %s”,c1,c2);
然后输出的时候也直接printf(“%s %s”,c1,c2)即可。而不是printf("%s %s",*c1,*c2);