Electron Configuration of a Periodic Table Element

3

I would like to create a program that would electronically distribute atoms, but I'm not sure how to implement them. Would it be as follows? you would place the atomic number and it would return the electronic distribution (K, L, M, N, O, P, Q).

I thought of using if's considering that the layers have a maximum number of electrons:

K = 2
L = 8
M = 18
N = 32
O = 32
P = 18
Q = 8

But there are 2 rules that are:

  • The last layer can not have more than 8 electrons
  • The penultimate layer can not have more than 18 electrons
  • These two rules bug all my logic. The question is: Does anyone know a better way to implement this project?

    Edit: I got a good part now, but the remaining numbers are the same as the valence numbers. Can someone help me to correct this error?

    using System;
       using System.Collections.Generic;
       using System.Linq;
       using System.Text;
       using System.Threading.Tasks;
    
       namespace ConsoleApplication5
       {
        class Program
        {
            static void Main(string[] args)
            {
    
               int z = 53;
    
    
    
                int K = 1, L = 555, M = 555, N = 555, O = 555, P = 555, Q = 555;
    
                if (z >= 2) { z -= 2; K = 2; } else { K = z; }
    
    
                if (z >= 8) { z -= 8; L = 8; }
                if (z < 8) L = z;
    
    
                if (z >= 18) { z -= 18; M = 18; }
                if (z <= 8) M = z;
    
    
                if (z >= 32) { z -= 32; N = 32; }
                if (z < 32 && z >= 18) { z -= 18; N = 18; }
                if (z <= 8) N = z;
    
    
                if (z >= 32) { z -= 32; O = 32; }
                if (z > 32 && z >= 18) { z -= 18; O = 18; }
                if (z <= 8) O = z;
    
    
                if (z >= 18) { z -= 18; P = 18; }
                if (z <= 8) P = z;
    
    
                if (z >= 8) {z -= 8; Q = 8; }
                if (z <= 8) Q = z;
    
    
                Console.WriteLine("Valores:\nK = " + K + "\nL = " + L + "\nM = " + M + "\nN = " + N + "\nO = " + O + "\nP = " + P + "\nQ = " + Q);
                Console.ReadKey(); 
    
            }
        }
        }
    
        
    asked by anonymous 30.07.2014 / 01:58

    2 answers

    2

    I made a code that I think will help you.

    I did not test very well, but it will already be the basis for you.

    Edited:

    class Program
    {
    
        enum DefinicaoCamada
        {
            K = 2,
            L = 8,
            M = 18,
            N = 32,
            O = 32,
            P = 18,
            Q = 8
        }
    
        class Camada
        {
            public DefinicaoCamada defCamada;
            public int eletrons;
    
            public Camada(DefinicaoCamada defCamada)
            {
                this.defCamada = defCamada;
            }
    
        }
    
        static void Main(string[] args)
        {
    
            int totalEletrons = 53;
    
            Camada ultima = null;
    
            List<Camada> listaCamadas = new List<Camada>() {
                new Camada(DefinicaoCamada.K),
                new Camada(DefinicaoCamada.L),
                new Camada(DefinicaoCamada.M),
                new Camada(DefinicaoCamada.N),
                new Camada(DefinicaoCamada.O),
                new Camada(DefinicaoCamada.P),
                new Camada(DefinicaoCamada.Q)
            };
    
            Camada camadaAtual = listaCamadas[0];
            while (totalEletrons > 0)
            {
                if (camadaAtual.eletrons >= (int)camadaAtual.defCamada)
                {
                    camadaAtual = listaCamadas[listaCamadas.IndexOf(camadaAtual) + 1];
                }
                else
                {
                    camadaAtual.eletrons++;
                    totalEletrons--;
                    ultima = camadaAtual;
                }
            }
    
            int dif = 0;
    
            if (ultima.eletrons > 18)
            {
                dif = ultima.eletrons - 18;
                ultima.eletrons = 18;
                Camada prox = listaCamadas[listaCamadas.IndexOf(ultima) + 1];
                prox.eletrons = dif;
                ultima = prox;
            }
    
            if (ultima.eletrons > 8 && ultima.eletrons < 18)
            {
                dif = ultima.eletrons - 8;
                ultima.eletrons = 8;
                Camada prox = listaCamadas[listaCamadas.IndexOf(ultima) + 1];
                prox.eletrons = dif;
            }
    
            Console.WriteLine("Valores:");
            foreach (Camada c in listaCamadas)
            {
                Console.WriteLine(c.defCamada.ToString() + " = " + c.eletrons);
            }
            Console.ReadKey();
        }
    }
    
        
    30.07.2014 / 03:03
    1
    using System;
    using System.Collections.Generic;
    
    namespace Eletrons
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Passo 1: Detectar qual a última camada cheia.
                int z = 53, i = 0, somaCamadas = 0;
                var arrayCamadas = new int[] { 2, 8, 18, 32, 32, 18, 8 };
    
                foreach (var camada in arrayCamadas)
                {
                    somaCamadas += camada;
                    if (z - somaCamadas <= 0) break;
                    i++;
                }
    
                // Até aqui, i é a última camada preenchida com todos os elétrons.
                // Para o exemplo 53, i deverá ser 3.
    
                // Passo 2: Construir a lista que será retornada.
    
                var listaRetorno = new List<int>();
                var eletronsFaltantes = z;
                for (int j = 0; j < i; j++)
                {
                    listaRetorno.Add(arrayCamadas[j]);
                    eletronsFaltantes -= arrayCamadas[j];
                }
    
                // No exemplo, eletronsFaltantes será 25.
                // A regra de conferência para as duas últimas camadas ocorrerá apenas se i for 
                // maior ou igual a 3.
    
                if (i >= 3)
                {
                    if (eletronsFaltantes - 18 > 0)
                    {
                        listaRetorno.Add(18);
                        eletronsFaltantes -= 18;
                    }
                }
    
                listaRetorno.Add(eletronsFaltantes);
    
                listaRetorno.ForEach(Console.WriteLine);
                Console.ReadKey();
            }
        }
    }
    

    For 53, this solution returned K = 2, L = 8, M = 18, N = 18, O = 7. I think it's right.     

    30.07.2014 / 05:08