日期:2014-05-18 浏览次数:20860 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[,] data = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; Console.WriteLine("----------------------------"); for (int k = 0; k < 12; k++) { int temp = data[0, 3]; for (int i = 3; i > 0; i--) { data[0, i] = data[0, i - 1]; } for (int i = 0; i < 3; i++) { data[i, 0] = data[i + 1, 0]; } for (int i = 0; i < 3; i++) { data[3, i] = data[3, i + 1]; } for (int i = 3; i > 0; i--) { data[i, 3] = data[i - 1, 3]; } data[1, 3] = temp; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) Console.Write(data[i, j] + "\t"); Console.WriteLine(); } Console.WriteLine("----------------------------"); } } } } /* ---------------------------- 5 1 2 3 9 6 7 4 13 10 11 8 14 15 16 12 ---------------------------- 9 5 1 2 13 6 7 3 14 10 11 4 15 16 12 8 ---------------------------- 13 9 5 1 14 6 7 2 15 10 11 3 16 12 8 4 ---------------------------- 14 13 9 5 15 6 7 1 16 10 11 2 12 8 4 3 ---------------------------- 15 14 13 9 16 6 7 5 12 10 11 1 8 4 3 2 ---------------------------- 16 15 14 13 12 6 7 9 8 10 11 5 4 3 2 1 ---------------------------- 12 16 15 14 8 6 7 13 4 10 11 9 3 2 1 5 ---------------------------- 8 12 16 15 4 6 7 14 3 10 11 13 2 1 5 9 ---------------------------- 4 8 12 16 3 6 7 15 2 10 11 14 1 5 9 13 ---------------------------- 3 4 8 12 2 6 7 16 1 10 11 15 5 9 13 14 ---------------------------- 2 3 4 8 1 6 7 12 5 10 11 16 9 13 14 15 ---------------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ---------------------------- Press any key to continue . . . */
------解决方案--------------------
把除中间数字外的数据 按 顺时针 放进1个队列, 队列右移1位后,再把队列放回对应的位置.
关键是取出4条边的数据 按 顺时针 放进1个队列,简单的可以用4个循环取出4条边.