Translate

Sunday 1 February 2015

NSYSTEM(Numeral System) spoj problem solution

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
HEY this is easy problem simply follow the rule.........
keep patient..........this may take some time.......
i give 45 minute in it......
then i got AC in 1st attempt.....



MY AC C++ SOLUTION IS:::::::::



#include<bits/stdc++.h>
using namespace std;
int main(){
 int t;
 scanf("%d\n",&t);
 while(t--)
 {
  char name1[1000],name2[1000];
  scanf("%s %s",name1,name2);
 // printf("%s %s\n",name1,name2);
  int ans1=0,ans2=0,len1,len2,i;
  len1=strlen(name1);
  len2=strlen(name2);
  for(i=0;i<len1;i++)
  {
   if(name1[i]>=48 && name1[i]<=57)
   {
    if(name1[i+1]=='m')
    ans1+=1000*(name1[i]-'0');
    if(name1[i+1]=='c')
    ans1+=100*(name1[i]-'0');
    if(name1[i+1]=='x')
    ans1+=10*(name1[i]-'0');
    if(name1[i+1]=='i')
    ans1+=1*(name1[i]-'0');
    i++;
   }
   else
   {
    if(name1[i]=='m')
    ans1+=1000;
    if(name1[i]=='c')
    ans1+=100;
    if(name1[i]=='x')
    ans1+=10;
    if(name1[i]=='i')
    ans1+=1;
   }
  }
  
   for(i=0;i<len2;i++)
  {
   if(name2[i]>=48 && name2[i]<=57)
   {
    if(name2[i+1]=='m')
    ans2+=1000*(name2[i]-'0');
    if(name2[i+1]=='c')
    ans2+=100*(name2[i]-'0');
    if(name2[i+1]=='x')
    ans2+=10*(name2[i]-'0');
    if(name2[i+1]=='i')
    ans2+=1*(name2[i]-'0');
    i++;
   }
   else
   {
    if(name2[i]=='m')
    ans2+=1000;
    if(name2[i]=='c')
    ans2+=100;
    if(name2[i]=='x')
    ans2+=10;
    if(name2[i]=='i')
    ans2+=1;
   }
  }
  int sum=ans1+ans2;
 // printf("%d %d =%d\n",ans1,ans2,sum);
  
  char arr[10];
  int count=0;
  while(sum)
  {
   arr[count++]=(sum%10)+'0';
   sum/=10;
  }
 /* for(i=count-1;i>=0;i--)
  {
   printf("%c ",arr[i]);
  }*/
  int p=0;
  char str[1000];
  for(i=count-1;i>=0;i--)
  {
   if(arr[i]!='0' && arr[i]!='1')
   {
    str[p++]=arr[i];
   }
   if(i==3 && arr[i]!='0')
   str[p++]='m';
   if(i==2 && arr[i]!='0')
   str[p++]='c';
   if(i==1 && arr[i]!='0')
   str[p++]='x';
   if(i==0 && arr[i]!='0') 
   str[p++]='i';
  }
  str[p]='\0';
  printf("%s\n",str);
 }
 return 0;
}

1 comment:

Unknown said...

http://ideone.com/F4rcVm
This is my solution to the same problem. I just don't know why am I getting a wrong answer. I tested many cases and have got right answers but the spoj judge gives a wrong answer. Please help

Working With Java Collections