HDU-1212 big number Java is faulty again

Source: Internet
Author: User

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;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.