题解(我知道不需要高精度,但是我忍不住)
2023-08-17 16:13:39
发布于:浙江
5阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
bool isSmaller(string str1, string str2) { //判断a是否小于b
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2)
returnrn true;
if (n2 < n1)
return false;
for (int i = 0; i < n1; i++){
if (str1[i] < str2[i])
return true;
if (str1[i] > str2[i])
return false;
}
return false;
}
string add(string a ,string b){ //高精度加法
int carry = 0 ,i = a.size() - 1 ,j = b.size() - 1;
string result;
while (i >= 0 || j >= 0 || carry){
int digit1 = (i >= 0) ? (a[i] - '0') : 0;
int digit2 = (j >= 0) ? (b[j] - '0') : 0;
int sum = digit1 + digit2 + carry;
carry = sum / 10;
sum %= 10;
result += (sum + '0');
if (i >= 0)
i--;
if (j >= 0)
j--;
}
reverse(result.begin(), result.end());
return result;
}
int main(){
string n ,a = "0",b = "1";
long long count = -1;
cin >> n;
while (!isSmaller(n ,a)){ //如果a大于等于b
a = add(a ,b);
b = add(b ,b); //等价于b * 2
count++;
}
cout << count;
cout << "\n1\n"; //输出
b = "1";
for (long long i = 1 ; i < count ; i++){
b = add(b ,b);
cout << b << "\n";
}
}
这里空空如也
有帮助,赞一个