1203. The Cubic End

来源:互联网 发布:c编程教学免费视频 编辑:程序博客网 时间:2024/06/05 08:07
#include <iostream>#include <vector>#include <cstring>using namespace std;string ending;vector<int> ans;void print() {bool firstnotzero = false;for(int i = ans.size() - 1; i >= 0; i--) {if(ans[i]) {firstnotzero = true;}if(firstnotzero) {cout << ans[i];}else if(i == 0) {cout << 0;}}cout << endl;}void find() {int len = ending.length();int ansBits = 0;ans.resize(len);int carry = 0;while(len - 1 - ansBits >= 0) {for(ans[ansBits] = 0; ans[ansBits] <= 9; ans[ansBits]++) {int resultBit = carry;for(int i = 0; i <= ansBits; i++) {for(int j = 0; j <= ansBits - i; j++) {int k = ansBits - i - j;resultBit += ans[i] * ans[j] * ans[k];}}if(resultBit % 10 == ending[len - 1 - ansBits] - '0') {carry = resultBit / 10;break;}    }    ansBits++;}}int main() {    int cases;    cin >> cases;    while(cases--) {    cin >> ending;    ans.clear();    find();    print();    }    return 0;}

0 0
原创粉丝点击