2018 c ++ B: change Lingchao; laser style

Fill in the blank
1. Title:
X planet notes with denominations only 100, 5- 2, $ 1, a total of four kinds.
Xiao Ming go x Stars Tours, his hand only 2 x $ 100 star coins, too inconvenient, just passing through x Star Bank enchant change.
Zhang Xiaoming number a little obsessive-compulsive disorder, he insisted that 200 yuan Ling Chao swapped out in just $ 2 $ 1 10 times the number of sheets, the rest of the course is 5 yuan denomination.
Bank staff a bit difficult, you can help calculate: Xiao Ming in the premise of meeting the requirements, at least give him want to change how many bills you (5 yuan, 2 yuan, 1 yuan denominations must have, can not be 0)?
Note that you need to submit is an integer, do not fill in any extra content

Mathematically: with x 1 yuan banknotes
(200-20X-X) / 5
can be divisible by 5 x = 5, banknotes least 74 Solution
A: 74
2. Title : X planet program for grand the atmosphere increases, with the light receiver unit 30 are lined up, the light beam shot into space. When installation is found, do not know why, two adjacent laser can not be opened at the same time, the king would like to know, in the current circumstances this bug exists, how many kinds of laser effects can play altogether?
Display, if only 3 machines, a total of five kinds of patterns can be to, namely: all closed (Sorry, silence speaks at this time, this, too, one)
to open a total of three kinds;
open two, only one kind;
30 bad well, the king had to make you useful
only required to submit an integer representing the laser pattern 30 can be formed of trees
recursion:
f (i, 0): represents all length i, the last one is 0, and the program number is not continuous 1
f (i, 1): represents all length i, the last bit is 1, and no continuous program number 0 is
f (i, 0) = f (i-1, 0) + F (I-1,1)
F (I,. 1) = F (I-1,0)
F (30,0) = F (30,0) + F (30,1)
method a: recursive law

#include<iostream>
using namespace std;
int main()
{
  int f[40][2]={0};   //局部变量需,要赋初值为0

   f[0][0]=1;  //第一位0位
   f[0][1]=0;    //第一位1位
for(int i=1;i<=30;i++)
{
  f[i][0]=f[i-1][0]+f[i-1][1];   //第i位为0的时候 
  f[i][1]=f[i-0][0];            //第i位为1的时候 
}
 cout<<f[30][0]+f[30][1]<<endl;
return 0;
}

The result:
2,178,309
Method Two: brute force

#include<iostream>
using namespace std;
//求x在二进制表示下第k位是0还是1,思路是把第k位移到个位,再&&上1判断
inline int get(int x,int k)  //函数;返回x的二进制里面第k位表示0还是1
{
    return x>>k & 1;  //直接返回
}
int main()
{
   int cnt=0;
   for(int i=0;i<1<<30;i++)  //枚举所有状态;左移:二进制表示1乘以2的30次方
{
   bool flag=true;   //判断答案是否合法
    for(int j=1;j<30;j++)   //依次枚举i这个转态的所有的二进制位;只要当前这一位和前一位的二进制位都是1的话表示不合法
    if(get(i,j) && get(i,j++))   //如果当前位是1,上一位也是1,
{
    flag=false;  //不合法
    break;
}
    cnt+=flag;
}
   cout<<cnt<<endl;
   return 0;
}

Published 65 original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/gl620321/article/details/104347189