经典算法
魔方算法
/* * 魔方算法 * */
public void MagicCube() { Console.Write("请输入魔方的边长:"); string strInput = Console.ReadLine(); int nBorderSize = Int32.Parse(strInput); //边长 int nRow,nCol; //行,列 nRow = 0; //初始行为0 nCol = nBorderSize / 2 ;//初始列为边长的一半 int[,] array = new int[nBorderSize,nBorderSize]; array[nRow,nCol] = 1; //初始值为1 for( int nCount = 2; nCount <= nBorderSize * nBorderSize; nCount++ ) //从 2 到总数填充到数组中 { if( nRow == 0 ) //如果行到最小边界,则转到最大边界 { nRow = nBorderSize - 1; } else //否则到上一行 { nRow--; } if( nCol == nBorderSize -1 ) //如果列到最大边界,则转到最小边界 { nCol = 0; } else //否则到下一列 { nCol++; } if( array[nRow,nCol] != 0 ) //如果当前位置已经有了元素,则转到下两行,前一列的位置 { nRow += 2; if( nRow < 0 ) //如果行到最小边界,则转到最大边界 { nRow = nBorderSize - 1; } if( nRow > nBorderSize - 1 ) //如果行到最大边界,则转到最小边界的下一行 { nRow -= nBorderSize; } nCol--; if( nCol < 0) //如果列到最小边界,则转到最大边界 { nCol = nBorderSize - 1; } } array[nRow,nCol] = nCount; //赋值 }
for( int i = 0; i < array.GetLength( 0 ); i++ ) { for( int j = 0; j < array.GetLength( 1 ); j++ ) { Console.Write( "{0} ",array[i,j] ); if( j == nBorderSize - 1 ) //输出一行的长度为边长,则到下一行输出 { Console.WriteLine(); } } } }
螺旋算法(左下开始)
public void Helix() { Console.Write("请输入螺旋的边长:"); string strInput = Console.ReadLine(); int nBorderSize = Int32.Parse(strInput); int[,] array = new int[nBorderSize,nBorderSize]; int nDirect = 0; int nRow, nCol; //坐标 nRow = 0; //坐标从0,0开始 nCol = 0; for( int nCount = 1; nCount < nBorderSize * nBorderSize + 1; nCount++ ) //从 2 开始赋值,一直到元素总数为止
{ array[nRow,nCol] = nCount;
switch( nDirect ) { case 0 : nRow = ( nRow + 1 + nBorderSize ) % nBorderSize; if( array[nRow,nCol] != 0 ) { nDirect = ( nDirect + 1 ) % 4; nRow = ( nRow - 1 + nBorderSize ) % nBorderSize; nCol = ( nCol + 1 + nBorderSize ) % nBorderSize; } break; case 1 : nCol = ( nCol + 1 + nBorderSize ) % nBorderSize; if( array[nRow,nCol] != 0 ) { nDirect = ( nDirect + 1 ) % 4; nCol = ( nCol - 1 + nBorderSize ) % nBorderSize; nRow = ( nRow - 1 + nBorderSize ) % nBorderSize; } break; case 2 : nRow = ( nRow - 1 + nBorderSize ) % nBorderSize; if( array[nRow,nCol] != 0 ) { nDirect = ( nDirect + 1 ) % 4; nRow = ( nRow + 1 + nBorderSize ) % nBorderSize; nCol = ( nCol - 1 + nBorderSize ) % nBorderSize; } break; case 3 : nCol = ( nCol - 1 + nBorderSize ) % nBorderSize; if( array[nRow,nCol] != 0 ) { nDirect = ( nDirect + 1 ) % 4; nCol = ( nCol + 1 + nBorderSize ) % nBorderSize; nRow = ( nRow + 1 + nBorderSize ) % nBorderSize; } break; } } for( int i = 0; i < array.GetLength( 0 ); i++ ) { for( int j = 0; j < array.GetLength( 1 ); j++ ) { Console.Write( "{0} ",array[i,j] ); if( j == nBorderSize - 1 ) //输出一行的长度为边长,则到下一行输出 { Console.WriteLine(); } } }
}
奶牛繁殖算法
/* * 奶牛繁殖问题 * */ public void CowBreed() { Console.WriteLine("请输入年数:"); string strInput = Console.ReadLine(); int nYears = Int32.Parse(strInput);
int[] array = new int[nYears];
int nCowSum = 1; //开始为一头 array[0] = nCowSum; //第一年为一头 for( int nI = 1; nI < nYears ; nI++ ) { if( nI < 3) //前三年都为一头 { nCowSum = 1; } else { nCowSum = nCowSum + array[nI - 3]; //今年的头数 = 去年的头数 + 倒数第三年的头数 } array[nI] = nCowSum; } for(int nI = 1,nJ = 0; nJ < array.Length; nJ++ ) { Console.WriteLine("第{0}年奶牛的头数为:{1},",nI,array[nJ]); nI++; } }