請高手幫幫忙~~ 感謝
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int method(char x)
{
switch(x)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}
}
int main()
{
char x[10000],y[10000];
int num1,num2;
while(scanf("%s",x)!=EOF && x[0]!='#')
{
int sum1=0,sum2=0,i=0,m=0,sum,num3=0;
scanf("%s",y);
num1=strlen(x);
num2=strlen(y);
x[num1]=0;
y[num2]=0;
while(i<num1)
{
if(method(x[i])<method(x[i+1]))
sum1-=method(x[i]);
else
sum1+=method(x[i]);
i++;
}
while(m<num2)
{
if(method(y[m])<method(y[m+1]))
sum2-=method(y[m]);
else
sum2+=method(y[m]);
m++;
}
if(sum1>sum2)
sum=sum1-sum2;
else
sum=sum2-sum1;
if(sum==0){
printf("ZERO\n");continue;}
num3=(sum/1000)%10;
while(num3>0)
{
printf("M");
num3--;
}
num3=(sum/100)%10;
if(num3==9)
{
printf("CM");num3-=9;
}
if(num3>=5)
{
printf("D");
num3-=5;
}
else if(num3>=4){printf("CD");num3-=4;}
while(num3>0)
{
printf("C");
num3--;
}
num3=(sum/10)%10;
if(num3==9){
printf("XC");
num3-=9;
}
if(num3>=5)
{
printf("L");
num3-=5;
}
else if(num3>=4){printf("XL");num3-=4;}
while(num3>0)
{
printf("X");
num3--;
}
num3=sum%10;
if(num3==9){
printf("IX");
num3-=9;
}
if(num3>=5)
{
printf("V");
num3-=5;
}
else if(num3>=4){printf("IV");num3-=4;}
while(num3>0)
{
printf("I");
num3--;
}
printf("\n");
}
return 0;
}
上面複製錯誤 更改
--------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int method(char x)
{
switch(x)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}
}
int main()
{
char x[10000],y[10000];
int num1,num2;
while(scanf("%s",x)!=EOF && x[0]!='#')
{
int sum1=0,sum2=0,i=0,m=0,sum,num3=0;
scanf("%s",y);
num1=strlen(x);
num2=strlen(y);
while(i<num1)
{
if(method(x[i])<method(x[i+1]))
sum1-=method(x[i]);
else
sum1+=method(x[i]);
i++;
}
while(m<num2)
{
if(method(y[m])<method(y[m+1]))
sum2-=method(y[m]);
else
sum2+=method(y[m]);
m++;
}
if(sum1>sum2)
sum=sum1-sum2;
else
sum=sum2-sum1;
if(sum==0){
printf("ZERO\n");continue;}
num3=(sum/1000)%10;
while(num3>0)
{
printf("M");
num3--;
}
num3=(sum/100)%10;
if(num3==9)
{
printf("CM");num3-=9;
}
if(num3>=5)
{
printf("D");
num3-=5;
}
else if(num3>=4){printf("CD");num3-=4;}
while(num3>0)
{
printf("C");
num3--;
}
num3=(sum/10)%10;
if(num3==9){
printf("XC");
num3-=9;
}
if(num3>=5)
{
printf("L");
num3-=5;
}
else if(num3>=4){printf("XL");num3-=4;}
while(num3>0)
{
printf("X");
num3--;
}
num3=sum%10;
if(num3==9){
printf("IX");
num3-=9;
}
if(num3>=5)
{
printf("V");
num3-=5;
}
else if(num3>=4){printf("IV");num3-=4;}
while(num3>0)
{
printf("I");
num3--;
}
printf("\n");
}
return 0;
}
#include <iostream> #include <cstring> using namespace std; int TransNum(char a[]) ; // 羅馬字轉數字函數 void TransRmnan(int Ans); // 數字轉羅馬字函數 int main() { char a[30], b[30]; while (cin >> a) { if ( a[0] == '#') break; cin >> b; TransRmnan (abs(TransNum(a) - TransNum(b))); cout << endl; } return 0; } //________________________________ int TransNum(char a[]) { int Ans = 0; for (int i = 0; i < strlen(a); i++) { if (a[i] == a[i + 1] || a[i] == a[i - 1]) { if (a[i] == 'M') Ans += 1000; if (a[i] == 'D') Ans += 500; if (a[i] == 'C') Ans += 100; if (a[i] == 'L') Ans += 50; if (a[i] == 'X') Ans += 10; if (a[i] == 'V') Ans += 5; if (a[i] == 'I') Ans += 1; } else if (a[i] == 'C'&& a[i + 1] == 'M') { //六種減法組合 Ans += 900; i++; continue; } else if (a[i] == 'C'&& a[i + 1] == 'D') { Ans += 400; i++; continue; } else if (a[i] == 'X'&& a[i + 1] == 'C') { Ans += 90; i++; continue; } else if (a[i] == 'X'&& a[i + 1] == 'L') { Ans += 40; i++; continue; } else if (a[i] == 'I'&& a[i + 1] == 'X') { Ans += 9; i++; continue; } else if (a[i] == 'I'&& a[i + 1] == 'V') { Ans += 4; i++; continue; } else { if (a[i] == 'M') Ans += 1000; if (a[i] == 'D') Ans += 500; if (a[i] == 'C') Ans += 100; if (a[i] == 'L') Ans += 50; if (a[i] == 'X') Ans += 10; if (a[i] == 'V') Ans += 5; if (a[i] == 'I') Ans += 1; } } return Ans; } //---------------------------------------------------------------------------Output void TransRmnan(int Ans) { if (Ans / 1000 != 0) for (int i = 0; i < Ans / 1000; i++) cout << "M"; //---------- 千位 if ((Ans % 1000) / 100 == 9) //---------- 百位 cout << "CM"; if ((Ans % 1000) / 100 == 4) cout << "CD"; if ((Ans % 1000) / 100 == 5) cout << "D"; if ((Ans % 1000) / 100 == 1 || (Ans % 1000) / 100 == 2 || (Ans % 1000) / 100 == 3) { for (int i = 0; i < (Ans % 1000) / 100; i++) cout << "C"; } if ((Ans % 1000) / 100 == 6 || (Ans % 1000) / 100 == 7 || (Ans % 1000) / 100 == 8) { cout << "D"; for (int i = 0; i < (Ans % 1000) / 100 - 5; i++) cout << "C"; } if ((Ans % 100) / 10 == 9) //---------- 十位 cout << "XC"; if ((Ans % 100) / 10 == 4) cout << "XL"; if ((Ans % 100) / 10 == 5) cout << "L"; if ((Ans % 100) / 10 == 1 || (Ans % 100) / 10 == 2 || (Ans % 100) / 10 == 3) { for (int i = 0; i < (Ans % 100) / 10; i++) cout << "X"; } if ((Ans % 100) / 10 == 6 || (Ans % 100) / 10 == 7 || (Ans % 100) / 10 == 8) { cout << "L"; for (int i = 0; i < (Ans % 100) / 10 - 5; i++) cout << "X"; } if (Ans % 10 == 9) //---------- 個位 cout << "IX"; if (Ans % 10 == 4) cout << "IV"; if (Ans % 10 == 5) cout << "V"; if ((Ans % 10) == 1 || (Ans % 10) == 2 || (Ans % 10) == 3) { for (int i = 0; i < Ans % 10; i++) cout << "I"; } if ((Ans % 10) == 6 || (Ans % 10) == 7 || (Ans % 10) == 8) { cout << "V"; for (int i = 0; i < Ans % 10 - 5; i++) cout << "I"; } if (Ans == 0) cout << "ZERO"; }