Blue Bridge Cup - 2012 C ++ A group Question 4: strange game [dfs]

I, entitled

   A television station held a low-carbon life Grand Prix. Scoring rules rather strange topic:

    Each player needs to answer 10 questions (numbered from 1-10), the more difficult the more behind. The answer, the current score doubled; failed to answer the same question number minus fraction (players must answer questions not answered by error handling).

    Each contestant had a starting score of 10 points.

    The final score of a winning player is just 100 points, if not let you watch the race, you can infer that he (she) which is the subject answered correctly, which it got it wrong topic?

    If the answer is denoted by 1, is referred to as a 0 wrong answer, the answer may be the case 10 containing only topic string of 0 and 1 are represented. For example: 0010110011 is possible.

    Your task is to calculate all possible cases. Each answer per line.

    Write your answer "answer .txt" Do not write here!

Second, the idea

With a storage array after each topic is right or wrong answer, 0 for error, 1 representative

After the case 10 directly answered questions include di and 100 points, and output array.

Next recursive dfs, if the score is doubled if the number is wrong subtracting the current title number.

have to be aware of is! ! That step must be placed on record score down the steps in front of dfs calculated.

Third, the problem solution

#include <iostream>
using namespace std;
int each_score[10]={0};//用来记录答完每个问题是对是错
void dfs(int n,int score)//n是第n个问题开始,score是默认分数
{
    if(n==11)//已经第10题之后
    {
        if(score==100)//如果已经得到100分
        {
            for(int i=1;i<11;i++)
            {
                cout << each_score[i] ;
            }
            cout << endl ;
            return ;
        }
        else
        {
            return;
        }
    }
    each_score[n]=0;//答错的情况
    dfs(n+1,score-n);
    each_score[n]=1;
    dfs(n+1,score*2);
}

int main()
{
    dfs(1,10);
    return 0;
}

 

Fourth, the results

0010110011
0111010000
1011010000

Process finished with exit code 0

In addition to the title of 0010110011, the answer is: 0111010000 and 1011010000

Published 57 original articles · won praise 9 · views 3566

Guess you like

Origin blog.csdn.net/Jayphone17/article/details/104279218