#include<bits/stdc++.h>
using namespace std;
void fun(int x ){
int a[10000];
int cont=0;
if(x==0){
cout<<0;
}
else {
int cnt=0;
for(int i=0; x>0; i++){
a[i]=x%2;
cnt++;
x=x/2;
}
for(int i=cnt-1; i>=0; i--){
if(a[i]==1){
cont++;
}
}
cout<<','<<cont;
}
}
int main(){
int x;
cin>>x;
int arr[x];
for(int i=0; i<=x; i++){
arr[i]=i;
fun(i);
}
}
to binary ecc
Last edited on
Also:
1 2 3 4 5
|
int main(){
int x;
cin>>x;
int arr[x];
|
That's not C++. It's using C99 extensions, and those have leaked into your C++ compiler.
And:
1 2
|
void fun(int x ){
int a[10000];
|
That's not great either, allocating 10,000 elements of 4 or 8 bytes on the stack.
A better alternative for both is to use a vector, which allocates memory off the heap.
1 2 3 4 5
|
int main(){
std::size_t x;
cin>>x;
std::vector<int> arr(x, 0);
|
And:
1 2
|
void fun(int x ){
std::vector<int> a(10000, 0);
|
Last edited on