Interchange Roman and Integer
来源:互联网 发布:如何删除筛选后的数据 编辑:程序博客网 时间:2024/05/22 06:37
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int value[] = { 1000,900,500,400,100,90,50,40,10,9,5,4,1 };
string roman[] = { "M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" };
string solution_1(int num)
{
int i,count;
string Roman;
for (i = 0; num != 0;++i)
{
count = num / value[i];
num = num%value[i];
for (; count >0; count--)
{
Roman += roman[i];
}
}
return Roman;
}
int solution_2(string roman)
{
int i,num=0;
for (i = 0; roman[i] != '\0';)
{
switch (roman[i])
{
case 'M':
{num += 1000;
i += 1;
break;
}
case 'C':
{
if (roman[i + 1] == 'M')
{
num += 900;
i += 2;
break;
}
else
if (roman[i + 1] == 'D')
{
num += 400;
i += 2;
break;
}
else
{
num += 100;
i += 1;
break;
}
}
case 'D':
{
num += 500;
i += 1;
break;
}
case 'X':
{
if (roman[i + 1] == 'C')
{
num += 90;
i += 2;
break;
}
else
if (roman[i + 1] == 'L')
{
num += 40;
i += 2;
break;
}
else
{
num += 10;
i += 1;
break;
}
}
case 'I':
{
if (roman[i + 1] == 'X')
{
num += 9;
i += 2;
break;
}
else
if (roman[i + 1] == 'V')
{
num += 4;
i += 2;
break;
}
else
{
num += 1;
i += 1;
break;
}
}
case 'V':
{
num += 5;
i += 1;
break;
}
case 'L':
{
num += 50;
i += 1;
break;
}
}
}
return num;
}
void main()
{
int i;
string j;
cout << "Please input a Integer:";
cin >> i;
cout << i<<" to Roman is:"<< solution_1(i)<<endl;
cout << "Please input a Roman:";
cin >> j;
cout << j << " to Integer is:" << solution_2(j) << endl;
}
#include<cstring>
#include<string>
using namespace std;
int value[] = { 1000,900,500,400,100,90,50,40,10,9,5,4,1 };
string roman[] = { "M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" };
string solution_1(int num)
{
int i,count;
string Roman;
for (i = 0; num != 0;++i)
{
count = num / value[i];
num = num%value[i];
for (; count >0; count--)
{
Roman += roman[i];
}
}
return Roman;
}
int solution_2(string roman)
{
int i,num=0;
for (i = 0; roman[i] != '\0';)
{
switch (roman[i])
{
case 'M':
{num += 1000;
i += 1;
break;
}
case 'C':
{
if (roman[i + 1] == 'M')
{
num += 900;
i += 2;
break;
}
else
if (roman[i + 1] == 'D')
{
num += 400;
i += 2;
break;
}
else
{
num += 100;
i += 1;
break;
}
}
case 'D':
{
num += 500;
i += 1;
break;
}
case 'X':
{
if (roman[i + 1] == 'C')
{
num += 90;
i += 2;
break;
}
else
if (roman[i + 1] == 'L')
{
num += 40;
i += 2;
break;
}
else
{
num += 10;
i += 1;
break;
}
}
case 'I':
{
if (roman[i + 1] == 'X')
{
num += 9;
i += 2;
break;
}
else
if (roman[i + 1] == 'V')
{
num += 4;
i += 2;
break;
}
else
{
num += 1;
i += 1;
break;
}
}
case 'V':
{
num += 5;
i += 1;
break;
}
case 'L':
{
num += 50;
i += 1;
break;
}
}
}
return num;
}
void main()
{
int i;
string j;
cout << "Please input a Integer:";
cin >> i;
cout << i<<" to Roman is:"<< solution_1(i)<<endl;
cout << "Please input a Roman:";
cin >> j;
cout << j << " to Integer is:" << solution_2(j) << endl;
}
阅读全文
1 0
- Interchange Roman and Integer
- Integer to Roman and Roman to Integer
- "Roman to Integer" and "Integer to Roman"
- Roman to Integer and Integer to Roman
- No12&13 Integer and Roman
- [LeetCode] Roman to Integer and Integer to Roman
- [LeetCode]Integer to Roman AND ROman to Integer
- LeetCode]Integer to Roman AND ROman to Integer
- [LeetCode/LinkedIn] Integer to Roman and roman number to Integer
- integer-to-roman and roman-to-integer罗马数字转换
- java 解决Leetcode Integer and Roman 问题
- 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]
- Integer to Roman & Roman to Integer
- Integer to Roman & Roman to Integer
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- <9/10>集训周记
- JDBC事务及事务的隔离级别
- The study of recursive function in Python(20170910)
- 博弈知识汇总
- [hdu-4006]The kth great number 题解
- Interchange Roman and Integer
- HDU6205 card card card【模拟】
- JDBC
- 【51Nod1296】有限制的排列
- c++学习2
- 多线程检索文件
- Android利用硬解硬编和OpenGLES来高效的处理MP4视频
- vue系列之动态路由【原创】
- 03:计算书费