代码改变世界,随手写了点代码解决了一个小学生级别的作业题,编程要从娃娃抓起

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

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

题目

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

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

 代码运算结果

 
A=4
B=7
C=9
D=2
原文地址:https://www.cnblogs.com/binghe021/p/13976167.html