Lotto Ziehung ()
Schreiben Sie zunächst eine Funktion, die als Rückgabewert eine einfache sortierte Liste mit 6 zufälligen Zahlen von 1 bis 49 liefert (6 aus 49). Dabei soll eine Zahl jeweils nur ein Mal in der Liste vorkommen!
Schreiben Sie anschließend ein Programm, welches die Funktion 3 Mal aufruft und den Rückgabewert jeweils wieder in einer Liste speichert. Am Ende geben Sie diese zweidimensionale Liste mit den drei Ziehungen 6 aus 49 auf dem Bildschirm aus.
0 Kommentare
9 Lösung(en)
static List<int> Lotto() => Enumerable.Range(1, 49).OrderBy(x => Guid.NewGuid()).Take(6).OrderBy(x => x).ToList();
var lst = Enumerable.Range(0, 3).Select(x => Lotto()).ToList();
lst.ForEach(x => Console.WriteLine(string.Join(", ", x)));
Lösung von: Jens Kelm (@JKooP)
function lotteryNumbers(min = 1, max = 49, num = 6) {
let out = [];
while (out.length < num) {
let cur = Math.floor(Math.random() * (max - min + 1) + min);
if (out.indexOf(cur) == -1) out.push(cur);
}
return out.sort(function(a, b) {return a - b});
}
let list = [], i;
for (i = 1; i <= 3; i++) list.push(lotteryNumbers());
console.log(list);
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// Achtung: F#
let lotto a b = [a .. b] |> List.sortBy (fun _ -> System.Random().Next()) |> List.take(6) |> List.sort
let ziehung n =
let mutable lst = []
for _ in 1 .. n do
lst <- [lotto 1 49] |> List.append lst
lst
printfn "%A" (ziehung 3)
Lösung von: Jens Kelm (@JKooP)
#include <iostream>
#include <vector>
#include <algorithm>
std::vector<int> lottery_numbers() {
std::vector<int> v;
auto c{ 0 };
while (c < 6) {
auto r{ rand() % 49 + 1 };
if (std::find(v.begin(), v.end(), r) == v.end()) {
v.push_back(r);
c++;
}
}
std::sort(v.begin(), v.end());
return v;
}
std::vector<std::vector<int>> list_lottery_numbers(int n) {
std::vector<std::vector<int>> v;
for (size_t i = 0; i < n; i++)
v.push_back(lottery_numbers());
return v;
}
void print_lottery_numbers(const std::vector<std::vector<int>>& v) {
for (const auto& l : v) {
for (const auto& n : l)
std::cout << n << " ";
std::cout << std::endl;
}
}
int main() {
srand(time(NULL));
print_lottery_numbers(list_lottery_numbers(3));
}
Lösung von: Jens Kelm (@JKooP)
import random
def lotto_numbers():
return sorted(random.sample(range(1, 50), k=6))
print([lotto_numbers() for _ in range(3)])
Lösung von: Name nicht veröffentlicht
Lottery Omelette.
Your Chef-recipe to 6 from 49 wealth and prosperity. No liability assumed.
<lissalanda@gmx.at>
Ingredients.
3 Fabergé eggs
Method.
Whisk Fabergé eggs. Serve with random sauce. Whisk Fabergé eggs until whisked.
Random Sauce.
Ingredients.
49 diamonds
6 pennies
Method.
Grind diamonds. Put diamonds into mixing bowl. Grind diamonds until
grinded. Mix the mixing bowl well. Flip pennies. Fold diamonds into mixing
bowl. Put diamonds into 2nd mixing bowl. Flip pennies until flipped. Pour
contents of the 2nd mixing bowl into the baking dish. Clean mixing bowl.
Refrigerate.
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
from random import randint
def sechszahlen(): return sorted([randint(1,49) for x in range(6)])
print([sechszahlen() for x in range(3)])
Lösung von: rob ert (tub)
' VBA
Sub SortArray(ByRef arr)
For i& = LBound(arr) To UBound(arr)
For k& = i + 1 To UBound(arr)
If arr(i) > arr(k) Then
tmp& = arr(i)
arr(i) = arr(k)
arr(k) = tmp
End If
Next k
Next i
End Sub
Function Contains(arr, comp%)
For i% = LBound(arr) To UBound(arr)
If arr(i) = comp Then
Contains = True
Exit Function
End If
Next
Contains = False
End Function
Function GetLotteryNumbers()
Dim arr(5), c%
Do While c < 6
r% = Rnd() * 1000 Mod 49 + 1
If Not Contains(arr, r) Then
arr(c) = r
c = c + 1
End If
Loop
GetLotteryNumbers = arr
End Function
Sub PrintArray(ByRef arr)
For i% = 0 To UBound(arr)
Debug.Print arr(i)
Next i
End Sub
Sub Main()
Randomize
nums = GetLotteryNumbers()
SortArray nums
PrintArray nums
End Sub
Lösung von: Jens Kelm (@JKooP)
// C++20 | VS-2022
#include <iostream>
#include <random>
#include <vector>
#include <numeric>
#include <format>
struct ticket {
size_t tips_num{ 1 }, max_num{ 49 }, take_num{ 6 };
bool sorted = true;
};
const auto get_lottery_numbers(const ticket& ticket_) {
std::random_device rd;
std::mt19937 mt(rd());
std::vector<size_t> vec(ticket_.max_num);
std::iota(vec.begin(), vec.end(), 1);
std::shuffle(vec.begin(), vec.end(), mt);
vec.resize(ticket_.take_num);
if (ticket_.sorted) std::sort(vec.begin(), vec.end());
return vec;
}
const auto get_lottery_tips(const ticket& ticket_) {
std::vector<std::vector<size_t>>vec;
for (size_t i{ 0 }; i < ticket_.tips_num; ++i)
vec.push_back(get_lottery_numbers(ticket_));
return vec;
}
const auto print(const auto& lottery_tips_) {
size_t i{ 0 };
for (const auto& tip : lottery_tips_) {
std::cout << std::format("{:02}: ", ++i);
for (const auto& num : tip)
std::cout << std::format(" {:2} |", num);
std::cout <<"\n" << std::string(34, '-') << "\n";
}
}
int main() {
print(get_lottery_tips(ticket{ 14 }));
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung: