每天学习一算法系列(7) (根据上排给出十个数,在其下排填出对应的十个数)

来源:互联网 发布:软件测试零基础 编辑:程序博客网 时间:2024/05/17 22:12

题目:

给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数
要求下排每个数都是先前上排那十个数在下排出现的次数。
上排的十个数如下:【0,1,2,3,4,5,6,7,8,9】

举个例子,
上排数值: 0,1,2,3,4,5,6,7,8,9
下排数值: 6,2,1,0,0,0,1,0,0,0
0在下排出现了6次,1在下排出现了2次,
2在下排出现了1次,3在下排出现了0次....

题目来源于:http://topic.csdn.net/u/20101011/16/2befbfd9-f3e4-41c5-bb31-814e9615832e.html

 

思路一:

这样的题目有点意思,但是仔细想想,它的原型跟八皇后有点类似,都是用回溯递归的方法去一次一次尝试,直到找出正确解。

具体的想法是:不断的去从下排数组中捉取在上排数组中对应位置中出现的个数,如果个数不对就更新下排数组中对应的值,只到找到正确值。(下排数组先初始为任意值)

如:

上排数组A:0,1,2,3,4,5,6,7,8,9
下排数组B:0,1,2,3,4,5,6,7,8,9
从上牌数组Index = 0开始,A[0] = 0,0在下排数组中的个数为1,那么下排数组B此时就要更新为:1,1,2,3,4,5,6,7,8,9,

Index = 1, A[1] = 1, 1在下排数组中的个数为2,那么下排数组B此时就要更新为:1,2,2,3,4,5,6,7,8,9,从此不断的往下进行,只要找不到正确值就一直往下进行,如果Index >= 数组长度时,那么重新恢复Index = 0再往下进行测试直到找出正确解。

 

代码如下:

[cpp] view plaincopy
  1. /*==================================== 
  2. Copyright by July       2010年10月18日 
  3. Modified by yuucyf.         2011.04.28 
  4. ======================================*/  
  5. #define     MAX_LEN                 10  
  6.   
  7. class C_NumberTB  
  8. {  
  9. private:  
  10.     int m_aryTop[MAX_LEN];  
  11.     int m_aryBottom[MAX_LEN];  
  12.     bool m_success;  
  13.   
  14. public:  
  15.     C_NumberTB();  
  16.   
  17. public:  
  18.     int* GetBottom();  
  19.     void SetNextBottom();  
  20.     int GetFrequecy(int nValue);  
  21. };  
  22.   
  23. C_NumberTB::C_NumberTB()  
  24. {  
  25.     m_success = false;  
  26.   
  27.     //format top  
  28.     for(int i = 0; i < MAX_LEN; i++)  
  29.     {  
  30.         m_aryTop[i] = i;  
  31.         m_aryBottom[i] = i;  
  32.     }  
  33. }  
  34.   
  35. int* C_NumberTB::GetBottom()  
  36. {  
  37.     int i = 0;  
  38.     while(!m_success)  
  39.     {  
  40.         i++;  
  41.         SetNextBottom();  
  42.     }  
  43.   
  44.     return m_aryBottom;  
  45. }  
  46.   
  47. //set next bottom  
  48. void C_NumberTB::SetNextBottom()  
  49. {  
  50.     bool bRet = true;  
  51.   
  52.     for(int i = 0; i < MAX_LEN; i++)  
  53.     {  
  54.         int nFreq = GetFrequecy(i);  
  55.         if(m_aryBottom[i] != nFreq)  
  56.         {  
  57.             m_aryBottom[i] = nFreq;  
  58.             bRet = false;  
  59.         }  
  60.     }  
  61.   
  62.     m_success = bRet;  
  63. }  
  64.   
  65.   
  66. //get frequency in bottom  
  67. int C_NumberTB::GetFrequecy(int nValue) //此处的nValue 即指上排的数i  
  68. {  
  69.     int nCnt = 0;  
  70.     for(int  i = 0; i < MAX_LEN; i++)  
  71.     {  
  72.         if(m_aryBottom[i] == nValue)  
  73.             nCnt++;  
  74.     }  
  75.   
  76.     return nCnt; //nCnt 即对应nFreq  
  77. }  
  78.   
  79.   
  80.   
  81. int _tmain(int argc, _TCHAR* argv[])  
  82. {  
  83.     C_NumberTB objTB;  
  84.     int* pResult = objTB.GetBottom();  
  85.   
  86.     for(int  i= 0 ;i < MAX_LEN; i++)  
  87.     {  
  88.         cout << *pResult++ << endl;  
  89.     }  
  90.   
  91.     return 0;  
  92. }  

0 0
原创粉丝点击