C# :: Aufgabe #177

4 Lösungen Lösungen öffentlich

Schneller als der Taschenrechner

Anfänger - C# von hollst - 16.05.2017 um 16:39 Uhr
Wenn ihr die folgende Aufgabe programmiert habt, könnt ihr auf jeder Party durch eure brilliante Kopfrechenleistung glänzend, ihr werdet schneller sein als jeder andere, selbst wenn er einen Tscherechner oder euren benutzen darf.

Zu programmieren ist ein kleiner, aber sehr spezieller Taschenrechner (Bild 1), der folgendes kann:

1) Entgegennahme einer dreistelligen positiven, geraden Ganzzahl (Bild 2).
2) Vergrößern der Dreistellenzahl auf sechs Stellen, indem die Zahl einfach noch einmal angehängt wird (Bild 3).
3) … 5) (komplizierte) Division der Sechstellenzahl nacheinander durch 22, 7 und 13. Reihenfolge spielt keine Rolle, aber nur jeweils 1x (deshalb deuten rote Buttons auf „nicht mehr benutzbar“ hin).

Im Beispiel (Bild 1 … 6) habt ihr allerdings das Ergebnis (117) wesentlich schneller per Kopf ausgerechnet als euer Freund mit dem Taschenrechner. Das liegt nicht daran, dass der Rechner so langsam rechnet, sondern daran, das das Buttondrücken so lange dauert.

Die ganze Übung vor größerem Publikum solltet ihr allerdings nach etwa höchstens 10 Vorführungen zunächst abbrechen, also noch bevor jemand hinter den Trick kommt. Was der Trick ist? Nun, bitte erst einmal selber nachdenken.

Lösungen:

1 Kommentar
vote_ok
von devnull (8870 Punkte) - 19.05.2017 um 12:17 Uhr
(1) Zahl sei a.
(2) Zahl vergrößern: 1000*a + a = 1001*a
(3) Division mit Teilern 7, 13, 22 -> Division durch (7*13*22) = 2002
(4) Ergebnis: 1001*a / 2002 = a / (2002/1001) = a/2
Quellcode ausblenden C-Code
/******************
 * zahlentrick.c
 ******************/
#include <stdlib.h>
#include <stdio.h>

/* main */
int main()
{
	int divisor[] = {7, 13, 22};
	int mdiv, ndiv, zahl, i;
	char yesno[4];

    mdiv = sizeof(divisor) / sizeof(int);
    ndiv = 0;
    do {
	    printf("Eingabe einer dreistelligen, geraden Zahl: ");
        scanf("%d", &zahl);
    } while (zahl<100 || zahl>999 || zahl%2);
    zahl = zahl*1000 + zahl;
	printf("Die 6stellige Zahl ist %d\n", zahl);

    while (ndiv < mdiv) {
		for (i=0; i<mdiv; i++) {
			if (divisor[i] > 0) {
				printf("Durch %d dividieren? ", divisor[i]);
				scanf("%s", yesno);
				if (*yesno == 'j' || *yesno == 'y') {
					zahl /= divisor[i];
					divisor[i] = 0;
					ndiv++;
					printf("Das Ergebnis der %d. Division ist %d\n", ndiv, zahl);
				}
			}
		}
	}
    return 0;
}
vote_ok
von daniel59 (4260 Punkte) - 30.05.2017 um 10:49 Uhr
MainWindow.xaml.cs
Quellcode ausblenden C#-Code
using System.Windows;

namespace WpfSchnellerAlsDerTaschenrechner
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private uint number;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonExpand_Click(object sender, RoutedEventArgs e)
        {
            if (uint.TryParse(textBoxNumber.Text, out number) && textBoxNumber.Text.Length == 3)
            {
                number *= 1001;
                textBoxNumber.Text = number.ToString();
                textBoxNumber.IsReadOnly = true;

                buttonExpand.IsEnabled = false;
                buttonDiv7.IsEnabled = true;
                buttonDiv13.IsEnabled = true;
                buttonDiv22.IsEnabled = true;
            }
            else
            {
                MessageBox.Show("Geben Sie eine 3-stellige Zahl ein", "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void buttonDiv22_Click(object sender, RoutedEventArgs e)
        {
            number /= 22;
            textBoxNumber.Text = number.ToString();
            buttonDiv22.IsEnabled = false;
        }

        private void buttonDiv7_Click(object sender, RoutedEventArgs e)
        {
            number /= 7;
            textBoxNumber.Text = number.ToString();
            buttonDiv7.IsEnabled = false;
        }

        private void buttonDiv13_Click(object sender, RoutedEventArgs e)
        {
            number /= 13;
            textBoxNumber.Text = number.ToString();
            buttonDiv13.IsEnabled = false;
        }

        private void buttonReset_Click(object sender, RoutedEventArgs e)
        {
            textBoxNumber.Text = "";
            textBoxNumber.IsReadOnly = false;
            buttonExpand.IsEnabled = true;
            buttonDiv7.IsEnabled = false;
            buttonDiv13.IsEnabled = false;
            buttonDiv22.IsEnabled = false;
        }
    }
}


MainWindow.xaml
Quellcode ausblenden XML-Code
<Window x:Class="WpfSchnellerAlsDerTaschenrechner.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfSchnellerAlsDerTaschenrechner"
        mc:Ignorable="d"
        Title="MainWindow" Height="218" Width="237" ResizeMode="CanMinimize">
    <Grid>
        <TextBox x:Name="textBoxNumber" HorizontalAlignment="Left" Height="23" Margin="92,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,13,0,0" TextWrapping="Wrap" Text="3-stellige Zahl:" VerticalAlignment="Top"/>
        <Button x:Name="buttonExpand" Content="Erweitern" HorizontalAlignment="Left" Margin="10,38,0,0" VerticalAlignment="Top" Width="202" Click="buttonExpand_Click"/>
        <Button x:Name="buttonDiv22" Content="/ 22" HorizontalAlignment="Left" Margin="10,65,0,0" VerticalAlignment="Top" Width="202" IsEnabled="False" Click="buttonDiv22_Click"/>
        <Button x:Name="buttonDiv7" Content="/ 7" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="202" IsEnabled="False" Click="buttonDiv7_Click"/>
        <Button x:Name="buttonDiv13" Content="/ 13" HorizontalAlignment="Left" Margin="10,119,0,0" VerticalAlignment="Top" Width="202" IsEnabled="False" Click="buttonDiv13_Click"/>
        <Button x:Name="buttonReset" Content="Reset" HorizontalAlignment="Left" Margin="10,146,0,0" VerticalAlignment="Top" Width="202" Click="buttonReset_Click"/>
    </Grid>
</Window>
1 Kommentar
vote_ok
von Pr0gr4mm3r (60 Punkte) - 01.06.2017 um 12:08 Uhr
Bestimmt nicht die Eleganteste Lösung aber ich bin auch recht neu in der Programmier branche und es läuft immerhin ;) hier kommt meine Lösung:
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace übung_trickrechner_wpf
{
    
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
        InitializeComponent();

        }
        public int eingegebenezahl = 0;
        public int neuezahl;
        public int zwischenergebnisse=0;
        public string zahl1;
        private void TB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if(TB.Text!="Bitte 3 stellige Zahl eingeben.")
            eingegebenezahl = Convert.ToInt32(TB.Text);
            else { return; }
        }

        private void ExpandB_Click(object sender, RoutedEventArgs e)
        {
            
            zahl1= Convert.ToString(eingegebenezahl) + Convert.ToString(eingegebenezahl);

            neuezahl = Convert.ToInt32(zahl1) ;
            TB.Text = Convert.ToString(neuezahl);
        }

        private void Teiler22_Click(object sender, RoutedEventArgs e)
        {
            if(zwischenergebnisse==0)
            {
                zwischenergebnisse = neuezahl / 22;
                Teiler22.Background = Brushes.Red;
                TB.Text = Convert.ToString(zwischenergebnisse);
            }
            else
            {
                zwischenergebnisse = zwischenergebnisse / 22;
                Teiler22.Background = Brushes.Red;
                TB.Text = Convert.ToString(zwischenergebnisse);
            }
        }

        private void Teiler7_Click(object sender, RoutedEventArgs e)
        {
            if (zwischenergebnisse == 0)
            {
                zwischenergebnisse = neuezahl / 7;
                Teiler7.Background = Brushes.Red;
                TB.Text = Convert.ToString(zwischenergebnisse);
            }
            else
            {
                zwischenergebnisse = zwischenergebnisse / 7;
                Teiler7.Background = Brushes.Red;
                TB.Text = Convert.ToString(zwischenergebnisse);
            }
        }

        private void Teiler13_Click(object sender, RoutedEventArgs e)
        {
            if (zwischenergebnisse == 0)
            {
                zwischenergebnisse = neuezahl / 13;
                Teiler13.Background = Brushes.Red;
                TB.Text = Convert.ToString(zwischenergebnisse);
            }
            else
            {
                zwischenergebnisse = zwischenergebnisse / 13;
                Teiler13.Background = Brushes.Red;
                TB.Text = Convert.ToString(zwischenergebnisse);
            }
        }

        private void ResetB_Click(object sender, RoutedEventArgs e)
        {
            Teiler22.Background = Brushes.Green;
            Teiler7.Background = Brushes.Green;
            Teiler13.Background = Brushes.Green;
            zwischenergebnisse = 0;
            TB.Text = "Bitte 3 stellige Zahl eingeben.";
        }
    }
}


und Das fenster:
Quellcode ausblenden XML-Code
<Window x:Class="übung_trickrechner_wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:übung_trickrechner_wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="ResetB" Content="Reset" HorizontalAlignment="Left" Margin="219,54,0,0" VerticalAlignment="Top" Width="75" Click="ResetB_Click"/>
        <TextBox x:Name="TB" HorizontalAlignment="Left" Height="23" Margin="132,110,0,0" TextWrapping="Wrap" Text="Bitte 3 stellige Zahl eingeben." VerticalAlignment="Top" Width="249" TextChanged="TB_TextChanged"/>
        <Button x:Name="ExpandB" Content="Expand" HorizontalAlignment="Left" Margin="164,138,0,0" VerticalAlignment="Top" Width="189" Background="Green" Click="ExpandB_Click" />
        <Button x:Name="Teiler22" Content="/22" HorizontalAlignment="Left" Margin="219,162,0,0" VerticalAlignment="Top" Width="75" Background="Green" Click="Teiler22_Click"/>
        <Button x:Name="Teiler7" Content="/7" HorizontalAlignment="Left" Margin="219,186,0,0" VerticalAlignment="Top" Width="75" Background="Green" Click="Teiler7_Click"/>
        <Button x:Name="Teiler13" Content="/13" HorizontalAlignment="Left" Margin="219,210,0,0" VerticalAlignment="Top" Width="75" Background="Green" Click="Teiler13_Click"/>

    </Grid>
</Window>

vote_ok
von hollst (13980 Punkte) - 03.06.2017 um 17:32 Uhr
Quellcode ausblenden C#-Code
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace jocke_calculator  {

    public partial class MainWindow : Window    {

        public MainWindow()        {

            InitializeComponent();
            bt_reset_Click(null, null);
        }

        private uint number;
        private bool[] bo_button_free = new bool[] {true, false, false, false };
        

        private void bt_reset_Click(object sender, RoutedEventArgs e)        {

            this.bo_button_free = new bool[] { true, false, false, false };
            this.set_color();
            this.tb_inout.Clear();
            this.tb_inout.Focus();
        }

        private SolidColorBrush brush_true = Brushes.Green, brush_false = Brushes.Red;
       
        private void set_color()        {

            if (this.bo_button_free[0]) this.bt_expand.Background = brush_true; 
            else this.bt_expand.Background = brush_false;

            if (this.bo_button_free[1]) this.bt_22.Background = brush_true;
            else this.bt_22.Background = brush_false;

            if (this.bo_button_free[2]) this.bt_7.Background = brush_true;
            else this.bt_7.Background = brush_false;

            if (this.bo_button_free[3]) this.bt_13.Background = brush_true;
            else this.bt_13.Background = brush_false;
        }

        private void tb_inout_KeyDown(object sender, KeyEventArgs e)        {
            if (e.Key == Key.Return)
                this.bt_expand_Click(null, null);
        }

        private void error()
        { MessageBox.Show("input not okay! just a positive three-digit even number (100 ... 998)"); }

        private void bt_expand_Click(object sender, RoutedEventArgs e)        {
            if (!bo_button_free[0])
                return;

            uint content;
            bool bo_content_ok = uint.TryParse(this.tb_inout.Text, out content);
            if (!bo_content_ok)
                error();
            else
                if (this.tb_inout.Text.Length != 3)
                    error();
                else    {
                    if (content % 2 == 1)
                        content--;
                    this.number = content * 1001;
                    this.tb_inout.Text = this.number.ToString();
                    this.bo_button_free = new bool[] { false, true, true, true };
                    this.set_color();
                }
        }

        private void bt_22_Click(object sender, RoutedEventArgs e)        {
            if (!bo_button_free[1])
                return;
            this.number /= 22;
            this.tb_inout.Text = this.number.ToString();
            bo_button_free[1] = false;
            this.set_color();
        }

        private void bt_7_Click(object sender, RoutedEventArgs e)        {
            if (!bo_button_free[2])
                return;
            this.number /= 7;
            this.tb_inout.Text = this.number.ToString();
            bo_button_free[2] = false;
            this.set_color();
        }
 
        private void bt_13_Click(object sender, RoutedEventArgs e)        {
            if (!bo_button_free[3])
                return;
            this.number /= 13;
            this.tb_inout.Text = this.number.ToString();
            bo_button_free[3] = false;
            this.set_color();
        }
    }
}

<Window x:Name="main_window" x:Class="jocke_calculator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:jocke_calculator"
        mc:Ignorable="d"
        Title="special calculator" Height="369.231" Width="525" FontFamily="Courier New" Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
    <Grid>
        <StackPanel Margin="50,5">
            <Button x:Name="bt_reset" Content="reset" Height="50" Margin="0,0,0,5" FontSize="36" FontWeight="Bold" Click="bt_reset_Click"/>
            <TextBox x:Name="tb_inout" Height="50" TextWrapping="Wrap" Text="TextBox" Margin="100,0,100,5" FontSize="36" FontWeight="Bold" KeyDown="tb_inout_KeyDown" />
            <Button x:Name="bt_expand" Content="expand" Height="50" Margin="0,0,0,5" FontSize="36" FontWeight="Bold" Click="bt_expand_Click"/>
            <Button x:Name="bt_22" Content="/22" Height="50" Margin="0,0,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" FontWeight="Bold" Click="bt_22_Click"/>
            <Button x:Name="bt_7"  Content="/7 " Height="50" Margin="0,0,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" FontWeight="Bold" Click="bt_7_Click"/>
            <Button x:Name="bt_13" Content="/13" Height="50" Margin="0,0,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" FontWeight="Bold" Click="bt_13_Click"/>

        </StackPanel>

    </Grid>
</Window>
1800916

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.