- #36
gnome
- 1,041
- 1
What's "normal"? And waddaya mean longer? You're not going to charge me for the "amenities", are you?
Bartholomew said:Point is, my algorithm is inherently simpler than yours.
Bartholomew said:Point is, my algorithm is inherently simpler than yours.
See what you started!Math Is Hard said:uh oh. them sounds like fightin' words..
I know. Mea culpa.gnome said:See what you started!
class RomanNumeral{
public static void main (String argv[])
{
int num, i, ii;
int cD[] = {1000, 500, 100, 50, 10, 5, 1};
int c[] = new int[7];
String cs = "MMMDDDCCCLLLXXXVVVIII";
num = 2949;
for(i=-1;i<6;c[++i]=num/cD[i],num%=cD[i]);
for(i=0;i<7;i++)
System.out.print(c[i]!=4||i==0?cs.substring(i*3,i*3+c[i]):""+
cs.charAt(i*3)+cs.charAt(i*3-1));
}
}
Oh no! Now everyone's posting condensed code samples in random languages – this thread is turning into the Obfuscated Roman Numeral Programming Competition...Bartholomew said:I'll just post it in Java
from sys import stdout
r = 'IVXLCDM'
print 'Decimal to Roman numeral conversions'
print 'Enter an integer 1-9999, press return or enter a non-integer to exit'
try:
while True:
n = int(raw_input('Your number? '))
while (n<1) or (n>9999): n = int(raw_input('Bad number!\nYour number? '))
if n > 999: stdout.write('M'*(n/1000))
n = str(n % 1000)
for (c1,c5,c10),d in [(r[i:i+3], int(n[-(i/2+1)])) for i in xrange((2*len(n)-2),-1,-2)]:
stdout.write(['', c1, c1*2, c1*3, c1+c5, c5, c5+c1, c5+c1*2, c5+c1*3, c1+c10][d])
stdout.write('\n')
except ValueError:
print 'Bye!'
From the page linked by Integral:Batholomew said:According to that page on roman numerals, IX is incorrect... you can't have a I and then an X, it skips the V, the same reason IMM is incorrect.
In other words, 90 = XC not LXL, and thus 9 = IX not VIV. Just look at the page of conversions at Integral's link.The subtracted number must be no less than a tenth of the value of the number it is subtracted from. So an X can be placed to the left of a C or an L but not to the left of an M or a D.
What page were you referring to?Bartholomew said:According to that page on roman numerals, IX is incorrect... you can't have a I and then an X, it skips the V, the same reason IMM is incorrect.
#include <iostream>
int main(){
int num, i;
int cD[] = {1000, 500, 100, 50, 10, 5, 1};
int c[7];
string cs = "IIIVVVXXXLLLCCCDDDMMM";
cin >> num;
for(i=-1;i<6;c[++i]=num/cD[i],num%=cD[i]);
for(i=6;i>-1;i--)
cout<<(c[6-i]!=4||i==6?cs.substr(i*3,c[6-i]):cs.substr(i*3+2,2));
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char **argv) {
int *input = new int();
int *rom = new int[7];
int *steps = new int[6];
char *map = new char[7];
*map = 'M'; *(map+1)='D'; *(map+2)='C'; *(map+3)='L'; *(map+4)='X';
*(map+5)='V'; *(map+6)='I';
printf("Please enter the number that must be converted: ");
scanf("%i", input);
*rom = *input / 1000;
*(rom + 1) = (*steps = *input % 1000) / 500;
*(rom + 2) = (*(steps+1) = *steps % 500) / 100;
*(rom + 3) = (*(steps+2) = *(steps+1) % 100) / 50;
*(rom + 4) = (*(steps+3) = *(steps+2) % 50) / 10;
*(rom + 5) = (*(steps+4) = *(steps+3) % 10) / 5;
*(rom + 6) = (*(steps+4) % 5);
printf("Here is your number in Roman numerals: ");
for (register int i = 0; i < 6; i++) {
for (register int k = 0; k < *(rom + i); k++) printf("%c", *(map + i));
};
switch(*(rom+6)) {
case 1: printf("%s", "I"); break;
case 2: printf("%s", "II"); break;
case 3: printf("%s", "III"); break;
case 4: printf("%s", "IV"); break;
default: break; };
return 0; }
That's no good -- it's not obfuscated at all!freemind said:Btw, I found this rather succint piece of code (I'm quite sure it works) http://home.att.net/~srschmitt/script_roman.html . I feel a JScript to C/C++ conversion is in order.
#include <iostream>
#include <string>
#include <math.h>
using namespace std; // added this line for MSVC 6.0
#define LTRS "MDCLXVI"
int iter[]={9,5,4};
void build(int& val, int p, string& s){
int r = (int)pow(10,p);
while(val>=r){
val -= r;
s += LTRS[6-2*p];
}
}
void trim(string t, int i, int j, string &s){
int ind = s.find(t,0);
string tt;
if (j!=1)
tt += LTRS[2*i+2];
j == 0 ? tt += LTRS[2*i] : tt += LTRS[2*i+1];
if (ind >=0)
s.replace(ind, iter[j], tt);
}
int main(){
int i, j, k, p, dec;
string t, roman;
cout << "Enter integer to be translated:\n";
cin >> dec;
for(p = 3; p >=0; p--)
build(dec, p, roman);
for(i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
t.erase();
for(k = 0; k < iter[j]; k++)
t += LTRS[2*i+2];
trim(t,i,j,roman);
}
}
cout << roman.c_str() << endl; // edited for MSVC 6.0
return 0;
}
#include <iostream>
int main(){
string s = "IIIVIXXXLXCCCDCMMM";
int n, c[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
cin>>n;
for(int i=12; i>-1;n%=c[i], i--)
cout<<(n/c[i]>0?s.substr(i+(i+3)/4,i%4?(i-2)%4?2:1:n/c[i]):"");
}
#include <iostream>
#include <string>
int main(){
string s = "IIIVIXXXLXCCCDCMMM";
int n, c[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
cin >> n;
for(int i=12; i>-1;n%=c[i], i--)
cout<< (n/c[i]>0 ? s.substr(i+(i+3)/4, i%4 ? (i-2)%4 ? 2 : 1 : n/c[i]) : "");
}