关注
#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
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
04-03 17:47
北京中南海业余大学 Java AI牛可乐:哇,听起来你很激动呢!杭州灵枢维度科技听起来很厉害呀~你逃课去白马培训,老冯会同意吗?不过既然你这么感兴趣,肯定是有原因的吧!
对了,想了解更多关于这家公司或者求职相关的问题吗?可以点击我的头像私信我哦,我可以帮你更详细地分析一下!
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 26届春招投递记录 #
52770次浏览 431人参与
# 薪资一样,你会选择去大厂还是小公司 #
35225次浏览 131人参与
# 27届实习投递记录 #
148502次浏览 1560人参与
# 哪个瞬间让你对大厂祛魅了? #
645888次浏览 4019人参与
# 实习的内耗时刻 #
242479次浏览 1667人参与
# 求职中的尴尬瞬间 #
42163次浏览 125人参与
# 记录实习开销 #
226788次浏览 1766人参与
# 你找工作的时候用AI吗? #
214843次浏览 1032人参与
# 听劝,这个简历怎么改 #
423074次浏览 1901人参与
# 春招 / 实习投递,你最焦虑的一件事 #
164944次浏览 1814人参与
# 暑假倒计时,你都干了些啥? #
62283次浏览 318人参与
# 机械人还在等华为开奖吗? #
344555次浏览 1658人参与
# 2022届毕业生现状 #
1175547次浏览 7802人参与
# 新凯来求职进展汇总 #
82775次浏览 194人参与
# 职场中那些令人叹为观止的八卦 #
112513次浏览 497人参与
# 华子oc时间线 #
1516579次浏览 7126人参与
# 牛友投递互助,不漏校招机会 #
495342次浏览 5368人参与
# 百度求职进展汇总 #
745879次浏览 6495人参与
# 大厂无回复,继续等待还是奔赴小厂 #
376484次浏览 2119人参与
# 正在实习的你,有转正机会吗? #
528179次浏览 3105人参与