Hulu written test programming-big integer addition

Source: Internet
Author: User

I took part in Hulu's test on Friday. I had a lot of questions in 70 minutes and I was not prepared enough. It took 40 minutes for the first 10 multiple choice questions, the last five blank questions had to be left blank. In this case, the time is not enough. The subjects are basic algorithms, data structures, and simple reasoning. Or its own reasons. The following algorithm questions are also very basic. Big integer addition (you can compare the big integer multiplication ). I have done it before, but I have not noticed it due to time. Sorry, here is the code written after I come back. Remind yourself to have a good review. Do not waste time.

# Include <iostream> using namespace STD; int main () {// string S1 = "3213124324131254356342652624624"; string S2 = "4"; string S1 = "9999999999999999999999999999999999999999999999 "; if (s1.length ()> s2.length () {string TMP = S1; S1 = S2; S2 = TMP;} string result (s2.length (), '0 '); int advance = 0; int I, j; for (I = s2.length ()-1, j = s1.length ()-1; j> = 0; -- I, -- J) {result [I] = (S1 [J]-'0') + (s2 [I]-'0') + advance) % 10 + '0'; advance = (S1 [J]-'0 ') + (s2 [I]-'0') + advance)/10;} while (advance & I! =-1) // considering the equal digits of the two, you must determine whether I is-1. {result [I] = (s2 [I]-'0') + advance) % 10 + '0'; advance = (s2 [I]-'0 ') + advance)/10; -- I;} if (I! =-1) {While (I! =-1) {result [I] = S2 [I]; -- I ;}} else {result. insert (result. begin (), advance + '0');} cout <result <Endl; return 0 ;}

NOTE: For the sake of simplicity, if S1 is more than S2, the two will be exchanged to ensure that S2 is always more than S2. I don't know if this is necessary, but there is additional consumption.

In addition, the result string also uses the storage space, which is also relatively lazy and requires additional space consumption.

#include <iostream>#include <string>using namespace std;string add(string left, string right){int advance = 0;string res;int i, j;for(i = left.length() - 1, j = right.length() - 1; i >= 0 && j >= 0; --i, --j){int sum = left[i] - '0' + right[j] - '0' + advance;advance = sum / 10;sum %= 10;res.insert(0, 1, '0' + sum);}if(i > 0){for(int j = i; j >= 0; --j){int sum = left[j] - '0' + advance;advance = sum / 10;sum %= 10;res.insert(0, 1, '0' + sum);}}else if(j > 0){for(int i = j; i >= 0; --i){int sum = right[i] - '0' + advance;advance = sum / 10;sum %= 10;res.insert(0, 1, '0' + sum);}}if(advance != 0)res.insert(0, 1, '0' + advance);return res;}int main(){string res;string left = "12346", right ="4";res = add(left, right);cout<<res<<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.