Algorithm training of Quantum team (20190802)

A - Ignatius and the Princess IV
“OK, you are not too bad, em… But you can never pass the next test.” feng5166 says.

“I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers.” feng5166 says.

“But what is the characteristic of the special integer?” Ignatius asks.

“The integer will appear at least (N+1)/2 times. If you can’t find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha…” feng5166 says.

Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
Sample Output
3
5
1

#include<bits/stdc++.h>

using namespace std;

int N[999999];

bool Less(int A,int B) {
	return A>B;
}

int main() {
	int n;
	int i=0;
	while(cin>>n) {
		memset(N,0,sizeof(N));
		i=0;
		int mask=0;
		int temp=(n+1)/2;
		int num;
		while(n--) {
			cin>>num;
			N[num]++;
			if(N[num]>=temp) {
				mask=num;
			}
		}
		cout<<mask<<endl;
	}
}
/*
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
*/

C - Humble Numbers
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, … shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence
Input
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line saying “The nth humble number is number.”. Depending on the value of n, the correct suffix “st”, “nd”, “rd”, or “th” for the ordinal number nth has to be used like it is shown in the sample output.
Sample Input
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0
Sample Output
The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long  const maxn=2000000000;
int cmp(const void *a,const void *b)
{
    return *(long long *)a>*(long long *)b?1:-1;
}
int cul(int a)
{
    int counter=0;
    long long temp=1;
    while(temp<maxn)
    {
        temp=temp*a;
        counter++;
    }
    return counter;
}
int main(int argc, const char * argv[]) {
    long long a[999999];
    int k2,k3,k5,k7,counter=0;
    k2=cul(2);
    k3=cul(3);
    k5=cul(5);
    k7=cul(7);
    cout<<k2<<" "<<k3<<" "<<k5<<" "<<k7<<endl;// 31 20 14 12
    for(int c2=0;c2<=k2;c2++)
    {
        for(int c3=0;c3<=k3;c3++)
        {
            for(int c5=0;c5<=k5;c5++)
            {
                for(int c7=0;c7<=k7;c7++)
                {
                    long long temp;
                    temp=pow(2,c2)*pow(3,c3)*pow(5,c5)*pow(7,c7);
                    if(temp<maxn)
                    {
                        a[counter]=temp;
                        counter++;
                    }
                }
            }
        }
    }
    counter++;
    qsort(a,counter,sizeof(a[0]),cmp);
    for(int i=0;i<counter;i++) if(a[i]>=0)cout<<a[i]<<",";
    cout<<endl;
    cout<<counter;
    return 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、付费专栏及课程。

余额充值