#13357: 看起來蠻精簡的阿,可是一直超時!


unknown111 (unknown111)

學校 : 國立臺中科技大學
編號 : 74848
來源 : [114.35.87.137]
最後登入時間 :
2018-02-23 10:40:53
a013. 羅馬數字 -- NPSC 模擬試題 | From: [114.35.87.137] | 發表日期 : 2018-02-07 16:39

#include <stdio.h>

int priority(char);
int numcheck(char, char);
void print(int);
main(){
char num1[2];
char num2[2];

while (scanf("%s%s", num1, num2) != '#'){

int sub1, sub2;//兩個數字分別的總和
sub1 = numcheck(num1[0], num1[1]);
sub2 = numcheck(num2[0], num2[1]);

int total;
if (sub1 >= sub2) total = sub1 - sub2;
else total = -sub1 + sub2;
if (total == 0) printf("ZERO");
else {
int allnum[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; //MDCLXVI
char allchar[13][2] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int i;
for (i = 0; i <= 12; i++){
while (total > allnum[i]){
total -= allnum[i];
if (i % 2 != 0)
printf("%c%c", allchar[i][0], allchar[i][1]);
else
printf("%c", allchar[i][0]);
}
}
printf("\n");

}
}
return 0;
}


int numcheck(char num1, char num2){ //計算第一個和第二個數字是多少
int pr1, pr2;
pr1 = priority(num1);
pr2 = priority(num2);
if (pr1 < pr2) return -pr1 + pr2;
else return pr1 +pr2;
}


int priority(char num){
switch (num){
case 'I':
return 1;
break;
case 'V':
return 5;
break;
case 'X':
return 10;
break;
case 'L':
return 50;
break;
case 'C':
return 100;
break;
case 'D':
return 500;
break;
case 'M':
return 1000;
break;
}
}

 
#13358: Re:看起來蠻精簡的阿,可是一直超時!


justinO__o (夜貓)

學校 : 臺北市立成功高級中學
編號 : 51052
來源 : [111.71.212.14]
最後登入時間 :
2024-09-22 17:57:48
a013. 羅馬數字 -- NPSC 模擬試題 | From: [42.73.247.206] | 發表日期 : 2018-02-07 17:24

#include

int priority(char);
int numcheck(char, char);
void print(int);
main(){
char num1[2];
char num2[2];

while (scanf("%s%s", num1, num2) != '#'){

int sub1, sub2;//兩個數字分別的總和
sub1 = numcheck(num1[0], num1[1]);
sub2 = numcheck(num2[0], num2[1]);

int total;
if (sub1 >= sub2) total = sub1 - sub2;
else total = -sub1 + sub2;
if (total == 0) printf("ZERO");
else {
int allnum[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; //MDCLXVI
char allchar[13][2] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int i;
for (i = 0; i <= 12; i++){
while (total > allnum[i]){
total -= allnum[i];
if (i % 2 != 0)
printf("%c%c", allchar[i][0], allchar[i][1]);
else
printf("%c", allchar[i][0]);
}
}
printf("\n");

}
}
return 0;
}


int numcheck(char num1, char num2){ //計算第一個和第二個數字是多少
int pr1, pr2;
pr1 = priority(num1);
pr2 = priority(num2);
if (pr1 < pr2) return -pr1 + pr2;
else return pr1 +pr2;
}


int priority(char num){
switch (num){
case 'I':
return 1;
break;
case 'V':
return 5;
break;
case 'X':
return 10;
break;
case 'L':
return 50;
break;
case 'C':
return 100;
break;
case 'D':
return 500;
break;
case 'M':
return 1000;
break;
}
}




while (scanf("%s", num1) && num1 != '#') {

    scanf("%s", num2);

...

}

 

應該是這樣吧

 
ZeroJudge Forum