Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1212
Unexpectedly, a problem occurs in Java, and the attention class cannot be identified. I don't know where the problem is. This is to find a large number to return a smaller number. The idea at the beginning was silly and naive. I calculated the number of divisor digits directly, and then took the corresponding number of digits to get the remainder. I don't know where the error is. This is the remainder operation on the number of 10 ^ K power. It is obviously not suitable here. If you want to do this, the premise is that the number must be mod-based. This question is handled in this way. First, determine if the number is 6 digits. If the number is greater than 6 digits, then, you can use the unit of a higher digit to obtain the remainder value equal to or greater than 10.
The Code is as follows:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
char num[1005];
inline int _pow( int N )
{
int res = 1;
for( int i = 0; i < N; ++i )
res *= 10;
return res;
}
int main()
{
int MOD, res, base;
while( scanf( "%s %d", num, &MOD ) == 2 )
{
res = 0;
base = 1000000 % MOD;
int len = strlen( num );
for( int i = len - 1, j = 0; i >= 0 && j < 6; --i, ++j )
{
res += ( num[i] - '0' ) * _pow( j );
}
if( len > 6 )
{
for( int i = len - 7; i >= 0; --i )
{
res += ( base * ( num[i] - '0' ) ) % MOD;
base = ( base * 10 ) % MOD;
}
}
printf( "%d\n", res % MOD );
}
return 0;
}
This is the code that I am ashamed of on the Internet. The idea is concise and the code is streamlined:
#include<stdio.h>
#include<string.h>
int main()
{
int n,sum,i,k;
char s[1001];
while(scanf("%s%d",s,&n)!=EOF)
{
k=strlen(s);
sum=0;
for(i=0;i<k;i++)
{
sum=sum*10+s[i]-'0';
sum=sum%n;
}
printf("%d\n",sum);
}
return 0;
}