C# :: Aufgabe #262
8 Lösungen

Array von Zahlen in die nächstgelegene durch 5 teilbare Zahl umwandeln
Anfänger - C#
von Gustl
- 08.05.2019 um 20:08 Uhr
Schreibe ein Programm welches aus einem Array von Dezimalzahlen diese Zahlen in die nächstgelegene durch 5 teilbare Zahl umwandeln.
Etwa so:
Etwa so:
Konsolenausgabe:
7.1 => 5
8.4 => 10
-2.4 => 0
-2.6 => -5
-8.3 => -10
Lösungen:

using System; using static System.Console; using System.Collections.Generic; namespace Aufgabe_262 //Array von (reellen) Zahlen in die (jeweils) nächstgelegene (restlos) durch 5 teilbare Zahl umwandeln { class Program { static void Main() { bool bo_loop = true; Random rand = new Random(); double xmax = 1000.0; List<double> l_double = new List<double>(); List<int> l_int = new List<int>(); while (bo_loop) { double x = xmax * (rand.NextDouble() - 0.5); l_double.Add(x); int i = (int)(5.0 * Math.Round(x / 5.0)); l_int.Add(i); WriteLine($"{x.ToString("0.0000"), 10} -> {i, 5} for next double press any key (ESC for exit)"); ConsoleKeyInfo ki = ReadKey(true); bo_loop = ki.Key != ConsoleKey.Escape; } double[] a_double = l_double.ToArray(); int[] a_int = l_int.ToArray(); WriteLine("ready"); ReadKey(); } } }

using System; using System.Linq; public class Program { public static void Main() { double[] _in = {7.1, 8.4, -2.4, -2.6, -8.3}; double[] _out = _in.Select(x => round(x, 5)).ToArray(); for(int i = 0; i < _in.Length; i++){ Console.WriteLine("{0} => {1}", _in[i], _out[i]); } } private static int round(int val, int factor){ return (int)(Math.Round((double)val / (double)factor, 0) * factor); } private static float round(float val, float factor){ return (float)(Math.Round((double)val / (double)factor, 0) * factor); } private static double round(double val, double factor){ return Math.Round(val / factor, 0) * factor; } private static decimal round(decimal val, decimal factor){ return Math.Round(val / factor, 0) * factor; } }

using System; using System.Collections.Generic; using System.Text; namespace Teilung5 { class Program { static void Main(string[] args) { float[] arr = new float[5]; arr[0] = 7.1f; arr[1] = 8.4f; arr[2] = -2.4f; arr[3] = -2.6f; arr[4] = -8.3f; foreach(float i in arr) { int ganz = Convert.ToInt32(i/5); float dez = i / 5; float div = dez - ganz; int wert; if (div >= 0.5) { wert = (ganz + 1) * 5; Console.WriteLine(i + " => " + wert); } else { wert = ganz * 5; Console.WriteLine(i + " => " + wert); } } Console.ReadLine(); } } }

using System; using System.Collections.Generic; namespace Split5 { class Program { static void Main(string[] args) { List<int> numbers = new List<int> { 15, 1, 5, 88, -14, 7, -21, 42, 29, 36, 74 }; foreach (int number in numbers) { double erg; double m = number % 5; if (m < 2.5) erg = number - m; else erg = number + (5 - m); Console.WriteLine(number + " => " + erg); } Console.Read(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Train { class Program { static void Main(string[] args) { Console.WriteLine("Wie lang soll der Array werden?"); int zahl = int.Parse(Console.ReadLine()); for (int i = 0; i <= zahl-1; i++) { Console.WriteLine("Geben sie eine Zahl ein"); double[] arr = new double[zahl]; arr[i] = double.Parse(Console.ReadLine()); double[] arrumgewandelt = new double[zahl]; if (arr[i] % 5 == 0) { arrumgewandelt[i] = arr[i]; } if ((arr[i] % 5) != 0 && (arr[i] % 5) <= 2.5 && arr[i] > 0) { arrumgewandelt[i] = arr[i] - (arr[i] % 5); } if ((arr[i] % 5) != 0 && (Math.Abs(arr[i] % 5)) <= 2.5 && arr[i] < 0) { arrumgewandelt[i] = (arr[i] - (arr[i] % 5)); } if ((arr[i] % 5) != 0 && (arr[i] % 5) > 2.5 && arr[i] > 0) { arrumgewandelt[i] = (arr[i] - (arr[i] % 5) + 5); } if ((arr[i] % 5) != 0 && (Math.Abs(arr[i] % 5)) > 2.5 && arr[i] < 0) { arrumgewandelt[i] = (arr[i] - (arr[i] % 5) - 5); } Console.WriteLine("Die Zahl " + arr[i] + " lautet umgewandelt " + arrumgewandelt[i]); } Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Train { class Program { static void Main(string[] args) { Console.WriteLine("Wie lang soll der Array werden?"); int zahl = int.Parse(Console.ReadLine()); for (int i = 0; i <= zahl-1; i++) { Console.WriteLine("Geben sie eine Zahl ein"); double[] arr = new double[zahl]; arr[i] = double.Parse(Console.ReadLine()); double[] arrumgewandelt = new double[zahl]; if (arr[i] % 5 == 0) { arrumgewandelt[i] = arr[i]; } if ((arr[i] % 5) != 0 && (arr[i] % 5) <= 2.5 && arr[i] > 0) { arrumgewandelt[i] = arr[i] - (arr[i] % 5); } if ((arr[i] % 5) != 0 && (Math.Abs(arr[i] % 5)) <= 2.5 && arr[i] < 0) { arrumgewandelt[i] = (arr[i] - (arr[i] % 5)); } if ((arr[i] % 5) != 0 && (arr[i] % 5) > 2.5 && arr[i] > 0) { arrumgewandelt[i] = (arr[i] - (arr[i] % 5) + 5); } if ((arr[i] % 5) != 0 && (Math.Abs(arr[i] % 5)) > 2.5 && arr[i] < 0) { arrumgewandelt[i] = (arr[i] - (arr[i] % 5) - 5); } Console.WriteLine("Die Zahl " + arr[i] + " lautet umgewandelt " + arrumgewandelt[i]); } Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _5_Divisble_Array { class Program { static void Main(string[] args) { double[] arr = new double[5]; arr[0] = 7.1; arr[1] = 8.4; arr[2] = -2.4; arr[3] = -2.6; arr[4] = -8.3; double[] newArr = DivisibleTo5(arr); PrintArr(arr, newArr); Console.Read(); } static double[] DivisibleTo5(double[] arr) { double[] newArr = new double[arr.Length]; int subNum = 0; int addNum = 0; bool isBreak = false; //Algorithmus: Zahl zu erst zu einer geraden zahl ohne komma machen und immer //um 1 erhöhen und um 1 subtrahieren(dann um 2 erhöhen und subtrahieren,3...) //und schauen ob die Zahl durch 5 tielbar ist for (int i = 0; i < arr.Length; i++) { int num = MakeKommaWeg(arr[i]); //7.1 --> 7 int counter = 1; isBreak = false; while (!isBreak) { addNum = num + counter; //8 dann 9 usw... subNum = num - counter; //6 dann 5 usw... if (addNum % 5 == 0) { newArr[i] = addNum; isBreak = true; } else if (subNum % 5 == 0) { newArr[i] = subNum; isBreak = true; } counter++; } } return newArr; } static int MakeKommaWeg(double number) { string str = Convert.ToString(number); //7.1 string afterKommaStelle = ""; string vorKommaStelle = ""; bool isBreak = false; int i = 0; //Nachkomma- und Vorkommastellen herausfiltern while(i < str.Length || !isBreak) { if (str[i] == ',') { while (!isBreak) { afterKommaStelle += str[i + 1]; //7.1 --> 1 isBreak = true; i++; } } else { vorKommaStelle += str[i]; } i++; } if (int.Parse(afterKommaStelle) >= 0 && int.Parse(afterKommaStelle) <= 4) { //abgerundeten Wert returnen return int.Parse(vorKommaStelle); //7 } else //if (int.Parse(afterKommaStelle) >= 5 && int.Parse(afterKommaStelle) <= 9) { //aufgerundeten Wert returnen int strInt = int.Parse(vorKommaStelle); //bsp -2 if (strInt < 0)//-2 -->true { return strInt - 1; //-2 - 1 --> -3 aufrunden } return strInt + 1; } } static void PrintArr(double[] arr, double[] newArr) { for (int i = 0; i < arr.Length; i++) { Console.WriteLine($"{arr[i],4} =>{newArr[i],3}"); } } } }Zitat:
Es ist vielleicht bisschen kompliziert und uneffizient aber es funktioniert xd.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace aufg_262 { class Program { static void Main(string[] args) { int divisor; Console.WriteLine("Geben sie die Ganzzahl ein durch die die nächstgelegene Zahl teilbahr sein soll:"); int.TryParse(Console.ReadLine(), out divisor); float[] arr = new float[5]; arr[0] = 7.1f; arr[1] = 8.4f; arr[2] = -2.4f; arr[3] = -2.6f; arr[4] = -8.3f; foreach (float i in arr) { int gnz = Convert.ToInt32(i / divisor); float f = i / divisor; float diff = f - gnz; Console.WriteLine(i+" -> "+naehsteZahlDurchX(i, divisor)); } Console.ReadKey(); } public static int naehsteZahlDurchX(double i, int v) { int gnz = Convert.ToInt32(i / v); float f = (float)i / v; float diff = f - gnz; return (Convert.ToInt32(i / v)) * v; } } }