Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fórum ➡ Rotating a Square Matrix One Cell Counter Clockwise
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav http://rextester.com/users/376 // in reply to the MSDN thread http://bit.ly/2lRPDjO namespace Rextester { public class Program { static void Write(string s,int[,] m){ Console.WriteLine("\n{0} {1}✖{1}:",s,m.GetLength(0)); for(int i=0;i<m.GetLength(0);++i){ for(int j=0;j<m.GetLength(1);++j){ Console.Write("{0,4} ",m[i,j]); } Console.WriteLine(); } } public static void Main(string[] args) { const int k=15; int[,] mk=new int[k,k]; for(int i=0;i<k*k;++i)mk[i/k,i-(i/k)*k]=i+1; int[,] m5={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}}; // int[,] m4={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; int[,] m3={{1,2,3},{4,5,6},{7,8,9}}; int[,] m2={{1,2},{3,4}}; int[,] m1={{1}}; dynamic[] ds={m1,m2,m3,m4,m5,mk}; // for(int i=0;i<ds.Length;++i){ int[,] m=ds[i] as int[,]; Write("Original",m); RotateOneCellCounterClockwise(ref m); Write("Rotated",m); Console.WriteLine("\n{0}",new string('❇',58)); } Console.WriteLine("\n\nHello, world!"); } static void RotateOneCellCounterClockwise(ref int[,] m){ int l=m.GetLength(0); if(l!=m.GetLength(1))throw new Exception("The matrix is not square"); int middle=l/2; bool odd=(l%2)==1; int[,] r=new int[l,l]; for(int i=0;i<l;++i){ int corner=l-i-1; bool firsthalf=odd?i<=middle:i<middle; for(int j=0;j<l;++j){ int v=m[i,j]; if(firsthalf){ if(j<i){ r[i+1,j]=v; }else if(j==i){ if(odd&&j==middle){r[i,j]=v;continue;} r[i+1,j]=v; }else if(j<=corner){ r[i,j-1]=v; }else{ r[i-1,j]=v; } }else{ if(j<corner){ r[i+1,j]=v; }else if(j<i){ r[i,j+1]=v; }else{ r[i-1,j]=v; } } } } m=r; } } }
run
|
edit
|
history
|
help
0
Min max elements swap of the array
gen retuen obj
Lotto35Generator
Plt-D v.0.9
C# program to Determine if Two Words Are Anagrams of Each Other
Wild West of Meming
Simplified palindrome test using stack
fsdf
ggg
MI