关注
#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
相关推荐
牛客热帖
更多
正在热议
更多
# AI让海力士市值突破9000亿美元 #
394次浏览 7人参与
# 百度工作体验 #
335830次浏览 2284人参与
# 在爱玛,骑向未来 #
46612次浏览 442人参与
# 打工人的精神状态 #
155392次浏览 1573人参与
# 职场新人体验 #
193463次浏览 1256人参与
# 百度求职进展汇总 #
732003次浏览 6451人参与
# 工作后,你落下了哪些病根 #
42738次浏览 302人参与
# 机械人,说说你的烦心事 #
149370次浏览 1176人参与
# 简历当中有水分算不算造假? #
176893次浏览 2331人参与
# 机械人,你最希望上岸的公司是? #
218466次浏览 1946人参与
# 你收到了哪些公司的笔试? #
69725次浏览 446人参与
# 米哈游求职进展汇总 #
692308次浏览 3364人参与
# 毕业季,给职场新人一些建议 #
222016次浏览 2610人参与
# 工作压力大,你会干什么? #
83119次浏览 717人参与
# 机械/制造每日一题 #
98132次浏览 1503人参与
# 为了求职,我做过的疯狂伪装 #
88712次浏览 778人参与
# 如果秋招能重来,我会____ #
105763次浏览 514人参与
# 你以为的实习VS真实的实习 #
143062次浏览 758人参与
# 什么专业适合考公 #
70053次浏览 362人参与
# 重来一次,我还会选择这个专业吗 #
468097次浏览 3988人参与
# 应届生第一份工作最好去大厂吗? #
150784次浏览 1230人参与
查看18道真题和解析