a013.
羅馬數字
--
NPSC 模擬試題
| From: [114.24.37.199] |
發表日期
:
2023-09-06 19:24
#include <iostream>
#include <cstdlib>
using namespace std;
int a,i,p,b,n=0;
int sum=1;
string roma,romanAnswer="";
char cina[5][15];
string all[5];
void Subtraction(void){
for (i;i<roma.size();i++)
{
if(roma[i]=='I' && roma[i+1]=='V')
{
sum-=4;
i++;
}else if(roma[i]=='I' && roma[i+1]=='X')
{
sum-=9;
i++;
}else if(roma[i]=='X' && roma[i+1]=='L')
{
sum-=40;
i++;
}else if(roma[i]=='X' && roma[i+1]=='C')
{
sum-=90;
i++;
}else if(roma[i]=='C' && roma[i+1]=='D')
{
sum-=400;
i++;
}else if(roma[i]=='C' && roma[i+1]=='M')
{
sum-=900;
i++;
}else if(roma[i]=='I')
{
sum-- ;
}else if(roma[i]=='V')
{
sum-=5;
}else if(roma[i]=='X')
{
sum-=10;
}else if(roma[i]=='L')
{
sum-=50;
}else if(roma[i]=='C')
{
sum-=100;
}else if(roma[i]=='D')
{
sum-=400;
}else if(roma[i]=='M')
{
sum-=1000;
}
}
}
void romanNumber(void){
for (i;i< roma.size();i++)
{
if(roma[i]=='I' && roma[i+1]=='V')
{
sum+=4;
i++;
}else if(roma[i]=='I' && roma[i+1]=='X')
{
sum+=9;
i++;
}else if(roma[i]=='X' && roma[i+1]=='L')
{
sum+=40;
i++;
}else if(roma[i]=='X' && roma[i+1]=='C')
{
sum+=90;
i++;
}else if(roma[i]=='C' && roma[i+1]=='D')
{
sum+=400;
i++;
}else if(roma[i]=='C' && roma[i+1]=='M')
{
sum+=900;
i++;
}else if(roma[i]=='I')
{
sum++ ;
}else if(roma[i]=='V')
{
sum+=5;
}else if(roma[i]=='X')
{
sum+=10;
}else if(roma[i]=='L')
{
sum+=50;
}else if(roma[i]=='C')
{
sum+=100;
}else if(roma[i]=='D')
{
sum+=400;
}else if(roma[i]=='M')
{
sum+=1000;
}else if (roma[i]=' ')
{
i++;
break;
}
}
}
void romatic(void){
int cut;
int val[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string ans[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
if(sum!=0)
{
if(sum<0){
sum*=-1;
}
while(sum!=0){
cut=sum/val[p];
for(int u=0;u<cut;u++){
romanAnswer+=ans[p];
}
sum-=val[p]*cut;
p++;
}
}else{
romanAnswer+="ZERO" ;
}
}
int main(void)
{
while(1){
sum=0;
getline(cin,roma);
//roma=cina[n];//
if(roma=="#")
{break;
}
romanNumber();
Subtraction();
romatic();
n++;
all[b]=romanAnswer;
b++;
romanAnswer="";
i=0;
}
for(int u=0;u<n;u++){
cout<<all[u]<<endl;
}
return 0;
}
我想知道 呼叫了 abort 函式! 的意思