如標題
#include <stdio.h>
#include <math.h>
#include <string.h>
#include<stdlib.h>
typedef struct {
char *ch;
} romnum;
int main(void) {
romnum roc[] = {
{ .ch = "M" },
{ .ch = "CM" },
{ .ch = "D" },
{ .ch = "CD" },
{ .ch = "C" },
{ .ch = "XC" },
{ .ch = "L" },
{ .ch = "XL" },
{ .ch = "X" },
{ .ch = "IX" },
{ .ch = "V" },
{ .ch = "IV" },
{ .ch= "I" },
};
char a[15],b[15];
int ta[15],tb[15];
char rc[]={'I','V','X','L','C','D','M'};
int rn[]={1,5,10,50,100,500,1000};
int ron[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
int na=-1,nb=-1;
while(scanf("%s",a)!=EOF){
if(a[0]=='#' && a[1]=='\0'){
break;
}
else{
scanf("%s",b);
}
//辨別字元並加入到比較陣列裡
for(int i=0;i<=strlen(a);i++){
for(int j=0;j<=7;j++){
if(a[i]==rc[j]){
ta[i]=rn[j];
}
}
}
for(int i=0;i<=strlen(b);i++){
for(int j=0;j<=7;j++){
if(b[i]==rc[j]){
tb[i]=rn[j];
}
}
}
//比較大小並加總
for(int i=0;i<=strlen(a);i++){
if(ta[i+1]>ta[i]){
ta[i]=~ta[i]+1;
na+=ta[i];
}
else{
na+=ta[i];
}
}
for(int i=0;i<=strlen(b);i++){
if(tb[i+1]>tb[i]){
tb[i]=~tb[i]+1;
nb+=tb[i];
}
else{
nb+=tb[i];
}
}
//兩數相減取絕對值
int sum=abs(na-nb);
//printf("%d\n",sum);
//轉換羅馬字
if(sum==0){
printf("ZERO\n");
}
else{
for(int i=0;i<13;i++){
if(sum<ron[i]){
continue;
}
while (sum>=ron[i]) {
sum-=ron[i];
printf("%s", roc[i].ch);
}
}
printf("\n");
}
na=0;
nb=0;
}
return 0;
}
如標題
#include
#include
#include
#include
typedef struct {
char *ch;
} romnum;
int main(void) {
romnum roc[] = {
{ .ch = "M" },
{ .ch = "CM" },
{ .ch = "D" },
{ .ch = "CD" },
{ .ch = "C" },
{ .ch = "XC" },
{ .ch = "L" },
{ .ch = "XL" },
{ .ch = "X" },
{ .ch = "IX" },
{ .ch = "V" },
{ .ch = "IV" },
{ .ch= "I" },
};
char a[15],b[15];
int ta[15],tb[15];
char rc[]={'I','V','X','L','C','D','M'};
int rn[]={1,5,10,50,100,500,1000};
int ron[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
int na=-1,nb=-1;
while(scanf("%s",a)!=EOF){
if(a[0]=='#' && a[1]=='\0'){
break;
}
else{
scanf("%s",b);
}
//辨別字元並加入到比較陣列裡
for(int i=0;i<=strlen(a);i++){
for(int j=0;j<=7;j++){
if(a[i]==rc[j]){
ta[i]=rn[j];
}
}
}
for(int i=0;i<=strlen(b);i++){
for(int j=0;j<=7;j++){
if(b[i]==rc[j]){
tb[i]=rn[j];
}
}
}
//比較大小並加總
for(int i=0;i<=strlen(a);i++){
if(ta[i+1]>ta[i]){
ta[i]=~ta[i]+1;
na+=ta[i];
}
else{
na+=ta[i];
}
}
for(int i=0;i<=strlen(b);i++){
if(tb[i+1]>tb[i]){
tb[i]=~tb[i]+1;
nb+=tb[i];
}
else{
nb+=tb[i];
}
}
//兩數相減取絕對值
int sum=abs(na-nb);
//printf("%d\n",sum);
//轉換羅馬字
if(sum==0){
printf("ZERO\n");
}
else{
for(int i=0;i<13;i++){
if(sum<ron[i]){
continue;
}
while (sum>=ron[i]) {
sum-=ron[i];
printf("%s", roc[i].ch);
}
}
printf("\n");
}
na=0;
nb=0;
}
return 0;
}
帥