Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Ziffernkürzbare Zahlenpaare (Schleifen)

Normalerweise darf man aus Brüchen nicht einfach einzelne Ziffern kürzen. So ist 18/12 natürlich nicht gleich 8/2 (wenn ich die eins kürzen würde).

Bei einigen Zahlenpaaren funktioniert dies jedoch zufälligerweise. So ist (65/26) dasselbe wie 5/2 (einfach die Ziffer 6 im Zählen und Nenner steichen).

Schreiben Sie ein Programm, dass von allen zweistelligen Zahlenpaaren (Paare aus zwei zweistelligen Zahlen) angibt, ob die Zahl «ziffernkürzbar» ist.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

7 Lösung(en)

package kuerzen; // 2019-11-25 ?

public class Kuerzen {

        public static void main(String[] arg) {
                new Kuerzen() . top2();
                new Kuerzen() . top3();
        }

        int x, y, z;
        void top2() {
                for(x = 1; x <= 9; x++) {
                        for(y=1; y <=9; y++) {
                                for(z = 0; z <=9; z++) {
                                        test();
                                }
                        }
                }
        }

        void test() {
                if(10*x*y+y*z == 10*y*z+x*z && x != y) {
                        System.out.println(""+x+z + "/" + y+x);
                }
        }

        int s, t, u;
        void top3() {
                for(x = 1; x <= 9; x++) {
                for(y = 0; y <= 9; y++) {
                for(z = 0; z <= 9; z++) {
                for(s = 1; s <= 9; s++) {
                for(t = 0; t <= 9; t++) {
                for(u = 0; u <= 9; u++) {
                        test3();
                }}}}}}
        }

        void spucksAus3() {
                System.out.println(""+x+y+z + "/" +s+t+u);
        }
        void test3() {
                int zaehlerOrig=100*x+10*y+z;
                int nennerOrig =100*s+10*t+u;

                // Trivialfälle aussortieren
                if(z==0 && u==0 && t==0) return;
                if(u==0 && y==0 && z==0) return;
                if(x==s && y==t && z==u) return;
                //nur aufsteigend
                if(nennerOrig<zaehlerOrig) return;

                if(x==t) {
                        if( (10*s+u)*zaehlerOrig == (10*y+z)*nennerOrig) {
                                spucksAus3(); return;
                        }
                }
                if(x==u) {
                        if( (10*s+t)*zaehlerOrig == (10*y+z)*nennerOrig) {
                                spucksAus3(); return;
                        }
                }
                if(y==s) {
                        if( (10*t+u)*zaehlerOrig == (10*x+z)*nennerOrig) {
                                spucksAus3(); return;
                        }
                }
                if(y==u) {
                        if( (10*s+t)*zaehlerOrig == (10*x+z)*nennerOrig) {
                                spucksAus3(); return;
                        }
                }if(z==s) {
                        if( (10*t+u)*zaehlerOrig == (10*x+y)*nennerOrig) {
                                spucksAus3(); return;
                        }
                }
                if(z==t) {
                        if( (10*s+u)*zaehlerOrig == (10*x+y)*nennerOrig) {
                                spucksAus3(); return;
                        }
                }
        }
}
                

Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

// Kotlin

fun main() {
    for(a in 1..999) {
        for(b in 1..999) {
            if(a == b)
                continue

            isCancelable(a, b)
        }
    }
}

fun isCancelable(a: Int, b: Int) {
    val digitsA = getDigits(a)
    val digitsB = getDigits(b)
    val common = digitsA intersect digitsB

    cancel(a, b, digitsA, digitsB, common) {
        println("${it.first.first} / ${it.first.second} = ${it.second.first} / ${it.second.second}")
    }
}

fun cancel(originalA: Int, originalB: Int, a: List<Int>, b: List<Int>, common: Set<Int>, results: (Pair<Pair<Int, Int>, Pair<Int, Int>>) -> Unit) {
    for(cancel in common) {
        if(!a.contains(cancel) || !b.contains(cancel))
            continue

        if(a.size == 1 || b.size == 1)
            continue

        val newA = a.toMutableList().apply { remove(cancel) }
        val newB = b.toMutableList().apply { remove(cancel) }

        cancel(originalA, originalB, newA, newB, common, results)

        val aInt = newA.joinToString(separator = "").toInt()
        val bInt = newB.joinToString(separator = "").toInt()

        if(aInt.toDouble() / bInt.toDouble() == originalA.toDouble() / originalB.toDouble())
            results((originalA to originalB) to (aInt to bInt))
    }
}


fun getDigits(x: Int): List<Int> {
    val digits = mutableListOf<Int>()
    var i = x

    while (i > 0) {
        digits.add(i % 10)
        i /= 10
    }

    return digits.reversed()
}
                

Lösung von: Name nicht veröffentlicht

# Zweistellige Bruchzahlen erstellen.
for zähler in range(10,100): 
    for nenner in range(10,100):
        # Brüche ausschliessen die sofort kürzbar sind.
        if zähler != nenner and nenner % 10 != 0:
            # Bruchzahlen in einzelne Ziffern trennen.
            ziffern_zähler = list(divmod(zähler,10))
            ziffern_nenner = list(divmod(nenner,10)) 
            # Gleiche Ziffern Streichen.
            for z in ziffern_zähler:
                for n in ziffern_nenner:
                    if z == n :
                        ziffern_zähler.remove(z); z1 = ziffern_zähler[0]
                        ziffern_nenner.remove(n); n1 = ziffern_nenner[0]
                        # Brüche ausgeben bei denen der Bruchwert erhalten bleibt.
                        if z1/n1 == zähler/nenner:
                            print(z1,'/',n1,' enspricht ',zähler,'/',nenner)
                

Lösung von: Alex Groeg (Freies Lernen)

using System;
using System.Linq;

namespace Kürzen
{
    class Program
    {
        static void Main(string[] args)
        {
            //Durchlauf für alle Zähler und Nenner von 10 bis 99
            for(int z = 10; z < 100; z++)
            {
                for(int n = 10; n < 100; n++)
                {
                    if (kürzbar(z, n))
                        Console.WriteLine($"{z}/{n}");
                }
            }
            Console.ReadLine();
        }

        /// <summary>
        /// Prüft ob ein Bruch nach der Kürzung einer Ziffer immer noch das gleiche Ergebnis liefert
        /// </summary>
        /// <param name="zähler"></param>
        /// <param name="nenner"></param>
        /// <returns></returns>
        private static bool kürzbar(int zähler, int nenner)
        {
            //Umwandeln in ein Array von Ziffern
            int[] z = getZiffern(zähler);
            int[] n = getZiffern(nenner);

            foreach(int item in z)
            {
                //Schaue ob Dopplungen in Zähler und Nenner sind
                if (n.Contains(item))
                {
                    //Entfernt eine Ziffer und setzt die Zahl wieder zusammen
                    int z1 = combine(z.Where(x => x != item).ToArray());
                    int n1 = combine(n.Where(x => x != item).ToArray());

                    //Prüft ob die beiden Brüche übereinstimmen
                    if((double)z1 / n1 == (double)zähler / nenner)
                        return true;
                }
            }

            //Gab es bis hier keine Übereinstummung, so wird false zurückgegeben
            return false;
        }

        /// <summary>
        /// Gibt die einzelnen Ziffern einer Zahl als Array zurück
        /// </summary>
        /// <param name="zahl"></param>
        /// <returns></returns>
        private static int[] getZiffern(int zahl)
        {
            //Array aus der Anzahl der Stellen einer Zahl
            int[] arr = new int[zahl.ToString().Length];

            //Füllen des Arrays mit den einzelnen Ziffern der Zahl
            for (int i = 0; i < arr.Length; i++)
            {
                int wertigkeit = (int)Math.Pow(10, i + 1);

                arr[i] = (zahl % wertigkeit) / (wertigkeit / 10);
                zahl -= arr[i];
            }

            return arr.Reverse().ToArray();
        }

        /// <summary>
        /// Setzt ein Array aus einstelligen Zahlen zu einer Zahl zusammen
        /// </summary>
        /// <param name="ziffern"></param>
        /// <returns></returns>
        private static int combine(int[] ziffern)
        {
            int sum = 0;

            //Addiere zur Summe die Wertigkeit der einzelnen Stellen
            for(int i = 0; i < ziffern.Length; i++)
            {
                sum += ziffern[ziffern.Length - i - 1] * (int)Math.Pow(10, i);
            }

            return sum;
        }
    }
}

                

Lösung von: Tobias Golz (Wilhelm Büchner Hochschule)

function isDigitReducible(a, b) {
  // klare fälle, ließe sich noch ergänzen
  // die trivialen fälle bleiben selbstverständlich
  // erhalten, da sie die bedingung erfüllen
  if (a < 10 || b < 10) return false;
  if (a == b) return true;
  if (a % 10 == 0 && b % 10 == 0) return true;

  let den = a.toString().split(''),
      div = b.toString().split('');

  for (let i = 0; i < den.length; i++)
    for (let j = 0; j < div.length; j++) {
      if (den[i] == div[j]) {                           // ziffer finden
        let test1 = den.slice(), test2 = div.slice();   // arrays kopieren
        test1.splice(i, 1), test2.splice(j, 1);         // ziffer kürzen
        // check
        if (parseInt(test1.join('')) / parseInt(test2.join('')) == a / b)
          return true;
      }
    }
  return false;
}

// ausgabe
function showResults(arr) {
  document.write('<p>')
  for (let i = 0; i < arr.length; i++)
    document.write(`${arr[i].join('/')}<br>`);
  document.write(`(${arr.length} Elemente)</p>`);
}

// rein zweistellige brüche
let results = [];
for (let i = 10; i <= 99; i++)
  for (let j = 10; j <= 99; j++)
    if (i < j) if (isDigitReducible(i, j)) results.push([i, j]);
showResults(results);

// zwei- und dreistellig gemischte brüche
results = [];
for (let i = 10; i <= 99; i++)
  for (let j = 100; j <= 999; j++)
    if (isDigitReducible(i, j)) results.push([i, j]);
showResults(results);

// rein dreistellige brüche
results = [];
for (let i = 100; i <= 999; i++)
  for (let j = 100; j <= 999; j++)
    if (i < j) if (isDigitReducible(i, j)) results.push([i, j]);
showResults(results);                                        // lissalanda@gmx.at

                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

for i in range(10, 100):
    for j in range(10, 100):
        # Zerlege die zweistelligen Zahlen in jeweils zwei einstellige Zahlen
        x1 = int(str(i)[0:1])
        x2 = int(str(i)[1:])
        y1 = int(str(j)[0:1])
        y2 = int(str(j)[1:])
                
        if x1 == y1:
            # Schließe die Nulldivision aus UND prüfe auf das gewünschte Ergebnis UND schließe gleiche Zahlen und Vielfache von 10 aus
            # INFO: Entferne jeweils die letzten beiden Bedingungen (i != j and .. and ..), um gleiche Zahlen und Vielfache von 10 mit
            # aufzulisten, denn auch diese entsprechen dem gewünschten Ergebnis
            if x2 and y2 != 0 and x2 / y2 == i / j and i != j and i%10 != 0 and j%10 != 0:
                print(str(i)+" / "+str(j)+" entspricht "+str(x2)+" / "+str(y2))
                continue
        if x1 == y2:
            if x2 and y1 != 0 and x2 / y1 == i / j and i != j and i%10 != 0 and j%10 != 0:
                print(str(i)+" / "+str(j)+" entspricht "+str(x2)+" / "+str(y1))
                continue
        if x2 == y1:
            if x1 and y2 != 0 and x1 / y2 == i / j and i != j and i%10 != 0 and j%10 != 0:
                print(str(i)+" / "+str(j)+" entspricht "+str(x1)+" / "+str(y2))
                continue
        if x2 == y2:
            if x1 and y1 != 0 and x1 / y1 == i / j and i != j and i%10 != 0 and j%10 != 0:
                print(str(i)+" / "+str(j)+" entspricht "+str(x1)+" / "+str(y1))
                

Lösung von: Name nicht veröffentlicht

// NET 6.x | C# 10.x | VS-2022

static (bool b, string s) IsReducible(int a, int b) {
    if (a == b || a % 10 == 0 || b % 10 == 0) return (false, "keine Loesung");
    var aChr = a.ToString().ToCharArray().ToList();
    var bChr = b.ToString().ToCharArray().ToList();
    var length = aChr.Count;

    for (var i = 0; i < length; i++) {
        for (var k = 0; k < length; k++)
            if (aChr[i] == bChr[k]) {
                aChr.RemoveAt(i);
                bChr.RemoveAt(k);
                goto IsMatch;
            }
        if (i == length - 1) return (false, "keine Loesung");
    }
    IsMatch:
    var x = double.Parse(string.Join("", aChr));
    var y = double.Parse(string.Join("", bChr));
    var t = (a / (double)b) == (x / y);
    return (t, t ? $"{x}/{y}" : "keine Loesung");
}

static IEnumerable<ReducibleNumber> GetReducibleNumbers(int num_first_digits, int num_second_digits, int den_digits) {
    for (int i = (int)Math.Pow(10, num_first_digits-1); i <= (int)Math.Pow(10, den_digits)-1; i++)
        for (int k = (int)Math.Pow(10, num_second_digits - 1); k <= (int)Math.Pow(10, den_digits) - 1; k++) {
            var (b, s) = IsReducible(i, k);
            if (b) yield return new(i, k, s);
        }
}

static void Print(IEnumerable<ReducibleNumber> lst) => lst.ToList().ForEach(i => Console.WriteLine($"{i.Numerator}/{i.Denominator} ({i.Fraction})"));

Print(GetReducibleNumbers(2, 2, 2));        // zweistellige
Print(GetReducibleNumbers(2, 3, 3));        // zwei- und dreistellige gemischt
Print(GetReducibleNumbers(3, 3, 3));        // dreistellige
Print(GetReducibleNumbers(4, 4, 4));        // vierstellige (dauert aber!)
Console.WriteLine(IsReducible(15, 17));     // Einzelaufruf

record struct ReducibleNumber(int Numerator, int Denominator, string Fraction);
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

Zweistellig:

16/64
26/65
19/95
49/98

Dreistellig:

106/265
112/616
116/464
119/595
121/220
130/325
132/330
133/532
133/931
134/335
136/340
138/345
139/695
143/440
146/365
149/298
149/596
149/894
154/550
159/795
160/640
161/644
162/648
163/652
164/656
165/660
166/664
167/668
168/672
169/676
176/770
179/895
183/732
186/465
187/880
190/950
191/955
192/960
193/965
194/970
195/975
196/980
197/985
198/990
199/398
199/597
199/796
199/995
216/864
217/775
224/728
226/565
231/330
233/932
238/340
242/440
249/498
249/996
253/550
260/650
262/655
264/660
266/665
268/670
270/756
275/770
286/880
291/970
294/980
297/990
299/598
299/897
305/854
306/765
332/830
334/835
335/536
335/737
335/938
341/440
346/865
349/698
352/550
363/660
374/770
385/880
386/965
390/975
392/980
394/985
396/990
398/995
399/798
427/976
449/898
451/550
462/660
469/670
473/770
484/880
490/980
491/982
492/984
493/986
494/988
495/990
496/992
497/994
498/996
499/998
561/660
572/770
583/880
591/985
594/990
597/995
671/770
682/880
693/990
781/880
792/990
796/995
891/990

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 1
Schwierigkeit: Mittel
Webcode: 4tmw-rpud
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen