Algorithm training of Quantum team (20190927)

Algorithm training of Quantum team (20190927)

A - 水仙花数
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+3^3。
现在要求输出所有在m和n范围内的水仙花数。
Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
Sample Input
100 120
300 380
Sample Output
no
370 371

#include "bits/stdc++.h"
using namespace std;

int main() {
	int l,r;
	while(cin >> l >> r) {
		int count = 0;
		for(int i=l; i<=r; i++) {
			int sum = pow(i%10,3) + pow((i/10)%10,3) + pow(i/100,3);
			if(sum == i) {
				count++;
				if(count == 1)
					cout << i;
				else
					cout << " " << i;
			}
		}
		if(count == 0)
			cout << "no";
		cout << endl;
	}
	return 0;
}
/*
100 120
300 380
*/

B - 变形课
呃…变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体.
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
Input
测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
Output
如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
Sample Input
so
soon
river
goes
them
got
moon
begin
big
0
Sample Output
Yes.

Harry 可以念这个咒语:“big-got-them”.
Hint
Hint


F-硬币
Snoopy has three coins. One day he tossed them on a table then and tried to flip some of them so that they had either all heads or all tails facing up. After several attempts, he found that regardless of the initial configuration of the coins, he could always achieve the goal by doing exactly two flippings, under the condition that only one coin could be flipped each time and a coin could be flipped more than once. He also noticed that he could never succeed with less than two flippings.

Snoopy then wondered, if he had n coins, was there a minimum number x such that he could do exactly x flippings to satisfy his requirements?

Input
The input contains multiple test cases. Each test case consists of a single positive integer n (n < 10,000) on a separate line. A zero indicates the end of input and should not be processed.

Output
For each test case output a single line containing your answer without leading or trailing spaces. If the answer does not exist, output “No Solution!”

Sample Input
2
3
0
Sample Output
No Solution!
2

#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
int main() {
	int n;
	while(scanf("%d",&n)!=EOF&&n) {
		if(n%2==0)
			printf("No Solution!\n");
		else
			printf("%d\n",n-1);
	}
	return 0;
}
/*
2
3
0
*/ 

G - Goldbach’s Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:
Every even number greater than 4 can be
written as the sum of two odd prime numbers.

For example:
8 = 3 + 5. Both 3 and 5 are odd prime numbers.
20 = 3 + 17 = 7 + 13.
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.)
Anyway, your task is now to verify Goldbach’s conjecture for all even numbers less than a million.
Input
The input will contain one or more test cases.
Each test case consists of one even integer n with 6 <= n < 1000000.
Input will be terminated by a value of 0 for n.
Output
For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying “Goldbach’s conjecture is wrong.”
Sample Input
8
20
42
0
Sample Output
8 = 3 + 5
20 = 3 + 17
42 = 5 + 37

#include "bits/stdc++.h"
using namespace std;

int p[1000010],n,a[200010],cnt;
void get_prime() {
	p[1]=1;
	for(int i=2; i<=1000; i++)
		if(!p[i])
			for(int j=i*2; j<=1000000; j=j+i)
				p[j]=1;
	for(int i=2; i<=1000000; i++)
		if(!p[i])a[++cnt]=i;
}
int main() {
	get_prime();
	while(scanf("%d",&n)==1&&n) {
		for(int i=1; a[i]<=n/2; i++)
			if(!p[n-a[i]]) {
				printf("%d = %d + %d\n",n,a[i],n-a[i]);
				break;
			} 
	}
	return 0;
}
/*
8
20
42
0
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
量子近似优化算法(Quantum Approximate Optimization Algorithm,简称QAOA)是一种基于量子计算的近似优化算法。它的目标是找到一个接近于最优解的解决方案,用于解决组合优化问题。 QAOA算法的主要思想是将优化问题转化为一个量子系统的能量最小化问题。该算法需要两个关键组成部分:参数化的量子电路和经典优化器。 首先,我们需要定义一个参数化的量子电路。这个电路将利用一系列的量子门操作来构建一个量子态。参数化量子电路的设计通常基于问题的结构和特点,以便在量子计算上进行优化。 接下来,我们需要选择一个经典优化器来优化参数化量子电路。经典优化器的目标是调整参数化量子电路的参数,以最小化经典优化问题的目标函数。经典优化器通常使用一些迭代的优化算法,如梯度下降法,来搜索最佳参数。 QAOA的代码实现主要包含以下几个步骤: 1. 初始化参数化量子电路的参数。 2. 使用经典优化器来优化参数。这包括计算目标函数的值,并更新参数化量子电路的参数。 3. 重复步骤2,直到达到预设的迭代次数或满足停止准则。 4. 获取最优的参数值,并根据这些参数配置量子电路。 5. 测量电路的输出,并统计得到优化问题的解决方案。 6. 输出近似的优化解。 需要注意的是,QAOA是一种近似优化算法,并不保证找到全局最优解。但通过增加迭代次数,我们可以增加获取更好近似解的可能性。 总结起来,QAOA代码实现的主要步骤包括初始化参数化量子电路、使用经典优化器进行参数优化、迭代优化步骤,以及获取优化解。这些步骤的具体实现会根据问题的不同而有所变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值