#17739: 給延平的同仁 c++ AC


089487 (089487)

學校 : 國立臺灣師範大學附屬高級中學
編號 : 82069
來源 : [220.130.10.185]
最後登入時間 :
2024-04-01 11:16:18
a013. 羅馬數字 -- NPSC 模擬試題 | From: [140.118.146.27] | 發表日期 : 2019-05-13 20:04

 #include<bits/stdc++.h>
using namespace std;
int length(int);
int main()
{
int n,m,z,r,e=0,k,e2,line=0;
char c[3][2]={{'I','V'},
{'X','L'},
{'C','D'}};
int num[3][2]={{1,5},{10,50},{100,500}};
string a,b;
bool f=false,f2;
while(cin>>a)
{
e=-2;
e2=-1;
if(a=="#") break;
cin>>b;
//if(line==20) {cout<<a<<" "<<b<<endl;continue;}
if(a==b) {cout<<"ZERO\n";continue;}
string h;
int x=0;
n=0,m=0;
//f2=false;
for(int i=0;i<a.length();i++)
{
f=false;
for(int j=0;j<3;j++)
{
int g;
for( g=0;g<2;g++) if(a[i]==c[j][g]){k=num[j][g];f=true;break;}
if(f)
{
if(e==-2)
{
if(i==a.length()-1) n+=k;
e=k;
}
else if(e==k) n+=e+k,e=-2;
else if(k>e)
{
n+=k-e;
e=-2;
}
else if(e>k)
{
n+=(e!=1000)*e,e=k;
if(i==a.length()-1) n+=k;
}
break;
}

}
if(f==false)
{
if(e==-2) n+=1000;
else if(e==1000) n+=1000;
else if(e==-1) n+=1000;
else if(e<1000) n+=1000-e;
else n+=1000+e;
e=1000;
}

}
//if(a.length()==1||f2) n+=e;
//cout<<n<<" ";
e=-2;
f2=false;
for(int i=0;i<b.length();i++)
{
f=false;
for(int j=0;j<3;j++)
{
int g;
for( g=0;g<2;g++) if(b[i]==c[j][g]){k=num[j][g];f=true;break;}
if(f)
{
if(e==-2)
{
if(i==b.length()-1) m+=k;
e=k;
}
else if(e==k) m+=e+k,e=-2;
else if(k>e)
{
m+=k-e;
e=-2;
}
else if(e>k)
{
m+=(e!=1000)*e,e=k;
if(i==b.length()-1) m+=k;
}
break;
}

}
if(f==false)
{
if(e==-2) m+=1000;
else if(e==1000) m+=1000;
else if(e==-1) m+=1000;
else if(e<1000) m+=1000-e;
else m+=1000+e;
e=1000;
}
// cout<<m<<" ";
}
//if(b.length()==1||f2) m+=e;
//cout<<m<<" ";
x=abs(n-m);
//x=444;
//cout<<x<<endl;
if(x==0) {cout<<"ZERO\n";continue;}
string s;
int p,l;
l=length(x);
while(l>1)
{
p=pow(10,l-1);
if(x/p==0) s+='0';
else s+=x/p+'0';
x%=p;
l--;
//cout<<s<<" ";
}
s+=x+'0';
//cout<<s<<endl;
r=0;
for(int i=s.length()-1;i>=0;i--)
{
if(s[i]=='0') {r++;continue;}
if(s.length()-i-1==3) for(int j=0;j<s[i]-'0';j++) h+='M';
else
{
if(s[i]>='5'&&s[i]<'9'){
//cout<<c[s.length()-i-1][0];
for(int mm=0;mm<(s[i]-'5');mm++) h+=c[s.length()-i-1][0];
//cout<<h.length()<<endl;
h+=c[s.length()-i-1][1];
r=0;
}
else if(s[i]=='9')
{
if(s.length()-i==3) h+='M';
else h+=c[s.length()-i][0];
h+=c[s.length()-1-i][0];
r=0;
}
else if(s[i]=='4')
{
h+=c[s.length()-i-1][1];
h+=c[s.length()-1-i][0];
r=0;
}
else
{
for(int mm=0;mm<s[i]-'0';mm++) h+=c[s.length()-i-1][0];
r=0;
}
}
}
string h2;
for(int i=h.length()-1;i>=0;i--) h2+=h[i];
/* if(h2=="MMDCCLXIII")
{
cout<<a<<"|"<<b<<endl;
continue;
}*/
cout<<h2<<endl;
}
}
int length(int n)
{
int num=1;
while(n>=10) n/=10,num++;
return num;
}

 
#17745: Re:給延平的同仁 c++ AC


freedom501999@gmail.com (帥氣魔方生)

學校 : 不指定學校
編號 : 88611
來源 : [39.8.203.54]
最後登入時間 :
2019-05-30 22:56:25
a013. 羅馬數字 -- NPSC 模擬試題 | From: [39.8.34.96] | 發表日期 : 2019-05-14 16:48

 


老實說,我蠻希望你排版,雖然我不是延平的

但至少給同校同學,甚至使用這個網站的網友,能看的舒服

寫程式的基本功夫之一就是排版,不只給自己看,還要給其他參考或維護的人能一目瞭然

以上是我的小小建議,希望您能參考~

 
#19405: Re:給延平的同仁 c++ AC


089487 (089487)

學校 : 國立臺灣師範大學附屬高級中學
編號 : 82069
來源 : [220.130.10.185]
最後登入時間 :
2024-04-01 11:16:18
a013. 羅馬數字 -- NPSC 模擬試題 | From: [223.136.150.251] | 發表日期 : 2019-09-29 11:36

 


老實說,我蠻希望你排版,雖然我不是延平的

但至少給同校同學,甚至使用這個網站的網友,能看的舒服

寫程式的基本功夫之一就是排版,不只給自己看,還要給其他參考或維護的人能一目瞭然

以上是我的小小建議,希望您能參考~



謝謝提醒

 
#19751: Re:給延平的同仁 c++ AC


ufve0704 (爬 我爬 我爬爬爬 有排行榜這種東西就是要爬 爬過我上面的那...)

學校 : 臺北市私立延平高級中學
編號 : 83268
來源 : [203.72.178.1]
最後登入時間 :
2023-10-30 13:02:50
a013. 羅馬數字 -- NPSC 模擬試題 | From: [114.42.211.145] | 發表日期 : 2019-10-27 09:56

 #include<bits/stdc++.h>
using namespace std;
int length(int);
int main()
{
int n,m,z,r,e=0,k,e2,line=0;
char c[3][2]={{'I','V'},
{'X','L'},
{'C','D'}};
int num[3][2]={{1,5},{10,50},{100,500}};
string a,b;
bool f=false,f2;
while(cin>>a)
{
e=-2;
e2=-1;
if(a=="#") break;
cin>>b;
//if(line==20) {cout<<a<<" "<<b<<endl;continue;}
if(a==b) {cout<<"ZERO\n";continue;}
string h;
int x=0;
n=0,m=0;
//f2=false;
for(int i=0;i<a.length();i++)
{
f=false;
for(int j=0;j<3;j++)
{
int g;
for( g=0;g<2;g++) if(a[i]==c[j][g]){k=num[j][g];f=true;break;}
if(f)
{
if(e==-2)
{
if(i==a.length()-1) n+=k;
e=k;
}
else if(e==k) n+=e+k,e=-2;
else if(k>e)
{
n+=k-e;
e=-2;
}
else if(e>k)
{
n+=(e!=1000)*e,e=k;
if(i==a.length()-1) n+=k;
}
break;
}

}
if(f==false)
{
if(e==-2) n+=1000;
else if(e==1000) n+=1000;
else if(e==-1) n+=1000;
else if(e<1000) n+=1000-e;
else n+=1000+e;
e=1000;
}

}
//if(a.length()==1||f2) n+=e;
//cout<<n<<" ";
e=-2;
f2=false;
for(int i=0;i<b.length();i++)
{
f=false;
for(int j=0;j<3;j++)
{
int g;
for( g=0;g<2;g++) if(b[i]==c[j][g]){k=num[j][g];f=true;break;}
if(f)
{
if(e==-2)
{
if(i==b.length()-1) m+=k;
e=k;
}
else if(e==k) m+=e+k,e=-2;
else if(k>e)
{
m+=k-e;
e=-2;
}
else if(e>k)
{
m+=(e!=1000)*e,e=k;
if(i==b.length()-1) m+=k;
}
break;
}

}
if(f==false)
{
if(e==-2) m+=1000;
else if(e==1000) m+=1000;
else if(e==-1) m+=1000;
else if(e<1000) m+=1000-e;
else m+=1000+e;
e=1000;
}
// cout<<m<<" ";
}
//if(b.length()==1||f2) m+=e;
//cout<<m<<" ";
x=abs(n-m);
//x=444;
//cout<<x<<endl;
if(x==0) {cout<<"ZERO\n";continue;}
string s;
int p,l;
l=length(x);
while(l>1)
{
p=pow(10,l-1);
if(x/p==0) s+='0';
else s+=x/p+'0';
x%=p;
l--;
//cout<<s<<" ";
}
s+=x+'0';
//cout<<s<<endl;
r=0;
for(int i=s.length()-1;i>=0;i--)
{
if(s[i]=='0') {r++;continue;}
if(s.length()-i-1==3) for(int j=0;j<s[i]-'0';j++) h+='M';
else
{
if(s[i]>='5'&&s[i]<'9'){
//cout<<c[s.length()-i-1][0];
for(int mm=0;mm<(s[i]-'5');mm++) h+=c[s.length()-i-1][0];
//cout<<h.length()<<endl;
h+=c[s.length()-i-1][1];
r=0;
}
else if(s[i]=='9')
{
if(s.length()-i==3) h+='M';
else h+=c[s.length()-i][0];
h+=c[s.length()-1-i][0];
r=0;
}
else if(s[i]=='4')
{
h+=c[s.length()-i-1][1];
h+=c[s.length()-1-i][0];
r=0;
}
else
{
for(int mm=0;mm<s[i]-'0';mm++) h+=c[s.length()-i-1][0];
r=0;
}
}
}
string h2;
for(int i=h.length()-1;i>=0;i--) h2+=h[i];
/* if(h2=="MMDCCLXIII")
{
cout<<a<<"|"<<b<<endl;
continue;
}*/
cout<<h2<<endl;
}
}
int length(int n)
{
int num=1;
while(n>=10) n/=10,num++;
return num;
}

這是我的寫法

建好表比較簡潔

#include <bits/stdc++.h>

using namespace std;

char c[7]={'I','V','X','L','C','D','M'};

int cc[7]={1,5,10,50,100,500,1000};

string ccc[13]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};

int cccc[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000};

int ch(char a){

for(int b=0;b<7;b++)

if(a==c[b])

return b;

}

int in(string a){

int d=0;

for(int b=0;b<a.size();b++){

if(!(b+1==a.size())&&ch(a[b])<ch(a[b+1]))

d-=cc[ch(a[b])];

else

d+=cc[ch(a[b])];

}

return d;

}

void out(int a){

if(a==0)

cout<<"ZERO"<<endl;

else{

string b="";

for(int d=12;d>=0;d--){

for(int e=0;e<a/cccc[d];e++)

b+=ccc[d];

a%=cccc[d];

}

cout<<b<<endl;

}

}

int main(){

string a,b;

while(cin>>a&&a[0]!='#'){

cin>>b;

out(abs(in(a)-in(b)));

}

}

 
#19754: Re:給延平的同仁 c++ AC


089487 (089487)

學校 : 國立臺灣師範大學附屬高級中學
編號 : 82069
來源 : [220.130.10.185]
最後登入時間 :
2024-04-01 11:16:18
a013. 羅馬數字 -- NPSC 模擬試題 | From: [223.136.89.104] | 發表日期 : 2019-10-27 11:46

 #include<bits/stdc++.h>
using namespace std;
int length(int);
int main()
{
int n,m,z,r,e=0,k,e2,line=0;
char c[3][2]={{'I','V'},
{'X','L'},
{'C','D'}};
int num[3][2]={{1,5},{10,50},{100,500}};
string a,b;
bool f=false,f2;
while(cin>>a)
{
e=-2;
e2=-1;
if(a=="#") break;
cin>>b;
//if(line==20) {cout<<a<<" "<<b<<endl;continue;}
if(a==b) {cout<<"ZERO\n";continue;}
string h;
int x=0;
n=0,m=0;
//f2=false;
for(int i=0;i<a.length();i++)
{
f=false;
for(int j=0;j<3;j++)
{
int g;
for( g=0;g<2;g++) if(a[i]==c[j][g]){k=num[j][g];f=true;break;}
if(f)
{
if(e==-2)
{
if(i==a.length()-1) n+=k;
e=k;
}
else if(e==k) n+=e+k,e=-2;
else if(k>e)
{
n+=k-e;
e=-2;
}
else if(e>k)
{
n+=(e!=1000)*e,e=k;
if(i==a.length()-1) n+=k;
}
break;
}

}
if(f==false)
{
if(e==-2) n+=1000;
else if(e==1000) n+=1000;
else if(e==-1) n+=1000;
else if(e<1000) n+=1000-e;
else n+=1000+e;
e=1000;
}

}
//if(a.length()==1||f2) n+=e;
//cout<<n<<" ";
e=-2;
f2=false;
for(int i=0;i<b.length();i++)
{
f=false;
for(int j=0;j<3;j++)
{
int g;
for( g=0;g<2;g++) if(b[i]==c[j][g]){k=num[j][g];f=true;break;}
if(f)
{
if(e==-2)
{
if(i==b.length()-1) m+=k;
e=k;
}
else if(e==k) m+=e+k,e=-2;
else if(k>e)
{
m+=k-e;
e=-2;
}
else if(e>k)
{
m+=(e!=1000)*e,e=k;
if(i==b.length()-1) m+=k;
}
break;
}

}
if(f==false)
{
if(e==-2) m+=1000;
else if(e==1000) m+=1000;
else if(e==-1) m+=1000;
else if(e<1000) m+=1000-e;
else m+=1000+e;
e=1000;
}
// cout<<m<<" ";
}
//if(b.length()==1||f2) m+=e;
//cout<<m<<" ";
x=abs(n-m);
//x=444;
//cout<<x<<endl;
if(x==0) {cout<<"ZERO\n";continue;}
string s;
int p,l;
l=length(x);
while(l>1)
{
p=pow(10,l-1);
if(x/p==0) s+='0';
else s+=x/p+'0';
x%=p;
l--;
//cout<<s<<" ";
}
s+=x+'0';
//cout<<s<<endl;
r=0;
for(int i=s.length()-1;i>=0;i--)
{
if(s[i]=='0') {r++;continue;}
if(s.length()-i-1==3) for(int j=0;j<s[i]-'0';j++) h+='M';
else
{
if(s[i]>='5'&&s[i]<'9'){
//cout<<c[s.length()-i-1][0];
for(int mm=0;mm<(s[i]-'5');mm++) h+=c[s.length()-i-1][0];
//cout<<h.length()<<endl;
h+=c[s.length()-i-1][1];
r=0;
}
else if(s[i]=='9')
{
if(s.length()-i==3) h+='M';
else h+=c[s.length()-i][0];
h+=c[s.length()-1-i][0];
r=0;
}
else if(s[i]=='4')
{
h+=c[s.length()-i-1][1];
h+=c[s.length()-1-i][0];
r=0;
}
else
{
for(int mm=0;mm<s[i]-'0';mm++) h+=c[s.length()-i-1][0];
r=0;
}
}
}
string h2;
for(int i=h.length()-1;i>=0;i--) h2+=h[i];
/* if(h2=="MMDCCLXIII")
{
cout<<a<<"|"<<b<<endl;
continue;
}*/
cout<<h2<<endl;
}
}
int length(int n)
{
int num=1;
while(n>=10) n/=10,num++;
return num;
}

這是我的寫法

建好表比較簡潔

#include <bits/stdc++.h>

using namespace std;

char c[7]={'I','V','X','L','C','D','M'};

int cc[7]={1,5,10,50,100,500,1000};

string ccc[13]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};

int cccc[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000};

int ch(char a){

for(int b=0;b<7;b++)

if(a==c[b])

return b;

}

int in(string a){

int d=0;

for(int b=0;b<a.size();b++){

if(!(b+1==a.size())&&ch(a[b])<ch(a[b+1]))

d-=cc[ch(a[b])];

else

d+=cc[ch(a[b])];

}

return d;

}

void out(int a){

if(a==0)

cout<<"ZERO"<<endl;

else{

string b="";

for(int d=12;d>=0;d--){

for(int e=0;e<a/cccc[d];e++)

b+=ccc[d];

a%=cccc[d];

}

cout<<b<<endl;

}

}

int main(){

string a,b;

while(cin>>a&&a[0]!='#'){

cin>>b;

out(abs(in(a)-in(b)));

}

}

新的寫法:

#include<bits/stdc++.h>

using namespace std;

int main()

{

int t;

char c[7]={'I', 'V', 'X', 'L', 'C', 'D', 'M'};

int n[7]={1, 5, 10, 50, 100, 500, 1000};

string s2[2];

while(cin>>s2[0]&&s2[0]!="#")

{

cin>>s2[1];

int l[2];

 

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

{

string s=s2[r];

//cout<<s<<" ";

l[r]=0;

char a='z';

for(int i=s.length()-1;i>=0;i--)

{

if(a=='z') 

{

//cout<<find(c,c+7,s[i])-c<<" ";

l[r]+=n[find(c,c+7,s[i])-c];

a=s[i];

}

else

{

if(find(c,c+7,s[i])-c<find(c,c+7,a)-c)

{

l[r]-=n[find(c,c+7,s[i])-c];

a='z';

}

else

{

l[r]+=n[find(c,c+7,s[i])-c];

a=s[i];

}

}

//cout<<l[r]<<" ";

}

 

}

int num=abs(l[0]-l[1]);

//cout<<num<<" ";

if(!num) {cout<<"ZERO\n";continue;

stringstream ss;

ss<<num;

string s;

ss>>s;

for(int i=0;i<s.length();i++)

{

if(s[i]=='0') continue;

if(s.length()-i-1==3) cout<<string(s[i]-'0','M');

else 

{

int p=pow(10,s.length()-i-1);

if(s[i]=='9'||s[i]=='4')

{

cout<<c[find(n,n+7,p)-n]<<c[find(n,n+7,p*(s[i]-'0'+1))-n];

else

{

if(s[i]>='5') cout<<c[find(n,n+7,5*p)-n];

cout<<string((s[i]-'0')%5,c[find(n,n+7,p)-n]);

//cout<<"p"<<p<<"\n";

}

}

 

 

}

cout<<"\n";

}

}

 

AC (2ms, 324KB)
 
#20529: Re:給延平的同仁 c++ AC


Easonsfriend (去寫./Problems?ownerid=89827)

學校 : 不指定學校
編號 : 89827
來源 : [106.105.188.90]
最後登入時間 :
2024-04-03 19:33:43
a013. 羅馬數字 -- NPSC 模擬試題 | From: [106.105.188.90] | 發表日期 : 2020-02-03 23:54

 #include<bits/stdc++.h>
using namespace std;
int length(int);
int main()
{
int n,m,z,r,e=0,k,e2,line=0;
char c[3][2]={{'I','V'},
{'X','L'},
{'C','D'}};
int num[3][2]={{1,5},{10,50},{100,500}};
string a,b;
bool f=false,f2;
while(cin>>a)
{
e=-2;
e2=-1;
if(a=="#") break;
cin>>b;
//if(line==20) {cout<<a<<" "<<b<<endl;continue;}
if(a==b) {cout<<"ZERO\n";continue;}
string h;
int x=0;
n=0,m=0;
//f2=false;
for(int i=0;i<a.length();i++)
{
f=false;
for(int j=0;j<3;j++)
{
int g;
for( g=0;g<2;g++) if(a[i]==c[j][g]){k=num[j][g];f=true;break;}
if(f)
{
if(e==-2)
{
if(i==a.length()-1) n+=k;
e=k;
}
else if(e==k) n+=e+k,e=-2;
else if(k>e)
{
n+=k-e;
e=-2;
}
else if(e>k)
{
n+=(e!=1000)*e,e=k;
if(i==a.length()-1) n+=k;
}
break;
}

}
if(f==false)
{
if(e==-2) n+=1000;
else if(e==1000) n+=1000;
else if(e==-1) n+=1000;
else if(e<1000) n+=1000-e;
else n+=1000+e;
e=1000;
}

}
//if(a.length()==1||f2) n+=e;
//cout<<n<<" ";
e=-2;
f2=false;
for(int i=0;i<b.length();i++)
{
f=false;
for(int j=0;j<3;j++)
{
int g;
for( g=0;g<2;g++) if(b[i]==c[j][g]){k=num[j][g];f=true;break;}
if(f)
{
if(e==-2)
{
if(i==b.length()-1) m+=k;
e=k;
}
else if(e==k) m+=e+k,e=-2;
else if(k>e)
{
m+=k-e;
e=-2;
}
else if(e>k)
{
m+=(e!=1000)*e,e=k;
if(i==b.length()-1) m+=k;
}
break;
}

}
if(f==false)
{
if(e==-2) m+=1000;
else if(e==1000) m+=1000;
else if(e==-1) m+=1000;
else if(e<1000) m+=1000-e;
else m+=1000+e;
e=1000;
}
// cout<<m<<" ";
}
//if(b.length()==1||f2) m+=e;
//cout<<m<<" ";
x=abs(n-m);
//x=444;
//cout<<x<<endl;
if(x==0) {cout<<"ZERO\n";continue;}
string s;
int p,l;
l=length(x);
while(l>1)
{
p=pow(10,l-1);
if(x/p==0) s+='0';
else s+=x/p+'0';
x%=p;
l--;
//cout<<s<<" ";
}
s+=x+'0';
//cout<<s<<endl;
r=0;
for(int i=s.length()-1;i>=0;i--)
{
if(s[i]=='0') {r++;continue;}
if(s.length()-i-1==3) for(int j=0;j<s[i]-'0';j++) h+='M';
else
{
if(s[i]>='5'&&s[i]<'9'){
//cout<<c[s.length()-i-1][0];
for(int mm=0;mm<(s[i]-'5');mm++) h+=c[s.length()-i-1][0];
//cout<<h.length()<<endl;
h+=c[s.length()-i-1][1];
r=0;
}
else if(s[i]=='9')
{
if(s.length()-i==3) h+='M';
else h+=c[s.length()-i][0];
h+=c[s.length()-1-i][0];
r=0;
}
else if(s[i]=='4')
{
h+=c[s.length()-i-1][1];
h+=c[s.length()-1-i][0];
r=0;
}
else
{
for(int mm=0;mm<s[i]-'0';mm++) h+=c[s.length()-i-1][0];
r=0;
}
}
}
string h2;
for(int i=h.length()-1;i>=0;i--) h2+=h[i];
/* if(h2=="MMDCCLXIII")
{
cout<<a<<"|"<<b<<endl;
continue;
}*/
cout<<h2<<endl;
}
}
int length(int n)
{
int num=1;
while(n>=10) n/=10,num++;
return num;
}

這是我的寫法

建好表比較簡潔

#include <bits/stdc++.h>

using namespace std;

char c[7]={'I','V','X','L','C','D','M'};

int cc[7]={1,5,10,50,100,500,1000};

string ccc[13]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};

int cccc[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000};

int ch(char a){

for(int b=0;b<7;b++)

if(a==c[b])

return b;

}

int in(string a){

int d=0;

for(int b=0;b<a.size();b++){

if(!(b+1==a.size())&&ch(a[b])<ch(a[b+1]))

d-=cc[ch(a[b])];

else

d+=cc[ch(a[b])];

}

return d;

}

void out(int a){

if(a==0)

cout<<"ZERO"<<endl;

else{

string b="";

for(int d=12;d>=0;d--){

for(int e=0;e<a/cccc[d];e++)

b+=ccc[d];

a%=cccc[d];

}

cout<<b<<endl;

}

}

int main(){

string a,b;

while(cin>>a&&a[0]!='#'){

cin>>b;

out(abs(in(a)-in(b)));

}

}

新的寫法:

#include<bits/stdc++.h>

using namespace std;

int main()

{

int t;

char c[7]={'I', 'V', 'X', 'L', 'C', 'D', 'M'};

int n[7]={1, 5, 10, 50, 100, 500, 1000};

string s2[2];

while(cin>>s2[0]&&s2[0]!="#")

{

cin>>s2[1];

int l[2];

 

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

{

string s=s2[r];

//cout<<s<<" ";

l[r]=0;

char a='z';

for(int i=s.length()-1;i>=0;i--)

{

if(a=='z') 

{

//cout<<find(c,c+7,s[i])-c<<" ";

l[r]+=n[find(c,c+7,s[i])-c];

a=s[i];

}

else

{

if(find(c,c+7,s[i])-c<find(c,c+7,a)-c)

{

l[r]-=n[find(c,c+7,s[i])-c];

a='z';

}

else

{

l[r]+=n[find(c,c+7,s[i])-c];

a=s[i];

}

}

//cout<<l[r]<<" ";

}

 

}

int num=abs(l[0]-l[1]);

//cout<<num<<" ";

if(!num) {cout<<"ZERO\n";continue;

stringstream ss;

ss<<num;

string s;

ss>>s;

for(int i=0;i<s.length();i++)

{

if(s[i]=='0') continue;

if(s.length()-i-1==3) cout<<string(s[i]-'0','M');

else 

{

int p=pow(10,s.length()-i-1);

if(s[i]=='9'||s[i]=='4')

{

cout<<c[find(n,n+7,p)-n]<<c[find(n,n+7,p*(s[i]-'0'+1))-n];

else

{

if(s[i]>='5') cout<<c[find(n,n+7,5*p)-n];

cout<<string((s[i]-'0')%5,c[find(n,n+7,p)-n]);

//cout<<"p"<<p<<"\n";

}

}

 

 

}

cout<<"\n";

}

}

 

AC (2ms, 324KB)

#include <iostream>

#include <cmath>

using namespace std;

 

int a[1000];

 

int r2i(string s){

    int sum=0;

    for(int i=0;i<s.length();i++){

        if(s[i]=='I')a[i]=1;

        else if(s[i]=='V')a[i]=5;

        else if(s[i]=='X')a[i]=10;

        else if(s[i]=='L')a[i]=50;

        else if(s[i]=='C')a[i]=100;

        else if(s[i]=='D')a[i]=500;

        else if(s[i]=='M')a[i]=1000;

    }

    for(int i=0;i<s.length()-1;i++){

        if(a[i]<a[i+1])a[i]*=-1;

    }

    for(int i=0;i<s.length();i++){

        sum+=a[i];

    }

    return sum;

}

 

 

string i2r(int n){

    string s;

    while(n>0){

        if(n>=1000){s+="M";n-=1000;}

        else if(n>=900){s+="CM";n-=900;}

        else if(n>=500){s+="D";n-=500;}

        else if(n>=400){s+="CD";n-=400;}

        else if(n>=100){s+="C";n-=100;}

        else if(n>=90){s+="XC";n-=90;}

        else if(n>=50){s+="L";n-=50;}

        else if(n>=40){s+="XL";n-=40;}

        else if(n>=10){s+="X";n-=10;}

        else if(n>=9){s+="IX";n-=9;}

        else if(n>=5){s+="V";n-=5;}

        else if(n>=4){s+="IV";n-=4;}

        else if(n>=1){s+="I";n-=1;}

    }

    return s;

    

}

 

 

int main() {

    

    ios_base::sync_with_stdio(false); cin.tie(NULL);

    string s1,s2;

    while(cin>>s1>>s2){

        if(s1=="#")return 0;

        if(s1==s2){

            cout<<"ZERO\n";

            continue;

        }

        int sum1,sum2;

        sum1=r2i(s1);

        sum2=r2i(s2);

        cout<<i2r(abs(sum1-sum2))<<"\n";

    

    }

    

    

    

}

 

#0: 100% AC (2ms, 320KB)

#我的寫法



 
ZeroJudge Forum