The idea is to store the data in strings. One-digit calculation is the same as the vertical method of manual calculation.
1. stored in a static variable, such as char STR [] = "123456789" for the 987654321 large number ";
2. I first wrote a class. I said I was very embarrassed because the speed was very slow. Later I wrote a function. The speed was still slow. It took 80 seconds to run 2 to the power of 100000, then I thought of a simplified method and it could finally run in 20 seconds.
.
A. in the past, the train of thought was to read a single character (0-9) and convert it to an integer by bitwise multiplication, and then convert it to a character (0-9) after completion ), this requires a lot of if logic judgment.
B. the current idea is: read a single character (0-9) by bitwise multiplication, and subtract '0' to be the corresponding integer. After calculation, when '0' is added, it is the corresponding character, eliminating the need for judgment.
3. Optimized Code
# Include <iostream> <br/> # include <fstream> <br/> # include <string. h> <br/> using namespace STD; <br/> # define digits 100000 <br/> static char strnum [digits]; <br/> static int ndigits = 0; <br/> void showarray () <br/> {<br/> for (INT I = 0; I <ndigits; I ++) <br/>{< br/> cout <strnum [ndigits-I-1]; <br/>}< br/> cout <Endl; <br/>}< br/> void output () <br/>{< br/> fstream fout ("power. log ", IOS: Out); </P> <P> for (INT I = 0; I <ndigits; I ++) <br/>{< br/> fout <strnum [ndigits-I-1]; <br/> If (I + 1) % 100 = 0) <br/>{< br/> fout <'/N '; <br/>}< br/> void power (INT exp) <br/>{< br/> ndigits = 0; <br/> strnum [ndigits] = '1'; <br/> ndigits ++; </P> <p> for (INT I = 0; I <exp; I ++) <br/> {// always multiply by 2 <br/> int n = 0; <br/> for (INT I = 0; I <ndigits; I ++) <br/> {// multiply each person by 2 <br/> int M = (strnum [I]-'0 ')* 2 + N; <br/> strnum [I] = '0' + (M % 10); <br/> N = m/10; <br/>}< br/> If (n> 0) <br/>{< br/> strnum [ndigits] = '0' + N; <br/> ndigits ++; <br/>}< br/> int main () <br/> {<br/> int number = 0; <br/> cout <"the base is 2, please enter the power :"; <br/> CIN> Number; <br/> while (number! = 0) <br/>{< br/> power (number); <br/> cout <"2 exp {" <number <"} = "; <br/> showarray (); <br/> cout <"--- digits:" <ndigits <Endl; <br/> output (); <br/> cout <"Please enter the power:"; <br/> CIN> Number; <br/>}< br/> return 0; <br/>}< br/>