Bartholomew
- 527
- 0
Actually, my method does not use any strings until the actual printing, unlike yours.
And I don't scan it before printing; I simply do a check each time I print a 5-ish character.
As for how short the code is, it can be this short:
It's possible there's a bug in this code since I haven't tested it, but I think you'll agree it's fairly elegant. I could have used more lines of code to make the program more efficient in the print loop, but I decided against it.
And I don't scan it before printing; I simply do a check each time I print a 5-ish character.
As for how short the code is, it can be this short:
Code:
# include <iostream>
using namespace std;
int main()
{
int num, i, ii;
int currentDivide[7] = {1000, 500, 100, 50, 10, 5, 1};
int count[7];
char characters[7] = {'M', 'D', 'C', 'L', 'X', 'V', 'I'};
cin >> num;
for(i = 0; i < 7; i++){
count[i] = num / currentDivide[i];
num %= currentDivide[i];
}
for(i = 0; i < 7; i++)
if(count[i] != 4 || i == 0)
for(ii = 0; ii < count[i]; ii++)
cout << characters[i];
else
cout << characters[i] << characters[i-1];
return 0;
}
It's possible there's a bug in this code since I haven't tested it, but I think you'll agree it's fairly elegant. I could have used more lines of code to make the program more efficient in the print loop, but I decided against it.
Last edited: