Created
February 7, 2012 05:07
-
-
Save shixish/1757327 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class Acm2 { | |
public static void main(String[] args) { | |
int p = solve(3121); | |
if (p>0){ | |
System.out.format("Found a soluion with %d persons\n", p); | |
}else{ | |
System.out.format("No solution found \n"); | |
} | |
} | |
static int solve(int input){ | |
for (int persons = 1; persons < 15; persons++){ | |
int coconuts = input; | |
for (int p=0;p<=persons;p++){ | |
int remove = (int)(coconuts/persons); | |
int extra = coconuts%persons; | |
coconuts -= remove+1; | |
if (extra == 0 && p == persons){//then we are ok | |
return persons; | |
}else if (extra != 1){ | |
break; //solution no good | |
} | |
//System.out.format("Removing: %d, extra: %d, coconuts: %d \n", remove, extra, coconuts); | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment