Code First Fractional Group: 16th array representation number, plus one operation
Given a number represented as an array of digits, plus one to the number.
#include <iostream>#include<stdlib.h>#include<stdio.h>#include<vector>using namespacestd;int* AddOne (int*a,intN) { int*ans; for(inti = n1; I >=0; i--) { if(a[i]!=9) {A[i]++; Break; } Else{A[i]=0; } } if(a[0]==0) {ans=(int*)malloc(sizeof(int) * (n+1)); for(inti = n; i >0; i--) {Ans[i]=a[i-1]; } ans[0]=1; returnans; } Else { returnA; }}vector<int> PlusOne (vector<int> &digits) { inti; for(i = digits.size ()-1; I >=0;--i) { if(Digits[i]! =9) {Digits[i]++; Break; } Else{Digits[i]=0; } } if(digits[0]==0) {Digits.insert (Digits.begin (),1); } returndigits;}intMain () {inta[4]={9,9,9,9}; intlen=0; if(a[3]==9) {len=sizeof(a)/sizeof(int)+1; } Else{len=sizeof(a)/sizeof(int); } int*b = AddOne (A,4); for(inti =0; i < Len; i++) {cout<<B[i]; } cout<<Endl; Vector<int>result; Vector<int> array = {1,9,9}; Result=PlusOne (array); intn =result.size (); for(inti =0; I < n;i++) {cout<<Result[i]; } return 0;}
Code First Fractional Group: 16th array representation number, plus one operation