fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. class M{
  5. static String c(int i){
  6. if(i <= 0){
  7. return "";
  8. }
  9. ArrayList<Long> r = f(1),
  10. s = f(3),
  11. t = f(9),
  12. x = f(i);
  13. String z = "first meets river ",
  14. y = " at ";
  15. for(int j = 0; j < r.size(); j++){
  16. long u = r.get(j),
  17. v = s.get(j),
  18. w = t.get(j);
  19. if(x.contains(u)){
  20. return z+1+y+u;
  21. }
  22. if(x.contains(v)){
  23. return z+3+y+v;
  24. }
  25. if(x.contains(w)){
  26. return z+9+y+w;
  27. }
  28. }
  29. return "";
  30. }
  31.  
  32. static ArrayList f(long i){
  33. ArrayList<Long> l = new ArrayList();
  34. l.add(i);
  35. for(long j = 0, x; j < 9e4; j++){
  36. x = l.get(l.size() - 1);
  37. for(char c : (x + "").toCharArray()){
  38. x += Character.getNumericValue(c);
  39. }
  40. l.add(x);
  41. if(x > 16383){
  42. return l;
  43. }
  44. }
  45. return l;
  46. }
  47.  
  48. public static void main(String[] a){
  49. System.out.println(c(86));
  50. System.out.println(c(12345));
  51. System.out.println(c(0));
  52. }
  53. }
Success #stdin #stdout 0.15s 320576KB
stdin
Standard input is empty
stdout
first meets river 1 at 101
first meets river 3 at 12423