我正在做一个抽奖彩票项目,根据人的6位彩票号码与6位中奖号码的匹配情况,可以赢得多个奖品。
如果中奖号码中的3个序号与个人彩票的3个序号相匹配,则可以获得其中一个奖品。
例如:如果彩票上的号码是123456,如果中奖号码在1-3位置有123,在2-4位置有234,在3-5位置有345,在4- 6位置有456,则中奖号码为获胜者。
我想知道我如何在彩票号码/彩票是整数的情况下做到这一点?
发布于 2015-07-14 14:58:42
您可以使用模算法进行测试,一次测试3位数字,然后重复除以10来测试不同的3位数组:
static boolean match3(int ticket, int winner)
{
for(int i = 0; i < 4; i++)
{
if((ticket % 1000) == (winner % 1000))
return true;
ticket /= 10; winner /= 10;
}
return false;
}假设少于6位的数字具有前导零。
发布于 2015-07-14 15:01:33
您可以使用以下逻辑来满足您的需求。
public class Test {
public static void main(String[] args) {
//This is containing all possible ascending combination
String possibleComb = "0123456789";
//This would be variable and will be part of your method argument
String ticketNumber = "123456";
//This loop is to get all 3 combination in only forward direction
for(int i = 0; i < ticketNumber.length() - 1;){
String tmpSubStr = ticketNumber.substring(i, i + 3);
if(possibleComb.indexOf(tmpSubStr) == (Integer.parseInt(tmpSubStr) / 100)) {
System.out.println("He is Lucky Customer of " + (i + 1) + "-" + (i + 3));
}
System.out.println();
i = i + 3;
}
}
}https://stackoverflow.com/questions/31399151
复制相似问题