Codeforces 718A Efim and Strange Grade program analysis
jerry's program
using namespace std; typedef long long ll; string buf; int i; void up(int at) { at--; if (at < 0) { buf = '1' + buf; i++; return; } if (buf[at] == '.') at--; buf[at]++; if (buf[at] == '9'+1) { buf[at] = '0'; up(at); } } int main() { cin.sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k >> buf; while (buf[i] != '.') i++; i++; while (i < buf.length() && buf[i] <= '4') i++; if (i == buf.length()) { cout << buf << endl; return 0; } for (;buf[i] != '.' && k;i--) if (buf[i] >= '5') { buf.resize(i); up(i); k--; } if (buf.back() == '.') buf.pop_back(); cout << buf << endl; return 0; }
First, find the first decimal point followed by 5 or more digits, if not, output the original number directly.
From that point, from the forward to the decimal point, if the current digit is greater than or equal to 5 and carry the chance, first change the ending of this number to this position, because all the numbers after that will be cleared, then call the up function carry. If 9 of the numbers continue recursion, quit, if the integer part of the first place is still in position, you need to add 1 to the front.
Finally, note that the decimal point of the last character is to be deleted.
Summary: use a function to carry out carry. If there is a carry opportunity, just continue to call the function, which is very convenient to handle.
izrak's program
#include <bits/stdc++.h> #define FO(i,a,b) for (int i = (a); i < (b); i++) #define sz(v) int(v.size()) using namespace std; char s[200005]; int main() { int l, t; scanf("%d %d", &l, &t); scanf("%s", s+1); s[0] = '0'; for (int i = 0; i <= l; i++) { if (s[i] == '.') { for (int j = i+1; j <= l; j++) if (s[j] >= '5') { s[j] = 0; bool roundup = true; t--; for (int k = j-1; k > i; k--) { // s[k]++ if (roundup) { s[k]++; if (s[k] >= '5' && t) { s[k] = 0; t--; roundup = true; } else roundup = false; } } if (roundup) { s[i] = 0; for (int k = i-1; k >= 0; k--) { s[k]++; if (s[k] <= '9') break; else s[k] = '0'; } } break; } } } if (s[0] != '0') { printf("%s\n", s); } else { printf("%s\n", s+1); } }
When this program reads in, it adds a leading zero before the number to deal with the first carry of the integer part.
The first layer circulates to find the position of decimal point, the second layer circulates to find the number with the first digit greater than or equal to 5 after the decimal point, the third layer circulates (the maximum carry to the decimal point), and the roundup records whether to continue carry or not. If there is a number greater than or equal to 5 after the carry and there is a carry opportunity, carry on. If carry still needs to be continued after the end of the third layer loop, carry in the integer part. In carry, set this bit to '\ 0' for each carry, so that the output will only end at the first '\ 0' position.
Note that the final judgment is that if the leading zero added at the beginning is still zero, it cannot be output.
Summary: it is unnecessary to carry on the maintenance with the bool variable round up, and the position of the entered bit is marked as' \ 0 '. If the carry is directly processed in a circular way before the decimal point, it is not easy to make mistakes.