关注
#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
相关推荐
点赞 评论 收藏
分享
查看21道真题和解析 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 有哪些公司在面试时考察AICoding? #
9302次浏览 192人参与
# 你认为小厂实习有用吗? #
137613次浏览 745人参与
# 厦门银行科技岗值不值得投 #
23962次浏览 506人参与
# HR面都在聊什么? #
7469次浏览 89人参与
# 如何快速融入团队? #
47967次浏览 299人参与
# 父母问你工作找得怎么样,怎么回 #
15723次浏览 194人参与
# AI“智障”时刻 #
35035次浏览 155人参与
# 找工作如何保持松弛感? #
136989次浏览 1469人参与
# AI时代还有必要刷leetcode吗? #
6046次浏览 99人参与
# 想从事Agent应该学习哪些技术? #
4125次浏览 133人参与
# 那些我实习了才知道的事 #
286815次浏览 1810人参与
# 什么人最适合大厂? #
8206次浏览 82人参与
# 哪些公司面试还在问八股? #
8394次浏览 84人参与
# 从投递到OC,你用了多久 #
14917次浏览 133人参与
# 顺丰求职进展汇总 #
84429次浏览 362人参与
# 查收我的offer竞争力报告 #
292616次浏览 1738人参与
# 如果没找到工作,考公是你的退路吗 #
70589次浏览 458人参与
# 双非本科的出路是什么? #
227684次浏览 1651人参与
# 哪一刻你突然觉得实习“有点值了” #
8943次浏览 71人参与
# 我的求职进度条 #
1035402次浏览 7106人参与
# 秋招踩过的“雷”,希望你别再踩 #
196234次浏览 1752人参与