1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace ConsoleApplication1  
  6. {  
  7.  
  8.     /*  
  9.      *   
  10.      * 腾讯算法面试题 算法与代码   
  11.      *   题目是这样的:给你10分钟时间,根据上排给出十个数,  
  12.      * 在其下排填出对应的十个数。  
  13.      * 要求下排每个数都是先前上排对应那个数在下排十个数中出现的次数。   
  14.  
  15.     例如:  
  16.         上排的十个数如下:   
  17.         0  1  2  3  4  5  6  7  8  9 ,那么下排的数应该是  
  18.      *  6  2  1  0  0  0  1  0  0  0  
  19.      *   
  20.      * 其实这是有规律可循的:  
  21.      * 不仅0~9有唯一解,0~n都只有唯一解。关键是最后面一个1它可以左右移动,1和2下面的数永远是2和1,  
  22.      * 0下面对应的数为n-3(n>=3),上排数n-3下面对应的数为1,其它上排数下面对应为0就ok了。  
  23.      * 有了这个一般性的结论再大的数都可以马上给出答案。   
  24.      *   
  25.      * 但是如果不是连续的数呢??例如 5 10 50 24 11 ,上面的方法就不好使了吧?  
  26.      * 下面的方法就是万能的方法了,不论给出的数字是否连续,都可以用  
  27.      */ 
  28.  
  29.  
  30.     class Test  
  31.     {  
  32.         int[] top;  
  33.         public int[] Top  
  34.         {  
  35.             get { return top; }  
  36.             set { top = value; }  
  37.         }  
  38.  
  39.         int[] bottom;  
  40.         public int[] Bottom  
  41.         {  
  42.             get { return bottom; }  
  43.             set { bottom = value; }  
  44.         }  
  45.  
  46.         bool flag = false;  
  47.         public bool Flag  
  48.         {  
  49.             get { return flag; }  
  50.             set { flag = value; }  
  51.         }  
  52.  
  53.         int length;  
  54.         public int Length  
  55.         {  
  56.             get { return length; }  
  57.             set { length = value; }  
  58.         }  
  59.  
  60.         public Test(int length)  
  61.         {  
  62.             this.Length = length<4?4:length;  
  63.             Top = new int[Length];  
  64.             Bottom = new int[Length];  
  65.         }  
  66.  
  67.         public static void Main()  
  68.         {  
  69.             /*测试1*/ 
  70.             Console.WriteLine("测试1");  
  71.             Test t1 = new Test(20);  
  72.             t1.Inital();  
  73.             //如果Top的长度小于4,不可能实现,除非不是从0开始的连续数!  
  74.             t1.SetBottom();  
  75.             t1.Print(t1.Top);  
  76.             t1.Print(t1.Bottom);  
  77.  
  78.  
  79.  
  80.             /*测试2  
  81.              *  
  82.              * 当Top不是从0开始的连续的数字时候,结果可能不止一种!  
  83.              */ 
  84.  
  85.             Console.WriteLine("测试2");  
  86.             Test t2 = new Test(10);  
  87.             t2.Top = new int[] { 0, 5, 12, 10, 6, 11, 2, 8, 4, 7 };  
  88.  
  89.             for (int j = 0; j < 20; j++)  
  90.             {  
  91.                 //每次循环确保Flag为false;  
  92.                 t2.Flag = false;  
  93.                 for (int i = 0; i < t2.Bottom.Length; i++)  
  94.                 {  
  95.                     t2.Bottom[i] = j;  
  96.                 }  
  97.  
  98.                 t2.SetBottom();  
  99.                 Console.WriteLine(j + "::");  
  100.                 t2.Print(t2.Top);  
  101.                 t2.Print(t2.Bottom);  
  102.                 Console.WriteLine();  
  103.             }  
  104.  
  105.         }  
  106.  
  107.  
  108.  
  109.  
  110.         void Inital()  
  111.         {  
  112.             for (int i = 0; i < Top.Length; i++)  
  113.             {  
  114.                 Top[i] = i;  
  115.             }  
  116.         }  
  117.  
  118.         void SetBottom()  
  119.         {  
  120.             while (!Flag)  
  121.             {  
  122.                 //循环逐一对Bottom赋值,直到Bottom中没有数值变动为止!  
  123.                 SetNextBottom();  
  124.             }  
  125.  
  126.         }  
  127.  
  128.         //对Bottom逐一赋值!  
  129.         void SetNextBottom()  
  130.         {  
  131.             int a;  
  132.             bool temp = true;  
  133.             for (int i = 0; i < Bottom.Length; i++)  
  134.             {  
  135.                 a = GetSum(Top[i]);  
  136.                 if (Bottom[i] != a)  
  137.                 {  
  138.                     Bottom[i] = a;  
  139.                     temp = false;  
  140.                 }  
  141.             }  
  142.             Flag = temp;  
  143.         }  
  144.  
  145.         //Bottom中出现num的次数!  
  146.         int GetSum(int num)  
  147.         {  
  148.             int count = 0;  
  149.             for (int i = 0; i < Bottom.Length; i++)  
  150.             {  
  151.                 if (Bottom[i] == num)  
  152.                 {  
  153.                     count++;  
  154.                 }  
  155.             }  
  156.             return count;  
  157.         }  
  158.         //数字输出方法,1位数与2位数!  
  159.         string ReturnNumber(int num)  
  160.         {  
  161.             string s = "" + num;  
  162.             if (s.Length == 1)  
  163.             {  
  164.                 return " " + s + "," + "  ";  
  165.             }  
  166.             else if (s.Length == 2)  
  167.             {  
  168.                 return s.Substring(0, 1) + " " + s.Substring(1, 1) + ", ";  
  169.             }  
  170.             return "  ";  
  171.         }  
  172.         //输出数组!  
  173.         void Print(int[] a)  
  174.         {  
  175.             for (int i = 0; i < a.Length; i++)  
  176.             {  
  177.                 Console.Write(ReturnNumber(a[i]));  
  178.             }  
  179.             Console.WriteLine();  
  180.         }  
  181.  
  182.  
  183.     }  
  184. }