用priority_queue<int, vector<int>, cmp >pq的方式定义。下面是cmp的定义:
struct cmp
{
bool operator() (const int a,const int b) const //a的优先级比b小时返回true
{
return a%10>b%10;
}
};
“越小的整数优先级越大到的优先队列”可以写成“ priority_queue<int,vector<int>,greater<int> >pq”
注意:最后的“>”不要写在一起,否则会被认为是“>>”运算符
#include<iostream>
using namespace std;
int quickOne(int n)
{
int count = 0;
while (n) {
count;
n= n & (n - 1);
}
return count;
}
int main()
{
char n;
while(cin>>n)
cout<<quickOne(n)<<endl;
return 0;
}
在int main()中输入,并把文件放入程序所在文件夹,检测样例对错
要加有文件#include<cstdio>
freopen("a.txt","r",stdin);
char c;
for(int i=1;i<=5;i)
{
for(int j=1;j<=5;j)
{
a[i][j]=getchar();
}
getchar(); //注意:还要有一个getchar()用来放回车符
}
即使求:在一个凸多边形中,通过若干条互不相交的对角线,把这个多边形划分成了若干个三角形。任务是键盘上输入凸多边形的边数n,求不同划分的方案数f(n)。
a[n1] 4n-6 ------- = ------- a[n] n
a[4]=2,a[5]=5,a[6]=14,a[7]=42
括号内的,在用freopen("a.txt","r",stdin);该语句的时候,在txt文档中可用到
举例:
#include<iostream>
using namespace std;
long long cnt;
int a[500010];
int T[500010]; //必须定义在前面
void merge_sort(int* A,long long x,longlong y)
{
if(y-x>1)
{
int m=x(y-x)/2; //划分
int p=x,q=m,i=x;
merge_sort(A,x,m); //递归求解
merge_sort(A,m,y); //递归求解
while(p<m||q<y)
{
if(q>=y||(p<m&&A[p]<=A[q]))
T[i]=A[p]; //从左半数组复制到临时空间,比较后小的赋给虚拟空间
else
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-34960-3.html
有种时尚大片的赶脚~
美舰在我军的严厉警告下夹着尾巴逃跑了