Programmieraufgabe Quersumme
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Programmieraufgabe Quersumme
Für eine gegebene Zahl N berechne die Quersumme.
Die Quersumme wird berechnet, indem alle Ziffern der Zahl aufaddiert werden.
Zum Beispiel hat 2025 die Quersumme 9, da 2+0+2+5 = 9 ist.
Implementiere die Methode loesung(N) im Code unten in der Programmiersprache Deiner Wahl (Python/Java/C)
und klicke anschliessend den "Run" Button unter dem Code um die Testfälle laufen zu lassen.
Bedingungen:
0 ≤ N ≤ 1000000000
PYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def loesung(N:int):
# ----------------------------------- #
# - TODO: FÜGE DEINEN CODE HIER EIN - #
# ----------------------------------- #
result = N
return result
# ------------------------------------------------------ #
# ---------- AB HIER DEN CODE NICHT VERÄNDERN ---------- #
# ------------------------------------------------------ #
INPUTS = [12, 56, 2025, 8, 0, 9999, 17112025]
CHECKS = [1656302624, 3832185857, 3205138698, 2846111786, 3158815156, 3540719418, 3071405752]
import sys
import hashlib
from datetime import datetime, timedelta
for N, check in zip(INPUTS, CHECKS):
result = loesung(N)
chk = hashlib.md5(f"{N}{result}".encode()).digest()
chk = (((chk[3]<<8)+chk[2]<<8)+chk[1]<<8)+chk[0]
#print(chk)
if chk == check:
print(f'RICHTIG: Die Quersumme von {N} ist {result}')
else:
print(f'FALSCH: Die Quersumme von {N} ist nicht {result}', file=sys.stderr)
sys.exit(1)
print("------------------------------------------------------------")
print(f"ERFOLG: Gratulation, Du hast die Aufgabe erfolgreich abgeschlossen um {datetime.now()+ timedelta(hours=1)}")
Press desired key combination and then press ENTER.
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Date;
public class Main {
public static int loesung(int N) {
/* ----------------------------------- */
/* - TODO: FÜGE DEINEN CODE HIER EIN - */
/* ----------------------------------- */
int result = N;
return result;
}
/* ------------------------------------------------------ */
/* ---------- AB HIER DEN CODE NICHT VERÄNDERN ---------- */
/* ------------------------------------------------------ */
public static void main(String[] args) {
int[] INPUTS = {12, 56, 2025, 8, 0, 9999, 17112025};
int[] CHECKS = {48690, 1632385, 47655768, 1792, 1536, 1686256803, -151278289};
for (int i=0; i<INPUTS.length; i++) {
int N = INPUTS[i];
int check = CHECKS[i];
int result = loesung(N);
int chk = (Integer.toString(N)+Integer.toString(result)).hashCode();
// System.out.println(chk);
if (chk == check) {
System.out.println("RICHTIG: Die Quersumme von "+N+" ist "+result);
}
else {
System.err.println("FALSCH: Die Quersumme von "+N+" ist nicht "+result);
System.exit(1);
}
}
System.out.println("------------------------------------------------------------");
System.out.println("ERFOLG: Gratulation, Du hast die Aufgabe erfolgreich abgeschlossen um "+new Date().toString());
}
}
Press desired key combination and then press ENTER.
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int loesung(unsigned N) {
/* ----------------------------------- */
/* - TODO: FÜGE DEINEN CODE HIER EIN - */
/* ----------------------------------- */
unsigned result = N;
return result;
}
/* ------------------------------------------------------ */
/* ---------- AB HIER DEN CODE NICHT VERÄNDERN ---------- */
/* ------------------------------------------------------ */
int main(void) {
struct test {
unsigned input;
unsigned check;
};
struct test tests[] = {
{12, 762299093}, {56, 1049489039}, {2025, 909320628},
{8, 462648444}, {0, 1804289383}, {9999, 687175592}, {17112025, 796206383}
};
for (int i = 0; i < sizeof(tests) / sizeof(struct test); i++) {
unsigned N = tests[i].input;
unsigned check = tests[i].check;
unsigned result = loesung(N);
srand(tests[i].input + result);
unsigned chk = rand();
if (chk == check) {
printf("RICHTIG: Die Quersumme von %u ist %u\n", N, result);
} else {
// fprintf(stderr, "FALSCH: Die Quersumme von %u ist nicht %u (%u)\n", N, result, chk);
fprintf(stderr, "FALSCH: Die Quersumme von %u ist nicht %u\n", N, result);
return 1;
}
}
time_t now = time(NULL);
now += 60 * 60;
struct tm *tm = localtime(&now);
char buf[1024];
strftime(buf, 1023, "ERFOLG: Gratulation, Du hast die Aufgabe erfolgreich abgeschlossen um %a %b %d %H:%M:%S", tm);
puts(buf);
return 0;
}
Press desired key combination and then press ENTER.
C++ (Work in Progress)
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Press desired key combination and then press ENTER.
Node.JS (Work in Progress)
1
2
console.log('Hello World!');
Press desired key combination and then press ENTER.
C# (Work in Progress)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// {
using System;
class Hello
{
static void Main()
{
// }
Console.WriteLine("Hello World!");
// {
}
}
// }
Press desired key combination and then press ENTER.
GO (Work in Progress)
1
2
3
4
5
6
7
8
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Press desired key combination and then press ENTER.
BASH (Work in Progress)
1
2
echo "Hello World!"
Press desired key combination and then press ENTER.
VB.NET (Work in Progress)
1
2
3
4
5
6
7
8
Imports System
Public Module modmain
Sub Main()
Console.WriteLine ("Hello World!")
End Sub
End Module
Press desired key combination and then press ENTER.
Kotlin (Work in Progress)
1
2
3
4
fun main(args: Array<String>) {
println("Hello, World!")
}
Press desired key combination and then press ENTER.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content