描述
输入
第一行为游戏局数正整数n,接下来n行每行由两部分组成,前者是Alice获得的16进制数串,后者是Bob获得的16进制数串,中间用空格隔开。
输出
每一行输出一局的胜者。
样例输入
样例输出
Alice和Bob在玩一个游戏,每一局中他们每人获得一个16进制数串,计算它对应的二进制数串中有多少个连续的1序列块(单个的1也算作序列块)。块数多者胜利,请判断游戏的胜者并输出,若平局则输出Tie,比如样例中的第一组数据为Alice: 0xfa425 Bob: 0xab3672。Alice的数据转换为2进制串后为11111010010000100101,其中有6个连续的1序列块。Bob的数据转换为2进制串后为 101010110011011001110010 其中有8个序列块,故胜者为Bob。
2 0xfa425 0xab3672 0x52c6 0xf429
Bob Tie
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javatest2;
import javax.swing.JOptionPane;
/**
*
* @author Sapphire
*/
public class JavaTest2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String times=JOptionPane.showInputDialog("请输入游戏局数");
int n=Integer.parseInt(times);
for(;n>0;n--){
String Alice=JOptionPane.showInputDialog("请输入Alice的数字");
String str1=hexString2binaryString(Alice);
System.out.println(str1);
int a=Count1(str1);
System.out.println(a);
String Bob=JOptionPane.showInputDialog("请输入Bob的数字");
String str2=hexString2binaryString(Bob);
System.out.println(str2);
int b=Count1(str2);
System.out.println(b);
if(a==b)
System.out.println("Tie");
else if(a<b)
System.out.println("Bob");
else
System.out.println("Alice");
}
}
public static String hexString2binaryString(String hexString)
{
if (hexString == null)
return null;
String bString = "", tmp;
for (int i = 2; i < hexString.length(); i++)
{
tmp = "0000"
+ Integer.toBinaryString(Integer.parseInt(hexString
.substring(i, i + 1), 16));
bString += tmp.substring(tmp.length() - 4);
}
return bString;
}
public static int Count1(String num)
{
int n=0;
char[] t=new char[num.length()];
for(int i=0;i<num.length();i++)
t[i]=num.charAt(i);
for(int i=1;i<num.length();i++){
if(t[i]=='0') {
if(t[i-1]=='1'){
n++;
}
}
}
if(t[num.length()-1]=='1')
n++;
return n;
}
}