[ 問題 - 原文 ]

For a given positive integer X we can obtain the reversed positive integer Rev(X) by reversing the order of X's digits and removing leading zeroes. For example, if X = 123 then Rev(X) = 321; and if X = 100, then Rev(X) = 1.

You will be given two positive integers x and y. Return their reversed sum, which is defined as Rev(Rev(x) + Rev(y)).

--

[ 問題 - 要約 ]

与えられた正の数を逆から並べて、その数値がもしゼロから始まるようであれば
そのゼロを取った数値を戻すRev関数を用意します。

そのRev関数を用いて与えられた2つの正数の Rev( Rev(x) + Rev(y) ) を求める問題。

---

[ 私の解答 ]

使用言語: JAVA


public class ReversedSum {

int getReversedSum(int x, int y) {
return rev( rev(x) + rev(y) );
}

int rev( int x ){
String value = String.valueOf(x);
String ret = "";
int length = value.length();
for( int i=length-1; i>=0; i-- ){
ret += value.charAt( i );
}
return Integer.parseInt( ret );
}
}


---

[ トッププレイヤーの解答 ]

名前:damnerd
使用言語:C++


class ReversedSum
{
public:
int reverse(int n)
{
int r=0;
while(n>0)
{
r=10*r+n%10;
n/=10;
}
return r;
}
int getReversedSum(int x, int y)
{
return reverse(reverse(x)+reverse(y));
}
};


---

[ 考察 ]

おおむね同じコードのようだ。
ただ、私は楽をするために一時的に文字列にして
その文字列を逆順に並べた後に数値に戻す Rev関数を書いたが
このトッププレイヤーは数値のままで処理したようだ。

実際問題、この方が処理速度は速い。
しかし、この問題においては処理速度は問題ではない。
# ワーストケースでもそんなに桁数が多くなるわけではなく、
# 数値を反転する処理の処理時間がかかりすぎて、
# システムテストを通過できない事にはならないはずなので

というわけで、今回はそつなく解答できたと言うことだろう。