很水,但是需要练速度反映。。。15分钟全部写对!!!!!!!!!!!!
#include <assert.h> #include <algorithm> #include <vector> using namespace std; class Decimal { public: int integer; bool recycle; vector<int> decimal; }; void toDecimal(int dividend, int divisor, Decimal& decimal) { decimal.integer = dividend / divisor; int remainder = dividend % divisor, recycle = 0, end = 0, curbit = 0; while (remainder) { int cur = remainder*10/divisor; decimal.decimal.push_back(cur); if (recycle || curbit > 0) recycle = decimal.decimal[recycle] == cur ? recycle + 1 : 0; if (curbit > 0 && recycle == end) { decimal.decimal.resize(recycle); decimal.recycle = true; return; } ++curbit; if (!recycle) end = curbit; remainder = (remainder * 10) % divisor; } decimal.recycle = false; } int main() { Decimal dec; toDecimal(12,8,dec); return 0; }
时间: 2024-10-08 11:03:37