結果
| 問題 |
No.518 ローマ数字の和
|
| コンテスト | |
| ユーザー |
tatuyan_edson
|
| 提出日時 | 2017-03-27 20:33:13 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 2,000 ms |
| コード長 | 1,477 bytes |
| コンパイル時間 | 222 ms |
| コンパイル使用メモリ | 31,744 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-06 12:18:13 |
| 合計ジャッジ時間 | 1,098 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int RomanToNum(char *roman);
char * NumToRoman(int n);
int main(void){
int n,sum=0;
char roman[24];
scanf("%d%*c",&n);
while(n--){
scanf("%s%*c",roman);
sum+=RomanToNum(roman);
}
puts(NumToRoman(sum));
return 0;
}
int RomanToNum(char *roman){
const int num[]={1000,500,100,50,10,5,1};
const char rome[]="MDCLXVI";
int i,l,n=0;
l=strlen(roman);
for(i=0;i<l;i++){
if(i+1!=l && strchr(rome,roman[i])-rome>strchr(rome,roman[i+1])-rome){
n+=num[strchr(rome,roman[i+1])-rome]-num[strchr(rome,roman[i])-rome];
i++;
}else{
n+=num[strchr(rome,roman[i])-rome];
}
}
return n;
}
char * NumToRoman(int n){
static char str[32]="";
const int num[]={1000,500,100,50,10,5,1};
const char rome[]="MDCLXVI";
int i,j,k,now=0,tmp;
if(n>3999) return "ERROR";
for(i=1000;i>0;i/=10){
tmp=n/i%10;
if(tmp==0) continue;
if(tmp==9){
for(j=0;num[j]!=i;j++);
str[now++]=rome[j];
for(j=0;num[j]!=i*10;j++);
str[now++]=rome[j];
}else if(tmp>=5){
for(j=0;num[j]!=i*5;j++);
str[now++]=rome[j];
for(j=0;num[j]!=i;j++);
for(k=0;k<tmp-5;k++) str[now++]=rome[j];
}else if(tmp==4){
for(j=0;num[j]!=i;j++);
str[now++]=rome[j];
for(j=0;num[j]!=i*5;j++);
str[now++]=rome[j];
}else{
for(j=0;num[j]!=i;j++);
for(k=0;k<tmp;k++) str[now++]=rome[j];
}
}
return str;
}
tatuyan_edson