acm大一寒假集训--异或/二进制枚举类

题目来源于东北林业大学OJ

东北林业大学OJ,点击进入

第三题开始为中文题目

题目一:teacher Li(格式陷阱)

Description
This time,suddenly,teacher Li wants to find out who have missed interesting DP lesson to have fun.The students who are found out will get strictly punishment.Because,teacher Li wants all the students master the DP algorithm.
However,Li doesn’t want to waste the class time to call over the names of students.So he let the students to write down their names in one paper.To his satisfaction,this time, only one student has not come.
He can get the name who has not come to class,but,it is troublesome,and,teacher always have many things to think about,so,teacher Li wants you, who is in the ACM team, to pick out the name.
Input
There are several test cases.The first line of each case have one positive integer N.N is the number of the students,and N will not greater than 500,000.
Then,following N lines,each line contains one name of students who have attended class.The N-1 lines are presented after N lines.These N-1 lines indicates the names of students who have come to class this time,one name in one line.
The length of student’s name is not greater than 30.
Process to the end of file.
Output
For each test case, first print a line saying “Scenario #k”, where k is the number of the test case.Then output the name of the student who have not come to class.One case per line.Print a blank line after each test case, even after the last one.
Sample Input
3
A
B
C
B
C
Sample Output
Scenario #1
A

#include 

using namespace std;
int main()
{
    char cs[35],ans[35];
    int n,t=0;
    while(cin>>n)
    {
        t++;
        int i,j;
        cin >> ans;
        for(i=1;i<=2*n-2;i++)
        {
            cin>>cs;
            int r1,r2;
            r1=strlen(ans);
            r2=strlen(cs);
            for(j=0;j<(r1>r2?r1:r2);j++)
            {
                  ans[j]^=cs[j];
            }
        }
        cout << "Scenario #" << t << endl ;
        cout << ans << endl << endl ;
    }
    return 0;
}

题目二:Find different

Description
Give an odd number n, (1<=n<=10000001)

Given you an array which have n numbers : a[1], a[2] a[3] … a[n].They are positive num.
There are n/2 numbers which appear twice and only one number appears once.
Now you should tell me the number which appears only once.
Input
There are several test cases end with EOF.
The first line there is a integer n.
Then the 2nd line there are n integer a[1],a[2]…a[n].
Output
For each case, output the number which appears only once.
Sample Input
7
3 2 7 2 1 7 3
1
7
11
1 1 2 2 3 3 4 4 5 5 9
Sample Output
1
7
9

#include

using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        int i;
        int a=0;
        for(i=1;i<=n;i++)
        {
            int t;
            cin >> t;
            a^=t;
        }
        cout << a << endl ;
    }
    return 0;
}

题目三:和为K–二进制枚举

Description
给出长度为n的数组,求能否从中选出若干个,使他们的和为K.如果可以,输出:Yes,否则输出No
Input
第一行:输入N,K,为数组的长度和需要判断的和(2<=N<=20,1<=K<=10^9)
第二行:N个值,表示数组中元素的值(1<=a[i]<=10^6)
Output
输出Yes或No
Sample Input
5 13
2 4 6 8 10
Sample Output
No

#include

using namespace std;

int main()
{
    int n,k,i;
    int a[25];
    while(cin >> n >> k)
    {
        int t;
        for(i=0;i<n;i++)
        {
            cin >> a[i];
        }
        int flag=0;
        for(i=0;i<(1<<n);i++)
        {
            int j;
            int all=0;
            for(j=0;j<n;j++)
            {
                if(i&(1<<j))
                {
                    all+=a[j];
                }
            }
            if(all==k)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            cout << "Yes" << endl ;
        else
            cout << "No" << endl ;
    }
}

题目四:陈老师加油-二进制枚举

Description
陈老师经常开车在哈尔滨的大街上行走,假设刚开始油箱里有T升汽油,每看见加油站陈老师就要把汽油的总量翻倍(就是乘2);每看见十字路口气油就要减少1升;最后的时候陈老师的车开到一个十字路口,然后车就没油了------就熄火了,陈老师好痛苦啊~~~!
然后他就开始回忆,一路上一共遇到5个加油站,10个十字路口,问造成这种惨烈的境遇有多少种可能?

Input
输入一个T ,(1<=T<=100);
Output
输出可能的方案数。
Sample Input
1
Sample Output
10

#include

using namespace std;

int main()
{
    int n,k,i,t;
    while(cin >> t)
    {
        int i;
        int flag=0;
        for(i=0;i<=(1<<14);i++)
        {
            int jc=t;
            int j;
            int jl1=0,jl2=0;
            for(j=0;j<15;j++)
            {
                if(i&(1<<j))
                {
                    jl1++;
                    jc*=2;
                }
                else
                {
                    jl2++;
                    jc--;
                }
            }
            if(jc==0&&jl1==5)
            {
                flag++;
            }
        }
        cout << flag << endl ;
    }
    return 0;
}

题目五:纸牌游戏-二进制-搜索

Description
给你一些扑克,每张都对应一个点数,分别对应1-13,K 就是13;J 是11;Q是12;
现在想从这些扑克牌中取出一些牌,让这些牌的点数的和等于一个幸运数值P,问有多少种方案?
Input
输入数据第一行为n和p,分别代表n张扑克牌和幸运数(1<=n<=20,p<=260)
接下来是这n张牌的点数; 1<=点数<=13;
Output
输出能得到P 的方案数?
Sample Input
5 5
1 2 3 4 5
Sample Output
3

#include

using namespace std;

int main()
{

    int n,p,k;
    int a[25];
    while(cin >> n >> k)
    {
        int i;
        int flag=0;
        for(i=0;i<n;i++)
        {
            cin >> a[i];
        }
        for(i=0;i<(1<<n);i++)
        {
            int j;
            int all=0;
            for(j=0;j<n;j++)
            {
                if(i&(1<<j))
                {
                    all+=a[j];
                }
            }
            if(all==k)
                flag++;
        }
        cout << flag << endl ;
    }

    return 0;
}

题目六:权利指数

Description
在选举问题中,总共有n个小团体,每个小团体拥有一定数量的选票数。如果其中m个小团体的票数和超过总票数的一半,则此组合为“获胜联盟”。n个团体可形成若干个获胜联盟。一个小团体要成为一个“关键加入者”的条件是:在其所在的获胜联盟中,如果缺少了这个小团体的加入,则此联盟不能成为获胜联盟。一个小团体的权利指数是指:一个小团体在所有获胜联盟中成为“关键加入者”的次数。请你计算每个小团体的权利指数。
Input
输入数据的第一行为一个正整数T,表示有T组测试数据。每一组测试数据的第一行为一个正整数n(0 Output
对每组测试数据,在同一个行按顺序输出1到n号小团体的权利指数。
Sample Input
2
1
10
7
5 7 4 8 6 7 5
Sample Output
1
16 22 16 24 20 22 16

#include 
using namespace std;
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        int n;
        cin >> n;
        int i;
        double dy=0;
        int score[25]={0};
        int a[25];
        for(i=0;i<n;i++)
        {
            cin >> a[i];
            dy+=a[i];
        }
        dy/=2;
        for(i=0;i<(1<<n);i++)
        {
            int all=0,j;
            for(j=0;j<n;j++)
            {
                if(i&(1<<j))
                    all+=a[j];
            }
            if(all>dy)
            {
                for(j=0;j<n;j++)
                {
                    if(i&(1<<j))
                    if(all-a[j]<=dy)
                        score[j]++;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            if(i==n-1)
                cout << score[i] << endl ;
            else
                cout << score[i] << " ";
        }
    }
    return 0;
}

题目七:趣味解题

Description
ACM程序设计大赛是大学级别最高的脑力竞赛,素来被冠以"程序设计的奥林匹克"的尊称。大赛至今已有近40年的历史,是世界范围内历史最悠久、规模最大的程序设计竞赛。比赛形式是:从各大洲区域预赛出线的参赛队伍,于指定的时间、地点参加世界级的决赛,由1个教练、3个成员组成的小组应用一台计算机解决7到13个生活中的实际问题。
现在假设你正在参加ACM程序设计大赛,这场比赛有 n 个题目,对于第 i 个题目你有 a_i 的概率AC掉它,如果你不会呢,那么这时候队友的作用就体现出来啦,队友甲有 b_i 的概率AC掉它, 队友乙有 c_i 的概率AC掉它,那么现在教练想知道你们队伍做出 x 个题目的概率。
Input
输入一个正整数T(T<=100),表示有T组数据,对于每组数据首先输入一个 n (7<=n<=13),表示有 n 个题目,接下来输入三行,
第一行输入 n 个数a_i,第二行输入 n 个数b_i,第三行输入 n 个数c_i, 其中 a_i, b_i, c_i 的意义如题,最后输入一个 x 表示教练想要知道你们队伍做出的题目数(x>=0)。
Output
输出一行表示结果,保留4位小数
Sample Input
2
7
0.1 0.2 0.3 0.4 0.5 0.6 0.7
0.2 0.3 0.4 0.5 0.6 0.7 0.8
0.3 0.4 0.5 0.6 0.7 0.8 0.9
1
7
0.1 0.2 0.3 0.4 0.5 0.6 0.7
0.2 0.3 0.4 0.5 0.6 0.7 0.8
0.3 0.4 0.5 0.6 0.7 0.8 0.9
5
Sample Output
0.0000
0.2811

#include 
using namespace std;
int main()
{
    int T,m;
    double a[15],b[15],c[15];
    cin >> T;
    while(T--)
    {
        int n;
        cin >> n;
        int i;
        for(i=0; i<n; i++)
            cin >> a[i];
        for(i=0; i<n; i++)
            cin >> b[i];
        for(i=0; i<n; i++)
            cin >> c[i];
        cin >> m;
        double ans=0;
        for(i=0; i<(1<<n); i++)
        {
            int j;
            int flag=0;
            double gl=1;
            for(j=0; j<n; j++)
            {
                if(i&(1<<j))
                {
                    gl*=a[j]+(1-a[j])*b[j]+(1-a[j])*(1-b[j])*c[j];
                    flag++;
                }
                else
                {
                    gl*=(1-a[j])*(1-b[j])*(1-c[j]);
                }
            }
            if(flag==m)
                ans+=gl;
        }
        printf("%.4lf\n",ans);
    }
    return 0;
}

你可能感兴趣的:(c++入门)