ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1432. Twofold Luck

Why WA?
Posted by Georgeek 6 Jul 2011 02:45
Give me more examples to test, pls.

import java.util.Scanner;


import java.util.Scanner;


public class DoubleHappiness1432 {

    private static String number;
    private static int[] ticket;
    private static int[] ticket2;
    private static boolean flug = true;
    private static int sum1 = 0;
    private static int sum2 = 0;
    private static boolean pr = false;
    private static int sum12 = 0;
    private static int sum22 = 0;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        number = scanner.nextLine();
        if(number.length()%2==0 && number.length()>=4 && number.length()<=1500){
            toInt();
            stop:while (flug) {
                flug=false;
                start:for (int j = number.length()-1; j >= 0; j--) {
                    if (ticket[j] < 9) {
                        ticket[j]+=1;
                        if(summary() || summary1()){
                            pr  = true;
                            break stop;
                        }else toNull(j);
                        flug=true;
                        break start;
                    }
                }
            }
            if(!pr)    System.out.print("No solution");
        }
    }
    private static void toInt(){
        ticket = new int[number.length()];
        ticket2 = new int[number.length()];
        for (int i = 0; i < number.length(); i++) {
            ticket[i] = Character.digit(number.charAt(i), 10);
        }
    }
    private static void toNull(int i){
        for (int j = i+1; j < number.length(); j++) {
            ticket[j]=0;
        }
    }
    private static boolean summary(){
        for (int i = 0; i < number.length()/2; i++) {
            sum1 += ticket[i];
        }
        for (int i = number.length()/2; i < number.length(); i++) {
            sum2 += ticket[i];
        }
        if (sum1==sum2) {
            System.arraycopy(ticket, 0, ticket2, 0, ticket.length);
            for (int i = number.length()-1; i >=0; i--) {
                if (ticket[i] < 9) {
                    ticket[i]+=1;
                    flug=true;
                    toNull(i);
                    break;
                }
            }
            if (flug) {
                for (int i = 1; i < ticket.length; i+=2) {
                    sum12 +=ticket[i];
                    sum22 +=ticket[i-1];
                }
            }else{return false;}
            if(sum12==sum22){
                    for (int i = 0; i < number.length(); i++) {
                        System.out.print(ticket2[i]);
                    }
                    System.out.print(" ");
                    for (int i = 0; i < number.length(); i++) {
                        System.out.print(ticket[i]);
                    }
                    return true;
            }else{sum12=0;sum22=0;return false;}
        }else{sum1=0;sum2=0;return false;}
    }

    private static boolean summary1(){
        for (int i = 1; i < ticket.length; i+=2) {
            sum12 +=ticket[i];
            sum22 +=ticket[i-1];
        }
        if(sum12==sum22){
            System.arraycopy(ticket, 0, ticket2, 0, ticket.length);
            for (int i = number.length()-1; i >=0; i--) {
                if (ticket[i] < 9) {
                    ticket[i]+=1;
                    flug=true;
                    toNull(i);
                    break;
                }
            }
            if (flug) {
                for (int i = 0; i < number.length()/2; i++) {
                    sum1 += ticket[i];
                }
                for (int i = number.length()/2; i < number.length(); i++) {
                    sum2 += ticket[i];
                }
                if (sum1==sum2) {
                    for (int i = 0; i < number.length(); i++) {
                        System.out.print(ticket2[i]);
                    }
                    System.out.print(" ");
                    for (int i = 0; i < number.length(); i++) {
                        System.out.print(ticket[i]);
                    }
                    return true;
                }else{return false;}
            }else{sum1=0;sum2=0;return false;}
        }else{sum12=0;sum22=0;return false;}
    }
}

Edited by author 06.07.2011 03:58
Re: Why WA?
Posted by Olja Soboleva {USU} 3 May 2012 01:28
Try this test:
316496

My program returns:
317559 317560

and yours:
318659 318660