Saturday, August 18, 2018

GO TO ROME


  • Problem Description

    Rafiq trying to convert roman to numbers. He asks maths teacher to know about the conversion. His maths teacher explained the conversion of roman to numbers. Then Rafiq understands the conversion concepts, by using that he write a C program to do conversion.
    Valid digits are I, V, X, L, C, D, M
  • CODING ARENA
  • #include <stdio.h>
    #include<string.h>
    int digit(char);
    int main()
    {
      char rom[30];
      int a[30],l,i,k,dec;
      scanf("%s",rom);
      l=strlen(rom);
      for(i=0;i<l;i++)
      {
        switch(rom[i])
        {
          case 'I':a[i]=1;
          break;
          case 'V':a[i]=5;
          break;
          case 'X':a[i]=10;
          break;
          case 'L':a[i]=50;
          break;
          case 'C':a[i]=100;
          break;
          case 'D':dec=dec+500;
          break;
          case 'M':a[i]=1000;
          break;
          default:printf("Invalid choice");
          break;
        }
      }
      k=a[l-1];
      for(i=l-1;i>0;i--)
      {
        if(a[i]>a[i-1])
        {
          k=k-a[i-1];
        }
        if(a[i]<=a[i-1])
        {
          k=k+a[i-1];
        }
      }
      printf("%d",k);
          
      

    return 0;
    }
  • Test Case 1

    Input (stdin)
    X
    
    
    Expected Output
    10
  • Test Case 2

    Input (stdin)
    XIV
    
    
    Expected Output
    14

1 comment:

  1. Rafiq trying to convert roman to numbers. He asks maths teacher to know about the conversion. His maths teacher explained the conversion of roman to numbers. Then Rafiq understands the conversion concepts, by using that he write a C program to do conversion.
    Valid digits are I, V, X, L, C, D, M

    ReplyDelete