C Language: Large number of take the remainder

Tarsus take the remainder (array)

Encountered a problem when doing school oj today, the problem can be seen at the screenshot:

Looked through various forums, have not met the right method, the conventional method is not available to be used in the form of an array.

 

 

 

 

 Dividend over long long type, can not use conventional thinking, there would be wrong answer. It is worth noting that, with a double, and so is not a real number type, only integer types available in order remainder;

It is possible to consider the use of array operation, can define a string array char a [5000], the subscript does not play some relationship, in case, the time can be used scanf ( "% s", a) solution.

The idea is to define the individual and then an array of integers int b [5000], each of the first number of characters is converted into a digital array. It follows that the pupils do topic:

Before a first digit multiplied by 10 and then add a number after, were taking the remainder, the remainder multiplied by a number after 10 plus, in order to reciprocate.

 

 The complete code is as follows:

 1 #include <stdio.h>
 2 #include <string.h>
 3 int main()
 4 {
 5     char a[3000];
 6     int k,b[3000];
 7     while(scanf("%s%d",a,&k)!=EOF)
 8     {
 9         int l;
10         l=strlen(a);
11         int i;
12         for(i=0;i<l;i++)
13         {
14             b[i]=a[i]-'0';
15         }
16         for(i=0;i<l;i++)
17         {
18             b[i+1]=(b[i]*10+b[i+1])%k;
19         }
20         printf("%d\n",b[l-1]);
21     }
22     return 0;
23 }

 

Guess you like

Origin www.cnblogs.com/jackwang-sparrow/p/11870323.html