comingsoon.cpp's blog

By comingsoon.cpp, history, 3 months ago, In English

Hello please i was wondering why my code for This problem B from the last atcoder beginner contest is getting WA for some few tests? This is the code below. I'd really appreciate any help :)

#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;

vector<int> tob(int n){
	vector<int> a;
	for(int i=0; i<n; i++){
		int ans = n%2;
		if(ans==0){
			a.push_back(ans);
		} else{
			break;
		}
		n/=2;
	}
	return a;
}
void solve(){
	//cout << "stuff works" << endl;
	int count = 0;
	int n; cin >> n;
	vector<int> a = tob(n);
	for(auto c : a ) count +=1;
	cout << count << endl;
}
int main (){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	//ll T = 1;
	//ll T; cin >> T;
	//for(;T--;){
	solve();
	//}
return 0;
}

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
3 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

because of the for loop on the tob function, just get rid of it and use while(n) instead and it'll work, because each step you're dividing n by 2 so at some point it'll become smaller than i even though there could be some zeros left