Description
给你一个二进制数,将其翻转后与原二进制数相乘,并按下列所述格式将乘积也按二进制输出。
Input
输入有多组测试数据(不超过1000组),每组数据只有一个二进制数并且占一行,每个二进制数长度不超过30。
Output
每组数据按照样例格式输出一行。
Sample Input
11101011000
Sample Output
1110*111=1100010101*101=110011000*1=1000
注意输入为0的情况, 输出去掉前导0, 用 long long 就可以了。
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
long long String2LongLong(string s){
long long elem = 0;
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); ++i){
elem += (s[i] - '0') * pow(2.0, i * 1.0);
}
return elem;
}
string LongLong2String(long long n){
string s;
for (long long i = n; i; i = i / 2){
s += i % 2 ? '1' : '0';
}
reverse(s.begin(), s.end());
return s;
}
void Del(string &s){
int i = 0;
while (s[i] == '0'){
++i;
}
s.erase(0, i);
}
int main(){
string s;
while (cin >> s){
int Cnt_0 = count(s.begin(), s.end(), '0');
if (Cnt_0 == s.size()){
cout << "0*0=0" << endl;
}
else{
string temp = s;
reverse(temp.begin(), temp.end());
long long n1 = String2LongLong(s);
long long n2 = String2LongLong(temp);
long long n = n1 * n2;
string str = LongLong2String(n);
Del(s);
Del(temp);
cout << s << "*" << temp << "=" << str << endl;
}
}
return 0;
}