posts - 14,comments - 2,trackbacks - 0
/*
 * =====================================================================================
 *
 *       Filename:  distribute.cc
 *
 *    Description:  7 = 1+6
 *                         7 = 2+5
 *                         7 = 3+4
 *                         7 = 1+1+5
 *                         7 = 1+2+4
 *                         7 = 1+3+3
 *                         7 = 2+2+3
 *                         7 = 1+1+1+4
 *                         7 = 1+1+2+3
 *                         7 = 1+2+2+2
 *                         7 = 1+1+1+1+3
 *                         7 = 1+1+1+2+2
 *                         7 = 1+1+1+1+1+2
 *                         7 = 1+1+1+1+1+1+1
 *
 *        Version:  1.0
 *        Created:  08/31/09 13:50:08
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Xlyzalleyoop (WL), xlyz.os@gmail.com
 *        Company:  XLYZ
 *
 * =====================================================================================
 
*/

#include 
<iostream>
using namespace std;

typedef unsigned 
int u32;
const u32 MAX_RES = 1024;
int main(int argc, char* argv[])
{
    cout 
<< "input the data you wanna distribute:";
    u32 n 
= 0;
    cin 
>> n;
    
if (n/2 <= 0)
        
return 0;
    u32
** res = new u32*[MAX_RES];
    
for (u32 i=0; i<MAX_RES; i++)
        res[i] 
= new u32[n];

    u32 current_row
=0;
    
for (u32 i=1; i <= n/2; i++)
    
{
        u32 current_col
=0;
        
while (res[current_row][current_col])
            current_col
++;
        res[current_row][current_col] 
= i;
        res[current_row
++][++current_col] = n-i;
    }


    u32 start_row 
= 0;
    u32 end_row 
= current_row;

    u32 counter
=2;
    
while (counter++ < n)
    
{
        
for (u32 i=start_row; i<end_row; i++)
        
{
            u32 j
=0;
            
while (res[i][j])
                j
++;
            
if (j<1)
            
{
                cout 
<< "ERROR" << endl;
                
return 0;
            }

            u32 split_val 
= res[i][--j];
            u32 comp_val 
= res[i][--j];

            
for (u32 k=1; k<=split_val/2;k++)
                
if (k>=comp_val && (split_val-k)>=comp_val)
                
{
                    u32 m
=0;
                    
while (res[i][m])
                        res[current_row][m] 
= res[i][m++];
                    res[current_row][m
--= split_val - k;
                    res[current_row][m] 
= k;
                    
++current_row;
                    
if (MAX_RES == current_row)
                    
{
                        cout 
<< "the array is not enough\n";
                        
return 0;
                    }

                }

        }

        start_row 
= end_row;
        end_row 
= current_row;
    }


    u32 row
=0;
    u32 col
=0;
    u32 non_zero_col
=0;
    
while(res[row][col])
    
{
        cout 
<< n << " = ";
        
while (non_zero_col < n && res[row][non_zero_col])
            
++non_zero_col;
        
for (; col<non_zero_col-1++col)
            cout 
<< res[row][col] << " + ";
        cout 
<< res[row][col] << '\n';
        
++row;
        col
=0;
        non_zero_col
=0;
    }

    
for (u32 m=0; m <MAX_RES; m++)
        delete []res[m];
    delete []res;
    
return 0;
}

posted on 2009-10-27 23:15 Jiggy.Stone 阅读(299) 评论(0)  编辑 收藏 引用 所属分类: C++ Zealot!

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理