Peking University 1047 questions

Source: Internet
Author: User

Question link: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1047

 

Difficulties:

1. How to handle the final result of the big integer multiplication with leading 0

2. How to determine whether the numbers in the result are the same as those in the original number?

Solution:

1. A single M-digit is multiplied by an N-digit. The result is a maximum of M + N bits and a minimum m + n-1 bits. According to the restrictions in this question, assume that op with leading 0 is M bit, and the other multiplier is 1 or 2 bits and the maximum is 60, therefore, the possible digits of the result are m, m + 1, and m + 2. If m + 1 and M + 2 are not 0, the result is M, if one of the m + 1 and M + 2 digits is not 0, it is determined that this number is not a cyclic Number.

2. In order to determine whether the numbers in the result are the same as those in the original number, two integer arrays are used to calculate the occurrence frequency of each number in the two numbers, then, the standard library algorithm is called to compare the two arrays to determine whether the content is equal.

 

# Include <iostream>
# Include <string>
# Include <sstream>
# Include <algorithm>
Using namespace STD;

# Define Max 61

Int main ()
{
Freopen ("in.txt", "r", stdin );
Int in1 [10], in2 [10];
Char OP1 [Max], OP2 [3], Res [Max + 2];
Int I, J, K, len1, len2, Len;
Stringstream SS;
Bool yes;
While (CIN> OP1)
{
Yes = true;
I = 0;
Memset (in1, 0, sizeof (in1 ));
While (OP1 [I]! = '/0 ')
{
++ In1 [OP1 [I]-'0'];
++ I;
}

Len1 = strlen (OP1 );
Reverse (OP1, OP1 + len1 );

For (I = 1; I <= len1; ++ I)
{
SS <I;
SS> OP2;
SS. Clear ();
Len2 = strlen (OP2 );
Reverse (OP2, OP2 + len2 );
Memset (Res, 0, sizeof (RES ));

For (j = 0; j <len1; ++ J)
{
For (k = 0; k <len2; ++ K)
{
Res [J + k] + = (OP1 [J]-'0') * (OP2 [k]-'0 ');
If (RES [J + k]> 9)
{
Res [J + k + 1] + = res [J + k]/10;
Res [J + k] % = 10;
}
}
}


If (RES [len1]! = 0 | res [len1 + 1]! = 0)
{
Reverse (OP1, OP1 + len1 );
Cout <OP1 <"is not cyclic" <Endl;
Yes = false;
Break;
}
Else
Len = len1;

Memset (in2, 0, sizeof (in2 ));
For (j = 0; j <Len; ++ J)
++ In2 [res [J];

If (! Equal (in1, in1 + 10, in2 ))
{
Reverse (OP1, OP1 + len1 );
Cout <OP1 <"is not cyclic" <Endl;
Yes = false;
Break;
}
}

If (yes)
{
Reverse (OP1, OP1 + len1 );
Cout <OP1 <"is cyclic" <Endl;
}
}
Return 0;
}

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.