Einzelnen Beitrag anzeigen
Alt 23.05.09, 13:52   #38 (permalink)
Alive
 
Registriert seit: 02.10.08
Alive Leistung: Facit NTK
Likes: 0
Standard

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;}	
}

}
./a.out 1439
Code:
MCDXXXIX
Alive ist offline   Mit Zitat antworten
 

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 51 52 53 54 55 56 57 58 59 60 61