Exponential multiplication of an infinite number of poj 1001 exponentiation

Source: Internet
Author: User

Poj is doing very well. This question requires an infinitely large exponential multiplication result.

Basic requirements: infinitely large digits multiply

Additional requirements: ability to handle special situations-the key is to test this ability.

Therefore, the use cases in this question are particularly important, and even smart people will neglect some use cases.

The test on program robustness has reached the abnormal level.

Someone posted the token trial data address: http://poj.org/showmessage? Message_id = 76017.

With these cases, debugging will be completed in a few times.

Key examples I have missed:

000.10 1
000000 1
000.00 1
. 00000 0
000010 1

# Include <stdio. h> # include <iostream> # include <string> # include <vector> # include <algorithm> using namespace STD; bool standardizenumnodot (string & S) {While (! S. empty () & '0' = s [0]) s. erase (S. begin (); If (S. empty () S = "0"; // when n = 1, 0 bool notdot = true; For (unsigned I = 0; I <S. size () & notdot; I ++) {If ('. '= s [I]) notdot = false;} If (notdot) return true; while (! S. Empty () & '0' = s [S. Size ()-1]) S. Erase (S. End ()-1); If (! S. empty ()&&'. '= s [S. size ()-1]) s. erase (S. end ()-1); If (S. empty () S = "0"; return false;} int handledecimalpoint (string & S) {If (standardizenumnodot (s) return 0; int fraction = 0; int J = 0; For (unsigned I = 0; I <S. size (); I ++) {If (fraction> 0) fraction ++; If (s [I]! = '. ') S [J ++] = s [I]; else fraction ++;} s. erase (S. end ()-1); Return fraction-1;} string mulstr (string a, string B) {If ("0" = A | "0" = B) return "0"; int ap = handledecimalpoint (a); int BP = handledecimalpoint (B); string ans (. size () + B. size (), '0'); For (INT I =. size ()-1; I> = 0; I --) {int carry = 0; int an = A [I]-'0'; For (Int J = B. size ()-1; j> = 0; j --) {int bn = B [J]-'0 '; int sum = An * bn + carry + ans [I + J + 1]-'0'; carry = sum/10; ans [I + J + 1] = sum % 10 + '0';} If (carry) ans [I] + = carry ;} if (AP> 0 | BP> 0) ans. insert (ans. end ()-ap-BP ,'. '); standardizenumnodot (ANS); Return ans;} string spow (string S, int N) {If (S. empty () | "0" = s) Return "0"; // For program robustness, add If (0 = N) Return "1 "; if (1 = N) return s; string dividestr = spow (S, N/2); dividestr = mulstr (dividestr, dividestr); If (N % 2) dividestr = mulstr (dividestr, S); Return dividestr;} void exponentiation () {string s; int N; while (CIN >>s> N) {standardizenumnodot (s ); // cout <spow (S, N) <Endl ;}} int main () {exponentiation (); Return 0;} When n = 1 ;}

This algorithm takes 0 ms, haha.


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.