| Programmieraufgaben Hier wird regelmäßig eine neue Programmieraufgabe gestellt, die dann gelöst werden soll und in Zusammenarbeit mit den Moderatoren auch besprochen werden kann. |
Diskussion: Römische Zahlen im Forum Programmieraufgaben, in der Kategorie Code Kitchen; Bevor ich die Java-Lösungen hier gelesen habe, hab ich selber eine gschrieben, die etwas anders aussieht... vielleicht denke ich ja ...
![]() |
| | #31 (permalink) |
| Registriert seit: 28.08.07 ![]() Likes: 0 | Bevor ich die Java-Lösungen hier gelesen habe, hab ich selber eine gschrieben, die etwas anders aussieht... vielleicht denke ich ja anders als die meisten Menschen ;-) für xyz kann man eine beliebige Dezimal-Zahl eingeben Code: public class RömischeZahlen {
public static void main(String[] args) {
int Zahl = xyz;
String Ausgabe = "";
while ((Zahl / 5000) > 0) {
Ausgabe = Ausgabe + "A";
Zahl -= 5000;
}
while ((Zahl / 1000) > 0) {
Ausgabe = Ausgabe + "M";
Zahl -= 1000;
}
while ((Zahl / 500) == 1 && (Zahl / 100) != 9) {
Ausgabe = Ausgabe + "D";
Zahl -= 500;
}
if ((Zahl / 100) > 0) {
if ((Zahl / 100) == 9) {
Ausgabe = Ausgabe + "CM";
Zahl -= 900;
}
else if ((Zahl / 100) == 4) {
Ausgabe = Ausgabe + "CD";
Zahl -= 400;
}
else {
while ((Zahl /100) > 0) {
Ausgabe += "C";
Zahl -= 100;
}
}
}
while ((Zahl / 50) == 1 && (Zahl / 10) != 9) {
Ausgabe = Ausgabe + "L";
Zahl -= 50;
}
if ((Zahl / 10) > 0) {
if ((Zahl / 10) == 9) {
Ausgabe = Ausgabe + "XC";
Zahl -= 90;
}
else if ((Zahl / 10) == 4) {
Ausgabe = Ausgabe + "XL";
Zahl -= 40;
}
else {
while ((Zahl /10) > 0) {
Ausgabe += "X";
Zahl -= 10;
}
}
}
while ((Zahl / 5) == 1 && (Zahl / 1) != 9) {
Ausgabe = Ausgabe + "V";
Zahl -= 5;
}
if ((Zahl / 1) > 0) {
if ((Zahl / 1) == 9) {
Ausgabe = Ausgabe + "IX";
Zahl -= 9;
}
else if ((Zahl / 1) == 4) {
Ausgabe = Ausgabe + "IV";
Zahl -= 4;
}
else {
while ((Zahl /1) > 0) {
Ausgabe += "I";
Zahl -= 1;
}
}
}
if (Zahl == 0) {
System.out.println(Ausgabe);
}
}
}
__________________ Lenovo ThinkPad X201 | Core i7 620M 2,67 GHz, 4 GB DDR3, 128 GB Kingston SSD Marke Eigenbau | Core 2 Duo E8400 3,0 GHz, 4 GB DDR2, XFX NVIDIA GeForce 9800 GT 670M blog.derLichtschalter.de |
| | |
| | #32 (permalink) |
| Registriert seit: 21.04.08 ![]() Likes: 0 | Hallo! Meine Java Lösung Main.java Code: public class Main {
public static void main(String[] args) {
RoemNums roem = new RoemNums();
System.out.println(roem.getRoemNum(5));
System.out.println(roem.getRoemNum(19));
System.out.println(roem.getRoemNum(21));
System.out.println(roem.getRoemNum(347));
System.out.println(roem.getRoemNum(1234));
}
} Code: public class RoemNums {
private RoemDigit[] roemArray = null;
public RoemNums() {
this.roemArray = new RoemDigit[15];
this.init();
}
private void init() {
this.roemArray[0] = new RoemDigit(5000, "A");
this.roemArray[1] = new RoemDigit(4000, "MA");
this.roemArray[2] = new RoemDigit(1000, "M");
this.roemArray[3] = new RoemDigit(900, "CM");
this.roemArray[4] = new RoemDigit(500, "D");
this.roemArray[5] = new RoemDigit(400, "CD");
this.roemArray[6] = new RoemDigit(100, "C");
this.roemArray[7] = new RoemDigit(90, "XC");
this.roemArray[8] = new RoemDigit(50, "L");
this.roemArray[9] = new RoemDigit(40, "XL");
this.roemArray[10] = new RoemDigit(10, "X");
this.roemArray[11] = new RoemDigit(9, "IX");
this.roemArray[12] = new RoemDigit(5, "V");
this.roemArray[13] = new RoemDigit(4, "IV");
this.roemArray[14] = new RoemDigit(1, "I");
}
public String getRoemNum(int num) {
String roem = "";
for(int idx=0; idx<roemArray.length; idx++) {
while(num >= roemArray[idx].getNum()) {
num -= roemArray[idx].getNum();
roem += roemArray[idx].getDigit();
}
}
return roem;
}
} Code: public class RoemDigit {
private int num = 0;
private String digit = null;
public RoemDigit(int num, String digit) {
this.num = num;
this.digit = digit;
}
public int getNum() {
return num;
}
public String getDigit() {
return digit;
}
} Felix |
| | |
| | #33 (permalink) |
| Registriert seit: 04.06.08 ![]() Likes: 0 | Ich habe da auch eine Idee, von arabisch ins römische. Wir mussten sowas für die Schule erstellen. Die gegeben Programme haben mir da auch sehr geholfen. Danke. Ihr könnt euch ja mal melden, falls es da noch Probleme gibt. Code: public class rom {
public static void main(String [] args) {
System.out.println("Herzlich Willkommen");
System.out.println("Dieses Programm erfüllt die Umrechnung arabischer in römische Zahlen");
int eingabe = IOTools.readInteger("Geben sie eine Zahl von 1 bis 3999 an: "); //Eingabe der Zahl
if (eingabe<4000 && eingabe>0)
eingabe = eingabe;
else
eingabe = IOTools.readInteger("Geben sie erneut eine Zahl von 1 bis 3999 an: ");
abfrage(eingabe,false); //Einfache Umrechnung
abfrage(eingabe,true); //Subtraktionsregel
}
public static void abfrage (int eingabe2,boolean b){
String sub = ""; //setzt die römische Zahl auf 0
boolean a = true; //Einfache Umrechnung wird festgelegt
while(a){ //Schleife mit der Berechnung startet
if(eingabe2 >= 1000) {
eingabe2 = eingabe2 - 1000;
sub = sub + 'M';
}
else if(eingabe2 >=900 && b){
eingabe2 = eingabe2 - 900;
sub = sub+"CM";
}
else if(eingabe2 >= 500) {
eingabe2 = eingabe2 - 500;
sub = sub + 'D';
}
else if(eingabe2 >=400 && b){
eingabe2 = eingabe2 - 400;
sub =sub+"CD";
}
else if(eingabe2 >= 100) {
eingabe2=eingabe2 - 100;
sub = sub + 'C';
}
else if(eingabe2 >=90 && b){
eingabe2 = eingabe2 - 90;
sub = sub+"XC";
}
else if(eingabe2 >= 50) {
eingabe2=eingabe2 - 50;
sub = sub + 'L';
}
else if(eingabe2 >=40 && b){
eingabe2 = eingabe2 - 40;
sub = sub+"XL";
}
else if(eingabe2 >= 10) {
eingabe2=eingabe2 - 10;
sub = sub + 'X';
}
else if(eingabe2 >=9 && b){
eingabe2 = eingabe2 - 9;
sub = sub+"IX";
}
else if(eingabe2 >= 5) {
eingabe2=eingabe2 - 5;
sub = sub + 'V';
}
else if(eingabe2 >=4 && b){
eingabe2 = eingabe2 - 4;
sub = sub+"IV";
}
else if(eingabe2 >= 1) {
eingabe2=eingabe2 - 1;
sub = sub + 'I';
}
else if(eingabe2 == 0){
System.out.println("Die römische Zeichenkette lautet: "+sub); //gibt die Reihenfolge der römischen Buchstaben aus
a = false; //ändert es auf die Subtraktionsregel; die Schleife wird erneut durchgeführt und mit der korrekten Ausgabe der Buchstaben ausgegeben
}
}
}
} Editiert von CDW -> CODE-Tags nutzen! |
| | |
| | #34 (permalink) |
| Moderator ![]() Registriert seit: 20.07.05 ![]() ![]() ![]() ![]() ![]() Likes: 156 | Umrechnung in beide Systeme,Prolog,[b Ausgabe: Code: ?- rom(2008,Jahr). Jahr = [m, m, v, i, i, i]. ?- dec([m,m,m,cm,xc,ix],Jahr). Jahr = 3999
__________________ Noch mal, für alle Pseudo-Geeks: 1+1=0. -> 10 wäre Überlauf! Selig, wer nichts zu sagen hat und trotzdem schweigt. |
| | |
| | #35 (permalink) |
| Moderator ![]() Registriert seit: 20.07.05 ![]() ![]() ![]() ![]() ![]() Likes: 156 | und nochmal in der "Spielzeugsprache" Hackish (Indentation-Syntax) Im Anhang gibts erstmal nur CLASS Dateien (die "Sprache" wird zu Java-Bytecode compiliert) RTS und IRTS sind "runtime system" (zum einlesen/ausgeben - sollten nur im selben Ordner wie das Hauptprogramm liegen). Den Compiler kann ich erst nach dem 7.7.08 freigeben Laufen lassen kann man das Ganze per "java Rom"
__________________ Noch mal, für alle Pseudo-Geeks: 1+1=0. -> 10 wäre Überlauf! Selig, wer nichts zu sagen hat und trotzdem schweigt. |
| | |
| | #36 (permalink) |
| Hier noch meine Lösung in C++, ich hoffe sie funktioniert wie sie soll, also die Beispiele bei wikipedia hab ich getestet die habn funktioniert. Ist vielleicht nicht ganz so optimal programiert, aber ich bin für Verbesserungsvorschläge offen C++ Gruß Virus | |
| | |
| | #37 (permalink) |
| Senior Member Registriert seit: 03.09.05 ![]() Likes: 0 | Hier noch eine Lösung in Lisp: lisp |
| | |
| | #38 (permalink) |
| Registriert seit: 02.10.08 ![]() Likes: 0 | Meine recht kurze C++-Variante: Unterstützt auch IV, IX, XL, etc Code: #include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
if(argc < 2) {
cout << "Usage: a.out <arabische Zahl>" << endl;
return -1;
}
int zahl = atoi(argv[1]);
string rom[] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
int romval[] = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
for(int i=(sizeof(rom)/sizeof(rom[0])); i>=0; i--) {
while((zahl-=romval[i])>0)
cout << rom[i];
if(zahl < 0)
zahl += romval[i];
if(zahl == 0)
{cout << rom[i]; cout << endl; return 0;}
}
} Code: MCDXXXIX |
| | |
| | #39 (permalink) |
| Registriert seit: 28.03.09 ![]() Likes: 0 | hier mal ne python lösung: Code: def roemisch(n):
try:
x = n/5000
summe = x*"A"
n = n - x * 5000
x = n/4000
summe = summe+x*"MA"
n = n - x * 4000
x = n/1000
summe = summe+x*"M"
n = n - x * 1000
x = n/900
summe = summe+x*"CM"
n = n - x * 900
x = n/500
summe = summe+x*"D"
n = n - x * 500
x = n/400
summe = summe+x*"CD"
n = n - x * 400
x = n/100
summe = summe+x*"C"
n = n - x * 100
x = n/90
summe = summe+x*"XC"
n = n - x * 90
x = n/50
summe = summe+x*"L"
n = n - x * 50
x = n/40
summe = summe+x*"XL"
n = n - x * 40
x = n/10
summe = summe+x*"X"
n = n - x * 10
x = n/9
summe = summe+x*"IX"
n = n - x * 9
x = n/5
summe = summe+x*"V"
n = n - x * 5
x = n/4
summe = summe+x*"IV"
n = n - x * 4
x = n/1
summe = summe+x*"I"
return summe
except:
print "Es trat leider ein Fehler auf" |
| | |
| | #40 (permalink) |
| Registriert seit: 14.04.06 ![]() Likes: 4 | Nochmal als Haskell-Programm: Code: rome num pos s = do
let myVal = rv !! pos
if myVal <= num
then do
rome (num - myVal) pos (s ++ (rc !! pos))
else if num > 0
then rome num (pos + 1) s
else putStrLn s
where
rc = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
rv = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
main = do
putStrLn "Zahl: "
n <- getLine
rome (read n) 0 "Ergebnis: " |
| | |
| | #41 (permalink) | |
| Senior Member Registriert seit: 03.09.05 ![]() Likes: 0 | Zitat:
Hier noch eine Variante in Haskell, die ich noch auf der Platte rumliegen hatte(keine Prüfung auf ungültige Eingaben): Code: import Data.List
import Data.Ord
digits = [("A",5000),("MA",4000),("M",1000),("CM",900),("D",500),("CD",400),("C",100),
("XC",90),("L",50),("XL",40),("X",10),("IX",9),("V",5),("IV",4),("I",1)]
toRoman 0 = ""
toRoman d = let match = head $ filter ((<=d) . snd) digits
in fst match ++ (toRoman $ d - snd match)
toDec [] = 0
toDec str = let table = reverse $ sortBy (comparing (length . fst)) digits
match = head $ filter ((`isPrefixOf` str) . fst) table
in snd match + (toDec $ drop (length $ fst match) str) | |
| | |
| | #42 (permalink) |
| Registriert seit: 03.12.09 ![]() Likes: 0 | Hier ist meine Java-Lösung: code Ist übrigens mein erstes Java-Programm. |
| | |
| | #43 (permalink) |
| Registriert seit: 27.12.07 ![]() Likes: 24 | Auch wenns geschummelt ist: Code: (defun print-roman (num) (format t "~@R" num))
__________________ You shoot yourself in somebody else's foot.|Dann gabs da noch den Mathematiker der P?=NP in O(1) erklärte. |[A]| = p(·,|[A]|)+1 |
| | |
| | #44 (permalink) |
| Registriert seit: 26.07.10 ![]() Likes: 0 | Also ich habe das so gemacht(in c++), ist das ok? mit dieser funktion: Code: string RomToDec(int x)
{
int a=x;
string numb;
while(a>=1000)
{
numb+="M";
a-=1000;
};
while(a>=500)
{
if(a>=900)
{
numb+="CM";
a-=900;
}
numb+="D";
a-=500;
}
while(a>=100)
{
if(a>=400){numb+="CD";a-=400;};
if(a>100){numb+="C";a-=100;};
}
while(a>=50)
{
if(a==90){numb+="XC";a-=90;};
if(a>=50){numb+="L";a-=50;};
}
while(a>=10)
{
if(a==40){numb+="XL";a-=40;};
if(a>=10){numb+="X";a-=10;};
}
while(a>=5)
{
if(a==9){numb+="IX";a-=9;};
if(a>=5){numb+="V";a-=5;};
}
while(a>=1)
{
if(a==4){numb+="IV";a-=4;};
numb+="I";a-=1;
}
return numb;
} Geändert von CDW (27.07.10 um 12:35 Uhr) Grund: CodeTags :) |
| | |
| | #45 (permalink) | |
| Moderator ![]() Registriert seit: 11.02.06 ![]() ![]() ![]() ![]() ![]() ![]() Likes: 195 | Zitat:
![]() Aber falls Du möchtest das sich jemand den Code näher ansieht, solltest Du in Zukunft beim posten von Code die Code-Tags nicht vergessen - das macht das ganze viel übersichtlicher und somit einfacher. | |
| | |
![]() |
| | |
| |
| Themen-Optionen | |
| Ansicht | |
| |
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| Zahlen per SMS | Dawen | Internet Allgemein | 3 | 06.07.08 01:22 |
| Zahlen ordnen C++ | Tux1990 | Code Kitchen | 3 | 26.02.07 18:02 |
| C/C++ Römische in Arabische Zahlen konvertieren + Batchkonvertierung | Fifilip | Code Kitchen | 7 | 13.12.06 07:59 |
| Problem mit Zahlen in PHP! | PtB | (Web-) Design und webbasierte Sprachen | 2 | 01.02.05 14:12 |
| Zahlen in Excel | nook | Windows | 1 | 15.06.02 13:47 |