L1-071 Past Life Files (20 points)

L1-071 Past Life Files (20 points)

This kind of funny fortune-telling applet is often encountered in the online world. The principle of implementation is very simple. Just design a few questions randomly, and choose a path in the judgment tree according to the player’s answer to each question (as shown in the figure below), and the conclusion is The node corresponding to the end point of the path.

insert image description here

Now we number the conclusions sequentially from left to right, starting with 1. It is assumed here that the answers are all simple "yes" or "no", and it is also assumed that answering "yes" corresponds to a path to the left, and answering "no" corresponds to a path to the right. Given a series of responses from the player, you return the number of the conclusion it got.

Input format:

输入第一行给出两个正整数:N(≤30)为玩家做一次测试要回答的问题数量;M(≤100)为玩家人数。

随后 M 行,每行顺次给出玩家的 N 个回答。这里用 y 代表“是”,用 n 代表“否”。

Output format:

对每个玩家,在一行中输出其对应的结论的编号。

Input sample:

3 4
yny
nyy
nyn
yyn

Sample output:

3
5
6
2
#include<iostream>
#include<string>
#include<cmath>
using namespace std;

int main(){
    
    
    int num,person;
    string str;
    cin>>num>>person;
    int results[person];
    int possibility = pow(2,num);
    cin.clear();
    cin.ignore();
    for(int i=0;i<person;i++){
    
    
        int result = 1;
        getline(cin, str);
        int cnt = 2;
        for(int j=0;j<num;j++){
    
    
            if(str[j]=='n'){
    
           //如果为no
                result += possibility/cnt;   //序号值加上当前一半
            }
            cnt *=2;   
        }
        results[i] = result;
    }
    for(int x =0 ;x<person;x++){
    
    
        cout<<results[x]<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_46368082/article/details/118601690