elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  [C#] Magic Square Builder :P
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [C#] Magic Square Builder :P  (Leído 4,671 veces)
Mace Windu

Desconectado Desconectado

Mensajes: 29


Flaming Our Skills Team


Ver Perfil WWW
[C#] Magic Square Builder :P
« en: 9 Enero 2009, 16:20 pm »

Código
  1. using System;
  2.  
  3. namespace CuadradoMagico
  4. {
  5.    class ProgramaAPP
  6.    {
  7.        static void Main(string[] args)
  8.        {
  9.            Console.ForegroundColor = ConsoleColor.Green;
  10.            Console.Title = "Magic Square Builder :P";
  11.            int n = 0;
  12.            do
  13.            {
  14.                Console.Clear();
  15.                Console.Write("Orden del cuadrado mágico (impar): ");
  16.  
  17.                try
  18.                {
  19.                    n = Int32.Parse(Console.ReadLine());
  20.                    if (CuadradoMagico.esPar(n))
  21.                    {
  22.                        Console.WriteLine("El orden debe ser impar.");
  23.                        Console.ReadKey(true);
  24.                    }
  25.                }
  26.                catch(Exception ex)
  27.                {
  28.                    Console.WriteLine(ex.Message);
  29.                    Console.ReadKey(true);
  30.                }
  31.            }
  32.            while (CuadradoMagico.esPar(n));
  33.  
  34.            CuadradoMagico cm = new CuadradoMagico(n);
  35.            cm.Crear();
  36.  
  37.            Console.WriteLine("\n\nEL cuadrado mágico de orden " + n + " es:\n\n");
  38.            cm.Mostrar();
  39.  
  40.            Console.ReadKey(true);
  41.        }
  42.    }
  43.  
  44.    class CuadradoMagico
  45.    {
  46.        private int[,] cm;
  47.        private int n;
  48.  
  49.        public CuadradoMagico(int orden)
  50.        {
  51.            n = orden;
  52.            cm = new int[n, n];
  53.        }
  54.  
  55.        public static bool esPar(int n)
  56.        {
  57.            if (n % 2 == 0) return true;
  58.            else return false;
  59.        }
  60.  
  61.        public void Crear()
  62.        {
  63.            int fil = 0, col = n / 2, fil_ant = 0, col_ant = n / 2, cont = 1;
  64.  
  65.            cm[fil, col] = cont++;
  66.  
  67.            while (cont <= n * n)
  68.            {
  69.                fil--;
  70.                col++;
  71.  
  72.                if (fil < 0) fil = n - 1;
  73.                if (col > n - 1) col = 0;
  74.                if (cm[fil, col] != 0)
  75.                {
  76.                    col = col_ant;
  77.                    fil = fil_ant + 1;
  78.                    if (fil == n) fil = 0;
  79.                }
  80.  
  81.                cm[fil, col] = cont++;
  82.                fil_ant = fil;
  83.                col_ant = col;
  84.            }
  85.        }
  86.  
  87.        public void Mostrar()
  88.        {
  89.            for (int i = 0; i < n; i++)
  90.            {
  91.                Console.Write("\t");
  92.                for (int j = 0; j < n; j++) Console.Write(cm[i, j] + "\t");
  93.                Console.WriteLine();
  94.            }
  95.        }
  96.    }
  97. }

Crea cuadrados mágicos de cualquier orden impar. Ejemplo:

Citar
8       1       6
3       5       7
4       9       2

Salu2


En línea

JMendez24

Desconectado Desconectado

Mensajes: 1


Ver Perfil
Re: [C#] Magic Square Builder :P
« Respuesta #1 en: 16 Abril 2012, 16:29 pm »

Hola,
Este codigo dice si una matriz es un cuadrado magico o no:

Descargue la solucion .NET completa: http://code.msdn.microsoft.com/Cuadrados-magicos-50df5132

Código:
using System; 
using System.Collections.Generic;
using System.Text;
 
namespace CuadradoMagico
{
    class Program
    {
        public static bool EsCuadradoPerfecto(int[,] matriz)
        {
            if (matriz.GetLength(0) != matriz.GetLength(1))
                return false;
            else
            {
                int suma1 = 0, suma2 = 0, suma3 = 0, suma4 = 0;
                for (int i = 0; i < matriz.GetLength(0); i++)
                {
                    for (int j = 0; j < matriz.GetLength(1); j++)
                    {
                        suma1 += matriz[i, j];
                        suma2 += matriz[j, i];
                        if (i == 0)
                        {
                            suma3 += matriz[j, j];
                            suma4 += matriz[j, matriz.GetLength(1) - 1 - j];
                        }
                    }
                    if (suma1 != suma2 || suma1 != suma3 || suma1 != suma4)
                        return false;
                    suma1 = 0;
                    suma2 = 0;
                }
                return true;
            }
        }
 
 
        static void Main(string[] args)
        {
            int[,] cuadrado ={ { 1, 8, 10, 15 }, { 12, 13, 3, 6 }, { 7, 2, 16, 9 }, { 14, 11, 5, 4 } };
            Console.WriteLine("La matriz cuadrada: ");
            for (int i = 0; i < cuadrado.GetLength(0); i++)
            {
                for (int j = 0; j < cuadrado.GetLength(1); j++)
                    Console.Write(cuadrado[i, j].ToString() + '\t');
                Console.WriteLine();
            }
            if (EsCuadradoPerfecto(cuadrado))
                Console.WriteLine("Es un cuadrado magico.");
            else
                Console.WriteLine("No es un cuadrado magico.");
            Console.ReadLine();
        }
    }
}


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
El cofundador de Google Wallet se pasa a Square
Noticias
wolfbcn 0 1,262 Último mensaje 3 Abril 2012, 15:12 pm
por wolfbcn
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines