#28973: 兩個測資都對 卡在line3 可以請大神們幫我看一下嗎?


i_hsuan (hsuan)

學校 : 國立文華高級中學
編號 : 91315
來源 : [111.82.22.79]
最後登入時間 :
2022-10-10 12:38:18
a013. 羅馬數字 -- NPSC 模擬試題 | From: [111.82.72.245] | 發表日期 : 2022-01-19 12:25

#include<stdio.h>
#include<string.h>

int main(){
int inputa=0;
int inputb=0;
int value=0;
 

while(1){
inputa=0;
inputb=0;
int numarr[4]={0};
char word={0};
char word0={'A'};
 
while(1){
scanf("%c",&word);
if(word==32){
break;
}
else if(word=='#'){
return 0;
}
 
if(word=='I'){
inputa++;
}
else if(word=='V'){
inputa+=5;
if(word0=='I'){
inputa-=2;
}
}
else if(word=='X'){
inputa+=10;
if(word0=='I'){
inputa-=2;
}
}
else if(word=='L'){
inputa+=50;
if(word0=='X'){
inputa-=20;
}
}
else if(word=='C'){
inputa+=100;
if(word0=='X'){
inputa-=20;
}
}
else if(word=='D'){
inputa+=500;
if(word0=='C'){
inputa-=200;
}
}
else if(word=='M'){
inputa+=1000;
if(word0=='C'){
inputa-=200;
}
}
word0=word;
 
}
//printf("%d\n",inputa);

while(1){
scanf("%c",&word);
if(word==10){
break;
}
if(word=='I'){
inputb++;
}
else if(word=='V'){
inputb+=5;
if(word0=='I'){
inputb-=2;
}
}
else if(word=='X'){
inputb+=10;
if(word0=='I'){
inputb-=2;
}
}
else if(word=='L'){
inputb+=50;
if(word0=='X'){
inputb-=20;
}
}
else if(word=='C'){
inputb+=100;
if(word0=='X'){
inputb-=20;
}
}
else if(word=='D'){
inputb+=500;
if(word0=='C'){
inputb-=200;
}
}
else if(word=='M'){
inputb+=1000;
if(word0=='C'){
inputb-=200;
}
}
word0=word;
 
}
//printf("-%d\n",inputb);
value=inputa-inputb;
if(value<0){
value*=-1;
}
else if(value==0){
printf("ZERO");
}
 

for(int i=3;i>=0;i--){
numarr[i]=value%10;
value/=10;
//printf("r");
}
while(numarr[0]>=1){
printf("M");
numarr[0]-=1;
}
if(numarr[1]==9){
printf("CM");
}
else{
if(numarr[1]>=5&&numarr[1]<=8){
printf("D");
numarr[1]-=5;
}
while(numarr[1]>=1){
printf("C");
numarr[1]-=1;
}
}
if(numarr[2]==9){
printf("XC");
}
else{
if(numarr[2]>=5&&numarr[2]<=8){
printf("L");
numarr[2]-=5;
}
while(numarr[2]>=1){
printf("X");
numarr[2]-=1;
}
}
if(numarr[3]==9){
printf("IX");
}
else{
if(numarr[3]>=5&&numarr[3]<=8){
printf("V");
numarr[3]-=5;
}
while(numarr[3]>=1){
printf("I");
numarr[3]-=1;
}
}
printf("\n");
}
}
 
#28974: Re:兩個測資都對 卡在line3 可以請大神們幫我看一下嗎?


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [39.9.74.255]
最後登入時間 :
2024-10-14 22:20:08
a013. 羅馬數字 -- NPSC 模擬試題 | From: [110.26.128.118] | 發表日期 : 2022-01-19 13:10

#include<stdio.h>
#include<string.h>

int main(){
int inputa=0;
int inputb=0;
int value=0;
 

while(1){
inputa=0;
inputb=0;
int numarr[4]={0};
char word={0};
char word0={'A'};
 
while(1){
scanf("%c",&word);
if(word==32){
break;
}
else if(word=='#'){
return 0;
}
 
if(word=='I'){
inputa++;
}
else if(word=='V'){
inputa+=5;
if(word0=='I'){
inputa-=2;
}
}
else if(word=='X'){
inputa+=10;
if(word0=='I'){
inputa-=2;
}
}
else if(word=='L'){
inputa+=50;
if(word0=='X'){
inputa-=20;
}
}
else if(word=='C'){
inputa+=100;
if(word0=='X'){
inputa-=20;
}
}
else if(word=='D'){
inputa+=500;
if(word0=='C'){
inputa-=200;
}
}
else if(word=='M'){
inputa+=1000;
if(word0=='C'){
inputa-=200;
}
}
word0=word;
 
}
//printf("%d\n",inputa);

while(1){
scanf("%c",&word);
if(word==10){
break;
}
if(word=='I'){
inputb++;
}
else if(word=='V'){
inputb+=5;
if(word0=='I'){
inputb-=2;
}
}
else if(word=='X'){
inputb+=10;
if(word0=='I'){
inputb-=2;
}
}
else if(word=='L'){
inputb+=50;
if(word0=='X'){
inputb-=20;
}
}
else if(word=='C'){
inputb+=100;
if(word0=='X'){
inputb-=20;
}
}
else if(word=='D'){
inputb+=500;
if(word0=='C'){
inputb-=200;
}
}
else if(word=='M'){
inputb+=1000;
if(word0=='C'){
inputb-=200;
}
}
word0=word;
 
}
//printf("-%d\n",inputb);
value=inputa-inputb;
if(value<0){
value*=-1;
}
else if(value==0){
printf("ZERO");
}
 

for(int i=3;i>=0;i--){
numarr[i]=value%10;
value/=10;
//printf("r");
}
while(numarr[0]>=1){
printf("M");
numarr[0]-=1;
}
if(numarr[1]==9){
printf("CM");
}
else{
if(numarr[1]>=5&&numarr[1]<=8){
printf("D");
numarr[1]-=5;
}
while(numarr[1]>=1){
printf("C");
numarr[1]-=1;
}
}
if(numarr[2]==9){
printf("XC");
}
else{
if(numarr[2]>=5&&numarr[2]<=8){
printf("L");
numarr[2]-=5;
}
while(numarr[2]>=1){
printf("X");
numarr[2]-=1;
}
}
if(numarr[3]==9){
printf("IX");
}
else{
if(numarr[3]>=5&&numarr[3]<=8){
printf("V");
numarr[3]-=5;
}
while(numarr[3]>=1){
printf("I");
numarr[3]-=1;
}
}
printf("\n");
}
}


輸入第二個羅馬數字前把word0改掉

 
ZeroJudge Forum