結果

問題 No.518 ローマ数字の和
ユーザー tatuyan_edsontatuyan_edson
提出日時 2017-03-27 20:33:13
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,477 bytes
コンパイル時間 558 ms
コンパイル使用メモリ 30,376 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 17:19:36
合計ジャッジ時間 1,649 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 0 ms
4,376 KB
testcase_03 AC 0 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 0 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 0 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 0 ms
4,376 KB
testcase_11 AC 0 ms
4,376 KB
testcase_12 AC 0 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 0 ms
4,376 KB
testcase_17 AC 0 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 0 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}


0