关注
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <map>
//#include <set>
#include <utility>
#include <iterator>
#include <array>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <climits>
#include <cstring>
#include <unordered_map>
#include <functional>
#include <iomanip>
#include <cmath>
using namespace std;
string addBinary(string a, string b) {
if (a.size() < b.size()) {
swap(a, b);
}
int lena = a.size();
int lenb = b.size();
if (lenb == 0) {
return a;
}
string ret;
unsigned short c = 0;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int i = 0, j = 0;
for (; i < lena && j < lenb; ) {
char cha = a[i], chb = b[j];
unsigned short sum = cha - '0' + chb - '0' + c;
c = sum / 2;
ret.push_back(sum % 2 + '0');
++i, ++j;
}
int k = i;
for (; k < lena; ++k) {
unsigned short sum = a[k] - '0' + c;
c = sum / 2;
ret.push_back(sum % 2 + '0');
}
if (c == 1) {
ret.push_back('1');
}
reverse(ret.begin(), ret.end());
return ret;
}
int cal(int n){
return pow(2, n - 1);
}
int main(int argc, char *argv[])
{
string a = "111";
string b = "1";
auto ret = addBinary(a,b);
freopen("input.txt", "r", stdin);
unsigned int n = 0;
while (cin >> n) {
stringstream sstrm;
sstrm << n;
string str;
sstrm >> str;
int sz = str.size();
string ss(sz, '1');
sstrm.str("");
sstrm.clear();
sstrm.str(ss);
int num = 0;
sstrm >> num;
int ret = 0;
if (n >= num) {
for (int i = 1; i <= sz; ++i) {
ret += cal(i);
}
} else {
for (int i = 1; i <= sz - 1; ++i) {
ret += cal(i);
}
string basestr(sz,'0');
basestr[0] = '1';
int tempnum = stoi(basestr);
while (tempnum <= n) {
++ret;
basestr = addBinary(basestr,"1");
tempnum = stoi(basestr);
}
}
cout << ret << endl;
}
return 0;
}
第一道覆盖了,没存下来
查看原帖
点赞 2
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 面试被问“你的缺点是什么?”怎么答 #
5998次浏览 95人参与
# 租房找室友 #
7941次浏览 53人参与
# 水滴春招 #
15553次浏览 192人参与
# 读研or工作,哪个性价比更高? #
26102次浏览 356人参与
# 文科生还参加今年的春招吗 #
4072次浏览 31人参与
# 职场新人生存指南 #
199060次浏览 5504人参与
# 简历无回复,你会继续海投还是优化再投? #
48570次浏览 560人参与
# 如果重来一次你还会读研吗 #
155692次浏览 1705人参与
# 机械人选offer,最看重什么? #
69068次浏览 449人参与
# 听劝,这个简历怎么改 #
14024次浏览 180人参与
# 参加完秋招的机械人,还参加春招吗? #
26835次浏览 276人参与
# 选择和努力,哪个更重要? #
44188次浏览 492人参与
# 入职第四天,心情怎么样 #
11169次浏览 62人参与
# 如果再来一次,你还会学硬件吗 #
103614次浏览 1245人参与
# 22届毕业,是读研还是拿外包offer先苟着 #
4650次浏览 27人参与
# 招聘要求与实际实习内容不符怎么办 #
46470次浏览 493人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
20503次浏览 412人参与
# 软开人,你觉得应届生多少薪资才算合理? #
81363次浏览 496人参与
# 你们的毕业论文什么进度了 #
900974次浏览 8956人参与
# 长光卫星求职进展汇总 #
27821次浏览 186人参与
# 互联网行业现在还值得去吗 #
2365次浏览 22人参与