偶然间发现一道小学生级别的作业题,发现很适合写点代码来得到结果。于是乎有了本篇文章。

代码改变世界,程序一秒钟不到就得出正确结果了。编程要从娃娃抓起。

题目

 

  1. 1 class Program
  2. 2 {
  3. 3 static void Main(string[] args)
  4. 4 {
  5. 5 //使用计时器记录所耗时间
  6. 6 Stopwatch stopwatch = new Stopwatch();
  7. 7 stopwatch.Start();
  8. 8
  9. 9 TryIt();
  10. 10
  11. 11 stopwatch.Stop();
  12. 12
  13. 13 Console.WriteLine("耗时:" + stopwatch.ElapsedMilliseconds.ToString() + "毫秒");
  14. 14 Console.ReadKey();
  15. 15
  16. 16 }
  17. 17
  18. 18 private static void TryIt()
  19. 19 {
  20. 20 Console.WriteLine("猜字母代表的数字");
  21. 21 Console.WriteLine("D + (C * 10 + D) + (B * 100 + C * 10 + D) + (A * 1000 + B * 100 + C * 10 + D) == 5678");
  22. 22
  23. 23 //定义ABCD四个变量
  24. 24 int A = 0;
  25. 25 int B = 0;
  26. 26 int C = 0;
  27. 27 int D = 0;
  28. 28
  29. 29 //进行4组循环
  30. 30 for (int i = 0; i < 10; i++)
  31. 31 {
  32. 32 A = i;
  33. 33 for (int j = 0; j < 10; j++)
  34. 34 {
  35. 35 B = j;
  36. 36 for (int k = 0; k < 10; k++)
  37. 37 {
  38. 38 C = k;
  39. 39 for (int l = 0; l < 10; l++)
  40. 40 {
  41. 41 D = l;
  42. 42
  43. 43 //判断是否满足等式的条件
  44. 44 if (D + (C * 10 + D) + (B * 100 + C * 10 + D) + (A * 1000 + B * 100 + C * 10 + D) == 5678)//加法模式的等式
  45. 45 //if ((A*1000)+(2*B*100)+(3*C*10)+4*D==5678)//乘法模式的等式 和加法模式结果一样
  46. 46 {
  47. 47 //因ABCD分别代表不同的字母,所以字母各不相同
  48. 48 if (A != B && A != C && A != D && B != C && B != D && C != D)
  49. 49 {
  50. 50 Console.WriteLine("--------------------------");
  51. 51 Console.WriteLine("A=" + A);
  52. 52 Console.WriteLine("B=" + B);
  53. 53 Console.WriteLine("C=" + C);
  54. 54 Console.WriteLine("D=" + D);
  55. 55 }
  56. 56 }
  57. 57 }
  58. 58 }
  59. 59 }
  60. 60 }
  61. 61 Console.WriteLine("--------------------------");
  62. 62 Console.WriteLine("结束");
  63. 63 }
  64. 64
  65. 65 }

C#代码实现 使用暴力破解的方法得出各字母的结果,计算机最主要的作用就是用来执行计算任务的。

 

 代码运算结果

 
A=4
B=7
C=9
D=2

 

版权声明:本文为binghe021原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/binghe021/p/13976167.html