Advertisement
Guest User

Untitled

a guest
May 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.StringTokenizer;
  3. //https://pastebin.com/n8x1xJgG
  4. //https://pastebin.com/pNUbViNM
  5. public class ccc04s3
  6. {
  7. static String [] [] formula = new String [10] [9];
  8. static int [] [] value = new int [10] [9];
  9.  
  10. public static void main (String [] args)
  11. {
  12. Scanner sc = new Scanner(System.in);
  13. for (int i = 0 ; i < 10 ; i++)
  14. {
  15. for (int j = 0 ; j < 9 ; j++)
  16. {
  17. formula [i] [j] = sc.next(); //contains real value or formula
  18. value [i] [j] = -1;
  19. }
  20. }
  21.  
  22. boolean change = true;
  23. while (change) //change is true represent
  24. //still some cell has no direct dependency on value
  25. {
  26. change = false;
  27. for (int i = 0 ; i < 10 ; i++)
  28. for (int j = 0 ; j < 9 ; j++)
  29. {
  30. StringTokenizer t = new StringTokenizer (formula [i] [j], "+");
  31. int v = 0;
  32. int x = 0;
  33. while (t.hasMoreTokens () && x != -1)
  34. {
  35. x = Value (t.nextToken ());
  36. if (x == -1)
  37. v = -1;
  38. else
  39. v += x;
  40. }
  41. if (value [i] [j] != v)
  42. {
  43. change = true;
  44. value [i] [j] = v;
  45. }
  46. }
  47. }
  48. for (int i = 0 ; i < 10 ; i++)
  49. {
  50. for (int j = 0 ; j < 9 ; j++)
  51. if (value [i] [j] == -1)
  52. System.out.print ("* ");
  53. else
  54. System.out.print (value [i] [j] + " ");
  55. System.out.println ();
  56. }
  57.  
  58. }
  59.  
  60. public static int Value (String s)
  61. {
  62. int v;
  63. if (s.charAt (0) >= 'A' && s.charAt (0) <= 'J')
  64. v = value [s.charAt (0) - 'A'] [s.charAt (1) - '1'];
  65. else
  66. v = Integer.parseInt (s);
  67. return v;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement