2 条题解
-
0
bitset方法:
#include <bits/stdc++.h> using namespace std; /* 题意: 输入 t 个整数(1 ≤ x ≤ 1e8),每个输出它的二进制表示(无前导零)。 思路: - 每个整数 x 转换为 bitset<32>(因为 1e8 < 2^27 < 2^32) - bitset<32> 的 to_string() 会输出 32 位的字符串(有前导 0) - 去掉最前面的所有 '0',保留从第一个 '1' 开始的子串 */ int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { unsigned int x; cin >> x; // 转换为 32 位二进制字符串 bitset<32> bits(x); string s = bits.to_string(); // 去除前导零:找到第一个 '1' 的位置 size_t pos = s.find('1'); if (pos == string::npos) // 特殊情况:x=0(不过题目保证x≥1) cout << "0\n"; else cout << s.substr(pos) << "\n"; } return 0; } -
0
普通方法:
#include <bits/stdc++.h> using namespace std; int x,t,yushu,a[1000],k; int main(){ cin>>t; for(int i=1;i<=t;i++){ cin>>x; for(int j=0;j<1000;j++) a[j]=0; k=0; yushu=0; while(x>0){ yushu=x%2; x/=2; a[k++]=yushu; } for(int i=k-1;i>=0;i--){ cout<<a[i]; } cout<<endl; } return 0; }
- 1
信息
- ID
- 192
- 时间
- 1000ms
- 内存
- 64MiB
- 难度
- 4
- 标签
- 递交数
- 21
- 已通过
- 15
- 上传者