Đăng nhập để hỏi chi tiết
Giúp với C+++++++++++
Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
Cách 1: $\\$
Thời gian: O(n log n)
Bộ nhớ: O(1)$\\$
#include <bits/stdc++.h>
using namespace std;
int main() {
int T; cin >> T;
while (T--) {
int n; cin >> n;
for (int i = 1; i <= n; i++) {
string s = bitset<14>(i).to_string();
cout << s.erase(0, s.find('1')) << " ";
}
cout << endl;
}
}
$\\\\$
Cách 2:
Thời gian: O(n log n)
Bộ nhớ: O(1)$\\$
#include <bits/stdc++.h>
using namespace std;
string toBinary(int x) {
string res = "";
while (x > 0) {
res = char(x % 2 + '0') + res;
x /= 2;
}
return res;
}
int main() {
int T; cin >> T;
while (T--) {
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cout << toBinary(i) << " ";
}
cout << endl;
}
return 0;
}
$\\\\$
Cách 3:$\\$
Thời gian: O(n log n)
Bộ nhớ: O(n log n)$\\$
#include <bits/stdc++.h>
using namespace std;
int main() {
int T; cin >> T;
while (T--) {
int n; cin >> n;
queue<string> q;
q.push("1");
for (int i = 1; i <= n; i++) {
string s = q.front(); q.pop();
cout << s << " ";
q.push(s + "0");
q.push(s + "1");
}
cout << endl;
}
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
#include <bits/stdc++.h>
using namespace std;
//typedef long long ll;
#define int long long
const int Maxn = 1e5 + 5;
int tt;
string doi (int x) {
if (x == 0) return "0";
string s;
while (x > 0) {
s.push_back(char('0' + (x % 2)));
x /= 2;
}
reverse(s.begin(), s.end());
return s;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> tt;
while (tt--) {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cout << doi(i) << " ";
}
cout << "\n";
}
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin