#include <iostream> #include <string> #include <cmath> #include <vector> using namespace std; string getKthNum(long long k) {     if (k == 1) {         return "4";     } else if (k == 2) {         return "7";     }     int numPos = ceil(log(k + 2.0) / log(2.0) - 1);     long long prevNum = pow(2.0, numPos) - 2;     long long nowNum = k - prevNum;     long long halfNum = pow(2.0, numPos - 1);     string postStr = getKthNum((nowNum > halfNum ? nowNum % halfNum : nowNum) + halfNum - 2);     string prevStr = (nowNum > halfNum) ? "7" : "4";     return prevStr + postStr; } int main() {     int n;     cin >> n;     vector<string> retVec;     while (n--) {         long long k;         cin >> k;                  string ret = getKthNum(k);         retVec.push_back(ret);     }     for (int i = 0; i < retVec.size(); i++) {         cout << retVec[i] << endl;     } return 0; } 从第k个数倒推有多少位,在用递归解
点赞 2

相关推荐

溱元:前端每年固定死几次,看两集广告就复活了
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务