穷举搜索:Exhaustive Search

点我访问题目链接

Write a program which reads a sequence A of n elements and an integer M, and outputs "yes" if you can make M by adding elements in A, otherwise "no". You can use an element only once.

You are given the sequence A and q questions where each question contains Mi.

Input

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers (Mi) are given.

Output

For each question Mi, print yes or no.

Constraints

  • n ≤ 20
  • q ≤ 200
  • 1 ≤ elements in A ≤ 2000
  • 1 ≤ Mi ≤ 2000

Sample Input 1

5
1 5 7 10 21
8
2 4 17 8 22 21 100 35

Sample Output 1

no
no
yes
yes
yes
yes
no
no

Notes

You can solve this problem by a Burte Force approach. Suppose solve(p, t) is a function which checkes whether you can make t by selecting elements after p-th element (inclusive). Then you can recursively call the following functions:

solve(0, M)
solve(1, M-{sum created from elements before 1st element})
solve(2, M-{sum created from elements before 2nd element})
...

The recursive function has two choices: you selected p-th element and not. So, you can check solve(p+1, t-A[p]) and solve(p+1, t) in solve(p, t) to check the all combinations.

For example, the following figure shows that 8 can be made by A[0] + A[2].

题目大意:

给定一个数组A、然后再给q个数、求数组A的数的任意几个数的和是否能组成q中的数字。

解题思路:

用递归与分治的方法、函数状态为选择第i个数和当前值。如果值为0则代表能成功、如果值大于0或者i大于等于n则不成功。

AC代码:

#include<iostream>using namespace std;int n,q;
int A[21];int solve(int i,int m)
{if(m==0) return 1;if(i>=n) return 0;int res=solve(i+1,m)||solve(i+1,m-A[i]);return res;
}int main()
{   cin >> n;for(int i=0;i<n;i++)cin >> A[i];cin >> q;int a;for(int i=0;i<q;i++){cin >> a;if(solve(0,a))cout << "yes\n";elsecout << "no\n"; }return 0;
}

Koch Curve

点我前往题目链接

Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n.

The Koch curve is well known as a kind of fractals.

You can draw a Koch curve in the following algorithm:

  • Divide a given segment (p1, p2) into three equal segments.
  • Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.
  • Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).

You should start (0, 0), (100, 0) as the first segment.

Input

An integer n is given.

Output

Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4.

Constraints

  • 0 ≤ n ≤ 6

Sample Input 1

1

Sample Output 1

0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Sample Input 2

2

Sample Output 2

0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250449
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250449
33.33333333 19.24500897
44.44444444 19.24500897
50.00000000 28.86751346
55.55555556 19.24500897
66.66666667 19.24500897
61.11111111 9.62250449
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250449
88.88888889 0.00000000
100.00000000 0.00000000

Notes

Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n.

The Koch curve is well known as a kind of fractals.

You can draw a Koch curve in the following algorithm:

  • Divide a given segment (p1, p2) into three equal segments.
  • Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.
  • Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).

You should start (0, 0), (100, 0) as the first segment.

Input

An integer n is given.

Output

Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4.

Constraints

  • 0 ≤ n ≤ 6

Sample Input 1

1

Sample Output 1

0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Sample Input 2

2

Sample Output 2

0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250449
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250449
33.33333333 19.24500897
44.44444444 19.24500897
50.00000000 28.86751346
55.55555556 19.24500897
66.66666667 19.24500897
61.11111111 9.62250449
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250449
88.88888889 0.00000000
100.00000000 0.00000000

详情请百度科赫曲线!

AC代码:

#include<bits/stdc++.h>
using namespace std;struct Point{double x,y;
};void Koch(int n,Point a,Point b)
{if(n==0)return;Point s,t,u;double th=M_PI*60.0/180.0;   //将单位从度转为弧度s.x=(2.0*a.x+1.0*b.x)/3.0; s.y=(2.0*a.y+1.0*b.y)/3.0;t.x=(1.0*a.x+2.0*b.x)/3.0;t.y=(1.0*a.y+2.0*b.y)/3.0;u.x=(t.x-s.x)*cos(th)-(t.y-s.y)*sin(th)+s.x;u.y=(t.x-s.x)*sin(th)+(t.y-s.y)*cos(th)+s.y;Koch(n-1,a,s);printf("%.8f %.8f\n",s.x,s.y);Koch(n-1,s,u);printf("%.8f %.8f\n",u.x,u.y);Koch(n-1,u,t);printf("%.8f %.8f\n",t.x,t.y);Koch(n-1,t,b);
}int main()
{Point a,b;int n;scanf("%d",&n);a.x=0;a.y=0;b.x=100;b.y=0;printf("%.8f %.8f\n",a.x,a.y);Koch(n,a,b);printf("%.8f %.8f\n",b.x,b.y);return 0;
}

全是英语…………我的妈哟………查字典查的脑瓜子疼!

【递归与分治】穷举搜索、科赫曲线相关推荐

  1. 用python画雪花 科赫曲线递归_python 画雪花 —科赫曲线的实现-Python 实用宝典

    漂亮的科赫曲线 科赫曲线是一种分形,其形态非常像雪花,因此又被称作科赫雪花.雪花曲线. 下面是用python的turtle包让我们来实时画一个如上图所示的雪花. import turtle def k ...

  2. 深入理解递归:美丽的科赫雪花

    一.科赫雪花简介: 所谓科赫雪花,也就是分形几何图形,例如: 分形几何是一种迭代的几何图形,广泛存在于自然界中. 我们来看看原理图: 二.科赫雪花的递归代码 先看看科赫曲线的递归代码: import ...

  3. 分治法与递归求科赫曲线

    分治法:把问题进行分解,通过求解局部的小问题来解开原本的问题.实现分治法需要用到递归 #include <iostream> #include<stdio.h> #includ ...

  4. 用python画雪花 科赫曲线递归_【TCE的编程小讲堂】【Python】【第三期】如何画出科赫雪花?(下)...

    大家还记得上期讲的科赫雪花吗?我们上次讲过画出一条边的方法,大家看看代码复习一下 import turtle#包含turtle库 def koch(l): turtle.forward(l / 4)# ...

  5. 函数的递归及科赫曲线绘制

    函数的递归及科赫曲线绘制 1 递归的定义 递归:在函数中调用自己本身 阶乘的例子表现了递归的两个特征: 1.存在一个或多个基例,基例不需要再次递归,它是确定的表达式 2.所有递归链要已一个或多个基例结 ...

  6. 【Python】递归绘制科赫曲线及科赫雪花及转换成可执行文件打包

    科赫曲线 ----------- 绘制科赫曲线 import turtle def koch(size, n):if n == 0:turtle.fd(size)else:for angle in [ ...

  7. java编写科赫曲线_分形——科赫曲线

    ? ? ? ? 这几天在因为在和别人合作写一个程序,就在想比如我要写一个科赫曲线,那么我写来给别人用的话,怎样是用着最方便的,即使是别人没有去详细看你的代码,拿到手就像写一个界面那样,直接 new 一 ...

  8. python绘制科赫曲线

    关于科赫曲线的变换: 1.将线段分成相等的三部分,ab,bc,cd 2.以bc为底,向内或向外做一个正三角形bcm,擦除bc 3.对ab,bm,mc,cd继续1.2操作 我们可以得出以下规律 可以看出 ...

  9. python学习笔记 第五章(科赫雪花与科赫曲线)

    科赫曲线,也叫做雪花曲线.是一种分形几何,分形几何是一种迭代的几何图形,广泛存在于自然界中. 科赫曲线是一种迭代的图形,所以我们是可以利用python中的迭代法来绘制. 我们先考虑如何实现迭代. 首先 ...

最新文章

  1. Transferring GANs: generating images from limited data 论文学习
  2. Verify that you have sufficient access to that key
  3. mybatis转义反斜杠_Shell echo命令:输出字符串
  4. OpenCASCADE:MFC示例
  5. 解决Windows 7删除执行过的 EXE、Bat文件有延迟的问题
  6. 使用caffemodel模型(由mnist训练)测试单张手写数字样本
  7. 星巴克全面上线美团外卖 并联合美团推出“1971客厅”
  8. java中的无效的列类型_java.sql.SQLException: 无效的列类型: 1111
  9. 看完这篇 HTTP,跟面试官扯皮就没问题了
  10. 为什么你就是学不会 Numpy ? | 技术头条
  11. python多维数据分析_使用python进行数据分析
  12. oracle修改asm参数文件,修改asm中的spfile参数
  13. Android shape属性大全
  14. ov7725摄像头--图像中间亮四周暗
  15. 拼多多二级限制惩罚要多久?怎么快速解除?
  16. 数据科学的重要支柱——统计学的最佳入门书籍
  17. 机器视觉系列(一)——概述
  18. 汽车基础——专业词汇
  19. 编译原理三大圣书——龙书、虎书、鲸书
  20. 麦子学院学习视频之机器学习(1):1.1 机器学习介绍

热门文章

  1. TWS 蓝牙耳机 ANC 调试步骤
  2. python3.6.6卸载_Python3.6安装卸载、执行命令、执行py文件的方法详解
  3. STM32的PID温控
  4. java s字符_javas字符串
  5. 关于LED电视机视频编码H.264,MPEG4,MPEG2能支持哪些格式的视频文件
  6. 百度无线音乐盒刷打印服务器,百度无线音乐盒设置路由器步骤:百度无线音乐盒设置智能路由器教程...
  7. 私人航空公司飞机HTML模板 - Flynext
  8. 项目——仿Steam商城
  9. 创新思维引领跨越式发展
  10. 使用statsmodels logit函数,出现MissingDataError: exog contains inf or nans