"S1" 1016. High Precision Division

Source: Internet
Author: User

The reason I wrote this is because my classmates generally said my code was a little short, and I was shocked when I looked at my code for the second time. = =| |

The core idea is not different

The key is that the for loop and while nesting save a lot of judgment length.

The most important factor in the simplicity of the code is the algorithm, which may increase the length of the code.

My logic here is:

When the first comparison, if less than, direct break.

From the second start, use the number of the selected length to subtract from the same number of digits.

If the first digit of the selected number is equal to zero, and cannot be subtracted, shift downward.

Use Tempint to count the values of each bit and put them into the final result

(。??) Dentetsu

1#include <iostream>2#include <string>3 using namespacestd;4 5 intMainintargcChar*argv[]) {6     stringA, B, substring;7     intI, J, K;8     intTempint;9     intNumber =0;Ten     intSize_a =0, Size_b =0;  One     intresult[ -] = {0}; ACin>>a>>b; -      -Size_a =a.size (); theSize_b =b.size (); -First, starting from high to low, calculate the result -      for(i =0; I <= Size_a-size_b; ++i) { -substring = a.substr (i, i +size_b); +Count the values of each bit, number<=9 -Number =0; +          while(true){ A             if(i = =0&& substring < b) Break;//special case for the first time at             if(I! =0&& substring < b && a[i-1] =='0') Break;//second cycle and subsequent stop conditions -              for(j = i + Size_b-1, k = Size_b-1; J >= I; --j,--k) { -Tempint = A[j]-B[k]; -                 if(Tempint <0) { -A[j-1] -=1; -Tempint + =Ten; in                 } -A[J] = Tempint +'0'; to }//above for loop to calculate each bit +++Number ; -substring = a.substr (i, i +size_b); Refresh the         } *Result[i] =Number ; $     }Panax Notoginseng     if(result[0]!=0){ -cout << result[0]; the     } +      for(i =1; I <= Size_a-size_b; ++i) { AOutput thecout <<Result[i]; +     } -     return 0; $}

"S1" 1016. High Precision Division

Related Article

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.