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