The addition of large numbers, multiplication, the realization of the whole arrangement

Source: Internet
Author: User

Print 1 to the maximum number of n digits:

Here is a very important concept is n-digit, do not know how big N, then you need to use a character to represent a large number


Here is a string to represent the large number, in order to print the time convenient, here need to simulate a character addition operation

In addition, the multiplication of the previously large number of implementations is similar:

http://blog.csdn.net/xietingcandice/article/details/44729323

#include <iostream> #include <string>using namespace Std;int incrument (string& num,int n) {int length = Num.length (); bool Overflag = False;int Nextflag = 0;//< flag carry symbol for (int i = length-1; i>=0;i--) {int nSum = num[i]-' 0 ' + Nextflag;if (i = = length-1) {nsum++;//< last one plus 1}if (NSum <) {Num[i] = nsum+ ' 0 '; break;} if (NSum >=) {if (i = = 0 && Length = = N)//< marked with the highest bit carry, the logo needs to exit the program {overflag = True;break;} Num[i] = nsum-10+ ' 0 '; nextflag = 1;if (i = = 0)//< If the minimum bit carry, then the string needs to be extended, if there is no string using char then in the output there is a certain action {num = ' 1 ' +num;}} return Overflag;} void Printnum (String num) {for (int i = 0; i < num.length (); i++) {cout << num[i];} cout << Endl;} void Printmax (int n) {string num = "0";//< output data if (n = = 0) {return;} while (! Incrument (Num,n)) {printnum (num);}} int main () {Printmax (4); return 0;}


Because the title is only needed to print out the data, it can also be used to directly take advantage of the full array of digital features:

The whole arrangement, basically is a loop plus the process of traversal


#include <iostream> #include <string>using namespace std;void printnum (char* num,int n) {int flag = True;for ( int i =  0; I <n; i++) {if (flag) {if (num[i] = = ' 0 ') {continue;} Else{cout<<num[i];flag = false;}} Else{cout<<num[i];}} if (!flag) {Cout<<endl;}} void printrecursively (char* num, int n, int index) {if (index = = 0) {printnum (num,n); return;} for (int i = 0; i < i++) {num[index-1] = i+ ' 0 '; Printrecursively (num,n,index-1);}} void Printmax (int n) {char *num = new Char[n];if (n = = 0) {return;} for (int i = 0; i < i++) {num[n-1] = i+ ' 0 '; Printrecursively (num,n,n-1);} delete []num;} int main () {Printmax (2); return 0;}


The addition of large numbers, multiplication, the realization of the whole arrangement

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.