二进制转十进制
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string a;
cin >> a;
reverse(a.begin(), a.end());
int sum = 0, t = 1;
for (int i = 0; a[i]; i++) sum += t * (a[i] - '0'), t = t * 2;
cout << sum;
return 0;
}
十进制转二进制
#include <iostream>
#include <cmath>
using namespace std;
int n, a[100000], i = 0;
int main() {
cin >> n;
while (n > 0) {
i++;
a[i] = n % 2;
n /= 2;
}
for (int j = i; j >= 1; j--) cout << a[j];
return 0;
}
共 2 条回复
谢谢大李老师
一进制转十进制
/手动调皮