Created
July 25, 2012 17:16
-
-
Save torazuka/3177352 to your computer and use it in GitHub Desktop.
第2回 オフラインリアルタイムどう書くの参考問題: Java解答
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample2; | |
/** | |
* 問題: http://qiita.com/items/9d80de41903775296ca6 | |
*/ | |
public class SpinImage { | |
protected String execute(String string) { | |
String[] split = string.split(":"); | |
final int imageLength = Integer.valueOf(split[0]); | |
String originHex = split[1]; | |
final int hexLength = originHex.length(); | |
String binaryString = ""; | |
for (int i = 0; i < hexLength; i++) { | |
char c = originHex.charAt(i); | |
int dec = Integer.parseInt(String.valueOf(c), 16); | |
// 左端を0パディングした4桁の二進数に変換 | |
String bin = Integer.toBinaryString(dec); | |
for (int k = bin.length(); k < 4; k++) { | |
binaryString += "0"; | |
} | |
binaryString += bin; | |
} | |
final int binaryStrLength = binaryString.length(); | |
// 画像に不要な右端のbitを捨てる | |
if (imageLength * imageLength < binaryString.length()) { | |
binaryString = binaryString.substring(0, imageLength * imageLength); | |
} | |
// 回転前の画像 | |
int counter = 0; | |
char[][] source = new char[imageLength][imageLength]; | |
for (int i = 0; i < imageLength; i++) { | |
for (int k = 0; k < imageLength; k++) { | |
source[i][k] = binaryString.charAt(counter++); | |
} | |
} | |
// 回転後の画像 | |
char[][] dest = new char[imageLength][imageLength]; | |
int m = 0; | |
for (int i = 0; i < imageLength; i++) { | |
int p = imageLength - 1; | |
for (int k = 0; k < imageLength; k++) { | |
dest[i][k] = source[p][m]; | |
--p; | |
} | |
m++; | |
} | |
// 16進表記に戻す準備: 2進表現を1つにまとめる | |
String preResult = ""; | |
for (int i = 0; i < imageLength; i++) { | |
for (int k = 0; k < imageLength; k++) { | |
preResult += dest[i][k]; | |
} | |
} | |
// 2進表現の末尾に、16進変換に必要な0を継ぎ足す | |
for (int i = preResult.length(); i < binaryStrLength; i++) { | |
preResult += "0"; | |
} | |
String hexString = ""; | |
for (int i = 0; i < preResult.length(); i += 4) { | |
String s = ""; | |
if (i + 4 < preResult.length()) { | |
s += preResult.substring(i, i + 4); | |
} else { | |
s += preResult.substring(i); | |
} | |
int parseInt = Integer.parseInt(s, 2); | |
hexString += Integer.toHexString(parseInt); | |
} | |
StringBuilder result = new StringBuilder(String.valueOf(imageLength)); | |
result.append(":"); | |
// 16進表記を元の桁数に揃える(先頭に0パディング) | |
for (int i = preResult.length(); i < hexLength; i++) { | |
result.append("0"); | |
} | |
result.append(hexString); | |
return new String(result); | |
} | |
public static void main(String[] args) { | |
// すべてのテストデータは、SpinImageTest.javaを参照。 | |
SpinImage spin = new SpinImage(); | |
System.out.println(spin.execute("6:123456789")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample2; | |
import static org.junit.Assert.assertEquals; | |
import org.junit.Test; | |
/** | |
* テストデータ: http://qiita.com/items/9d80de41903775296ca6 | |
*/ | |
public class SpinImageTest { | |
SpinImage spin; | |
@Test | |
public void testExecute() throws Exception { | |
spin = new SpinImage(); | |
assertEquals("3:5b8 → 3:de0", "3:de0", spin.execute("3:5b8")); | |
assertEquals("1:8 → 1:8", "1:8", spin.execute("1:8")); | |
assertEquals("2:8 → 2:4", "2:4", spin.execute("2:8")); | |
assertEquals("2:4 → 2:1", "2:1", spin.execute("2:4")); | |
assertEquals("2:1 → 2:2", "2:2", spin.execute("2:1")); | |
assertEquals("3:5d0 → 3:5d0", "3:5d0", spin.execute("3:5d0")); | |
assertEquals("4:1234 → 4:0865", "4:0865", spin.execute("4:1234")); | |
assertEquals("5:22a2a20 → 5:22a2a20", "5:22a2a20", | |
spin.execute("5:22a2a20")); | |
assertEquals("5:1234567 → 5:25b0540", "5:25b0540", | |
spin.execute("5:1234567")); | |
assertEquals("6:123456789 → 6:09cc196a6", "6:09cc196a6", | |
spin.execute("6:123456789")); | |
assertEquals("7:123456789abcd → 7:f1a206734b258", "7:f1a206734b258", | |
spin.execute("7:123456789abcd")); | |
assertEquals("7:fffffffffffff → 7:ffffffffffff8", "7:ffffffffffff8", | |
spin.execute("7:fffffffffffff")); | |
assertEquals("7:fdfbf7efdfbf0 → 7:ffffffffffc00", "7:ffffffffffc00", | |
spin.execute("7:fdfbf7efdfbf0")); | |
assertEquals("8:123456789abcdef1 → 8:f0ccaaff78665580", | |
"8:f0ccaaff78665580", spin.execute("8:123456789abcdef1")); | |
assertEquals("9:112233445566778899aab → 9:b23da9011d22daf005d40", | |
"9:b23da9011d22daf005d40", | |
spin.execute("9:112233445566778899aab")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment