Advertisement
GraionDilach

Számjegyazonosságkereső/összegkiíró

Feb 17th, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[] DasArray = new int[1];
  13.             int ArrayCount = 0;
  14.  
  15.             bool ell;
  16.             int Current;
  17.             bool superell = true;
  18.  
  19.             do
  20.             {
  21.                 do
  22.                 {
  23.                     Console.Write("Kérem a " + (ArrayCount + 1) + ". számot: ");
  24.                     ell = int.TryParse(Console.ReadLine(), out Current);
  25.                 } while (!(ell && Current > -1));
  26.                 // } while (!ell || Current < 0);
  27.  
  28.                 if (Current != 0)
  29.                 {
  30.                     Array.Resize(ref DasArray, ArrayCount + 1);
  31.                     DasArray[ArrayCount] = Current;
  32.                     ArrayCount++;
  33.                 }
  34.  
  35.                 if (Current == 0 && ArrayCount == 0)
  36.                 { superell = false; }
  37.  
  38.             } while (Current != 0);
  39.  
  40.             if (superell)
  41.             {
  42.                 for (int i = 0; i < DasArray.Length; i++)
  43.                 {
  44.                     int Sum;
  45.                     ell = HashNumber(out Sum, DasArray[i]);
  46.                     if (ell)
  47.                     {
  48.                         Console.WriteLine("A " + (i + 1) + ". szám számjegyazonossággal rendelkezik, számjegyösszege " + Sum + ".");
  49.                     }
  50.                     else
  51.                     {
  52.                         Console.WriteLine("A " + (i + 1) + ". szám számjegyazonossággal nem rendelkezik, számjegyösszege " + Sum + ".");
  53.                     }
  54.  
  55.                 }
  56.             }
  57.         }
  58.  
  59.         static bool HashNumber (out int Sum, int Input)
  60.         {
  61.             int[] Numbers = new int[10];
  62.             Sum = 0;
  63.  
  64.             do
  65.             {
  66.                 int Temp = Input % 10;
  67.                 Sum = Sum + Temp;
  68.                 Numbers[Temp]++;
  69.                 Input = Input / 10;
  70.  
  71.             } while (Input > 0);
  72.  
  73.             for (int i = 0; i < Numbers.Length; i++)
  74.             {
  75.                 if (Numbers[i] > 1)
  76.                 {
  77.                     return true;
  78.                 }
  79.  
  80.  
  81.             }
  82.  
  83.             return false;
  84.         }
  85.  
  86.  
  87.  
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement