+ 2
problem in program.
#include <iostream> using namespace std; int main() { // your code goes here int t; cin>>t; while(t--){ int n; cin>>n; int tsum=0; for(int i=1;i<n;i++){ int j=i; int pos=0; int sum=0; while(i){ if(pos%2==0){ if(i&1){ sum++; i<<=1; } else{ i<<=1; } pos++; } } tsum+=sum; i=j; } cout<<tsum<<endl; } return 0; } can anyone tell why this is failing and accepting too many inpts except 2 integers t and n??
9 Respuestas
+ 2
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int tsum=0;
for(int i=1;i<n;i++){
int j=i;
int pos=0;
int sum=0;
while(j){
if(pos%2==0){
if(j&1){
sum++;
j<<=1;
}
else{
j<<=1;
}
pos++;
}
}
tsum+=sum;
}
cout<<tsum<<endl;
}
return 0;
}
DHANANJAY PATEL I knew about t and n inputs in codechef it was todays contest problem between after this j still the problem isn't fixed??
+ 2
DHANANJAY PATEL the program should print all the occurrence of 1 odd bits from 1 to n.
Like if n = 2;
1=10, 2=100
So output is = 2
test at sololearn n results as
t=1;
n=2;
output: no output??
+ 1
HK Lite
It needs more than 2 input in case, if you have first input t (integer value) is > 1(greater than one). Since, input t is taken only ones. And input n is required for as many times as value t, if t = 3 then value of n is required to be input for 3 times.
Use case 1
Input values are
3
1
2
3
Use case 2
Input values
4
2
4
6
8
Output is number of one's bits in value n and sum of all one's bits at all even position in 1-n(1), 1-n(2) ... 1-n(t) and print the sum for t times.
fix is required for innermost while loop
while(j){
if (pos%2==0) {
if (j&1) {
sum++;
j<<=1;
}
else {
j<<=1;
}
pos++;
}
}
Fix is to replace i variable with j variable in innermost while loop to get required output.
DHANANJAY PATEL
+ 1
DHANANJAY PATEL did you tried and did it worked?? Between even codechef didn't provided the solution explanation video on this problem only 😶.
l tried but no gain;
+ 1
https://www.codechef.com/problems/ODDBITS
its wrong, totally wrong, try on this problems editor if you have time between thanks for your effort. DHANANJAY PATEL
0
HK Lite
Because you have taken input inside while loop so if there t is 5 then while loop will work till 5 times and there will be 5 inputs for n
If there t is 10 then while loop will work till 10 times and there will be 10 inputs for n.
0
It's misleading that
t = 1 and n = 2
1 in binary is 0001
2 in binary is 0010
Few more fixes are for odd bits summation
for loop should be
condition should be I <= n
for (I = 1; I <= n; I++)
And for shifting
j >>= 1 rather than j <<= 1
for t = 1 and n = 2
t = 1
n = 2
I = 1. sum = 1
I = 2. sum = 0
tsum = 1
This fixes will help you
0
HK Lite
fix is required for innermost while loop
while(j){
if (pos%2==0) {
if (j&1) {
sum++;
}
}
pos++;
j>>=1;
}
DHANANJAY PATEL
0
HK Lite
Complete Solution
#include <iostream>
using namespace std;
/*
1 to t values from 1 to n numbers
odd bits count summations calculate
*/
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int tsum=0;
int n;
cin>>n;
for(int i=1;i<=n;i++){
int j=i;
int pos=0;
int sum=0;
while(j){
if(pos%2==0){
if(j&1){
sum++;
}
}
pos++;
j>>=1;
}
tsum+=sum;
}
cout << tsum << endl;
}
return 0;
}