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