fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. /* quite possibly the worst code ever?
  11. brute forces through the ratio relationships of the first 1000 numbers
  12. if any of the numbers a 8:5 ratio and a 9:7 ratio after adding 19 to the
  13. 2nd number and 4 to the first, then it outputs the number.
  14.  
  15. Changed code slighly to allow it to compile online with this editor.
  16.  
  17. */
  18. public static void main (String[] args) throws java.lang.Exception
  19. {
  20.  
  21.  
  22. for (int girl = 8; girl < 1000; girl++){
  23.  
  24. for (int boy = 5; boy < 1000; boy++){
  25.  
  26. int gcd = gcd(girl,boy);
  27. //checks if the ratio is a 8:5
  28. if (girl / gcd(girl,boy) == 8 && boy / gcd(girl,boy) == 5 ){
  29.  
  30. int girl2 = girl;
  31. int boy2 = boy;
  32.  
  33. // adds a number of people to the corresponding gender
  34. girl2 += 4;
  35. boy2 += 19;
  36.  
  37. int gcd2 = gcd(girl2,boy2);
  38.  
  39. //checks if the ratio is a 9:7
  40. if (girl2 / gcd2 == 9 && boy2 / gcd2 == 7 ){
  41. System.out.println(girl + " g : " + boy + "b");
  42.  
  43.  
  44. }
  45. }
  46. }
  47. }
  48. }
  49.  
  50. //Finds greatest common denominator
  51. public static int gcd(int p, int q) {
  52. if (q == 0){
  53. return p;
  54. }
  55. else{
  56. return gcd(q, p % q);
  57. }
  58. }
  59. }
Success #stdin #stdout 0.12s 711168KB
stdin
Standard input is empty
stdout
104 g : 65b