Title Description
In order to enrich the extracurricular life of party members, the Academy organized a contest to learn the knowledge of a strong country. The scoring rules of the contest are: each player has a starting score of 10 points, and the contestants need to answer 10 questions (numbered 1 to 10) , The answer is correct, the current score is doubled, if the answer is wrong, the score with the same question number will be deducted.Input
The first line has an integer M (1 <= M <100) means there are M sets of test data; then each line enters a score score (0≤score≤100)
Output
Assuming that you have not watched the game, please output the number of possible situations for each score, and output -1 if there are no possible situations. (Hint: If 0 represents the player ’s wrong question, 1 represents the player ’s correct question, and the player ’s final score is 90, there may be the following two situations
1110000010
0001110110)Sample input
3 68 63 100Sample output
7 -1 3
Original title link: Interesting game
Observed:
0000000000 -> 0
0000000001 -> 1
0000000010 -> 2
…
1111111111 -> 1023
You only need to traverse 0 ~ 1023 to get all the cases, and judge whether each case meets the entered score.
- 1. Grasp the decimal number to binary number (% 2 each time)
- 2. The front vacancy needs to be filled with 0 (the whole is set to 0, and the result of inserting% 2 from the back)
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main() {
int m;
cin>>m;
int a[10];
while(m--) {
int score;
cin>>score;
int cnt=0;
for(int i=0; i<=1023; i++) {
memset(a,0,sizeof(a));
int p=9;
int t=i;
while(t) {
a[p--]=t%2;
t/=2;
}
int s = 10;
for(int j=0; j<10; j++) {
if(a[j]==0) s-=(j+1);
else s*=2;
}
if(score==s) cnt++;
}
if(cnt==0) cout<<-1<<endl;
else cout<<cnt<<endl;
}
return 0;
}