Translate

Sunday, 18 January 2015

spoj PUCMM025 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
hii guys this is logic for spoj Divisor Digits problem.............
1.every no is divisble by 1
2.Any no is divisible by 2 if its last digit is divisible by 2
3.Any no is divisible by 3 if its sum of digit is divisible by 3
4.Any no is divisible by 4 if number formed by its last two digits is divisible by 4
5.Any no is divisible by 5 if its last digit is divisible by 5  i.e its last digit either 0 or 5
6.Any no is divisible by 6 if the no is divisible by 2 and 3
8.Any no is divisible by 8 if number formed by last three digit is divisible by 8
9.Any no is divisible by 9 if sum of digits is divisible by 9

The only issue is how to check for 7...
so there is a very good concept for 7........
I am declareing an array
int a[]={1,3,2,-1,-3,-2};
letsay i want to check for str[]="73089089";
its len=8;
start from Rightmost digit upto leftmost digit
and just find out the sum as follows.......
int sum=0,k=0;
for(int i=len-1;i>=0;i--)
{
sum+=(str[i]-' 0')*(a[k]);
k=(k+1)%6;
}
after this you have to check
if(sum%7==0)
then number is divisible by 7
else
not


I HOPE YOU GOT THE LOGIC............







my AC solution is.........

#include <cstdio>
#include <cstring>

int main()
{
 int len,count,sum,num,num2,i;
 int array[] = {1,3,2,-1,-3,-2};
 int yes;
 char str[245],chr;
 while(scanf("%s",str) != EOF)
 {
  len = strlen(str);
  for(i = 0,count = 0,sum = 0,num = 0,num2 = 0,yes = 0; i < len; i++)
  {
   chr = str[i];
   if(chr == '0')
   {
    continue;
   }
   else if(chr == '1')
   {
    count++;
   }
   else if(chr == '2')
   {
    if((str[len-1]-48)%2 == 0)
     count++;
   }
   else if(chr == '3')
   {
    if(sum == 0)
    {
     for(int j = 0; j < len; j++)
     {
      sum += (str[j] - 48);
     }
    }
    if(sum % 3 == 0)
     count ++;
   }
   else if(chr == '4')
   {
    if(num == 0)
    {
     num = 10*(str[len-2] - 48) + (str[len-1] - 48);
    }
    if(num%4 == 0)
     count++;
   }
   else if(chr == '5')
   {
    if((str[len-1]-48)%5 == 0)
     count++;
   }
   else if(chr == '6')
   {
    if(sum == 0)
    {
     for(int j = 0; j < len; j++)
     {
      sum += (str[j] - 48);
     }
    }
    if(sum%3 == 0 && (str[len-1]-48)%2 == 0)
     count++;
   }
   else if(chr == '7')
   {
    if(yes == 1) count++;
    else if(yes == -1) continue;
    else
    {
     int numk,k = 0,nsum = 0;
     for(int j = len-1; j >=0 ; j--)
     {
      numk = str[j] - 48;
      nsum += array[k]*numk;
      k = (k+1)%6;
     }
     if(nsum% 7 == 0)
     {
      count++;
      yes = 1;
     }
     else yes = -1;
    }
   }
   else if(chr == '8')
   {
    if(num2 == 0)
    {
     num2 = 100*(str[len-3]-48) + 10*(str[len-2]-48) + str[len-1]-48;
    }
    if(num2%8 == 0)
     count++;
   }
   else if(chr == '9')
   {
    if(sum == 0)
    {
     for(int j = 0; j < len; j++)
     {
      sum += (str[j] - 48);
     }
    }
    if(sum % 9 == 0)
     count++;
   }
  }
  printf("%d\n",count);
 }
}

No comments:

Working With Java Collections