#28324: C++白癡寫法


ahsomeone607@gmail.com (陳文鑫)

School : 國立交通大學
ID : 174401
IP address : [140.113.0.229]
Last Login :
2022-01-16 01:08:13
a013. 羅馬數字 -- NPSC 模擬試題 | From: [140.113.90.172] | Post Date : 2021-11-29 00:26

#include<iostream>

#include<memory.h>

using namespace std;

 int differ;

 int transformint [2][15];

 int a=0,b=0;

 int &ref1=a;

 int &ref2=b;

 int &ref3=differ;

 void transforminput(char rome [2][15])

{

for(int i=0;i<2;i++)

{

for(int j=0;j<15;j++)

{

switch(rome [i][j])

{

case'I':

transformint [i][j]=1;

break;

case'V':

transformint [i][j]=5;

break;

case'X':

transformint [i][j]=10;

break;

case'L':

transformint [i][j]=50;

break;

case'C':

transformint [i][j]=100;

break;

case'D':

transformint [i][j]=500;

break;

case'M':

transformint [i][j]=1000;

break;

default:

transformint [i][j]=0;

}

}

}

for(int i=0;i<2;i++)

{

for(int j=0;j<15;j++)

{

if(transformint [i][j]<transformint [i][j+1])

{

transformint [i][j]*=(-1);

}

else

{

transformint [i][j]*=(1);

}

 

}

}

 

for(int j=0;j<15;j++)

{

ref1+=transformint [0][j];

}

for(int j=0;j<15;j++)

{

ref2+=transformint [1][j];

}

 

}

 

 

void deterdiffer(int &ref1,int &ref2)

{

if(a-b>=0)

{

ref3=ref1-ref2;

}

else

{

ref3=ref2-ref1;

}

}

 

 

void transformresult(int &ref3)

{

int digit=0,tens=0,hundreds=0,thousands=0;

if(ref3==0)

{

cout<<"ZERO\n";

}

else if(ref3!=0)

{

if(ref3<=4000&&ref3>=1000)

{

thousands=ref3/1000%10;

if(thousands==1)

{

if(ref3==1000)

cout<<"M"<<endl;

else

cout<<"M";

}

else if(thousands==2)

{

if(ref3==2000)

cout<<"MM"<<endl;

else

cout<<"MM";

}

else if(thousands==3)

{

if(ref3==3000)

cout<<"MMM"<<endl;

else

cout<<"MMM";

}

else if(thousands==4)

{

if(ref3==4000)

cout<<"MMMM"<<endl;

 

}

}

if(ref3<=4000&&ref3>=100)

{

hundreds=ref3/100%10;

if(hundreds==1)

{

if(ref3==100)

cout<<"C"<<endl;

else

cout<<"C";

}

else if(hundreds==2)

{

if(ref3==200)

cout<<"CC"<<endl;

else

cout<<"CC";

}

else if(hundreds==3)

{

if(ref3==300)

cout<<"CCC"<<endl;

else

cout<<"CCC";

}

else if(hundreds==4)

{

if(ref3==400)

cout<<"CD"<<endl;

else

cout<<"CD";

}

else if(hundreds==5)

{

if(ref3==500)

cout<<"D"<<endl;

else

cout<<"D";

}

else if(hundreds==6)

{

if(ref3==600)

cout<<"DC"<<endl;

else

cout<<"DC";

else if(hundreds==7)

{

if(ref3==700)

cout<<"DCC"<<endl;

else

cout<<"DCC";

else if(hundreds==8)

{

if(ref3==800)

cout<<"DCCC"<<endl;

else

cout<<"DCCC";

else if(hundreds==9)

{

if(ref3==900)

cout<<"CM"<<endl;

else

cout<<"CM";

}

if(ref3<=4000&&ref3>=10)

{

tens=ref3/10%10;

if(tens==1)

{

if(ref3==10)

cout<<"X"<<endl;

else

cout<<"X";

}

else if(tens==2)

{

if(ref3==20)

cout<<"XX"<<endl;

else

cout<<"XX";

}

else if(tens==3)

{

if(ref3==30)

cout<<"XXX"<<endl;

else

cout<<"XXX";

}

else if(tens==4)

{

if(ref3==40)

cout<<"XL"<<endl;

else

cout<<"XL";

}

else if(tens==5)

{

if(ref3==50)

cout<<"L"<<endl;

else

cout<<"L";

}

else if(tens==6)

{

if(ref3==60)

cout<<"LX"<<endl;

else

cout<<"LX";

else if(tens==7)

{ if(ref3==70)

cout<<"LXX"<<endl;

else

cout<<"LXX";

else if(tens==8)

{

if(ref3==80)

cout<<"LXXX"<<endl;

else

cout<<"LXXX";

else if(tens==9)

{

if(ref3==90)

cout<<"XC"<<endl;

else

cout<<"XC";

}

digit=ref3%10;

if(digit==1)

{

cout<<"I"<<endl;

}

else if(digit==2)

{

cout<<"II"<<endl;

}

else if(digit==3)

{

cout<<"III"<<endl;

}

else if(digit==4)

{

cout<<"IV"<<endl;

}

else if(digit==5)

{

cout<<"V"<<endl;

}

else if(digit==6)

{

cout<<"VI"<<endl;

else if(digit==7)

{

cout<<"VII"<<endl;

else if(digit==8)

{

cout<<"VIII"<<endl;

else if(digit==9)

{

cout<<"IX"<<endl;

}

 

}

int main(void)

{

while(true)

{

char rome [2][15];

memset(rome,0, sizeof rome);

memset(transformint,0, sizeof transformint);

a=0;

b=0;

differ=0;

cin>>rome[0];

if(rome [0][0]=='#')

{

exit(0);

}

cin>>rome[1];

transforminput(rome);

deterdiffer(ref1,ref2);

transformresult(ref3);

}

}

 
ZeroJudge Forum