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

穷举搜索: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";
		else
			cout << "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;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值