【bool优先队列结构体学生身高体重排序】
#include<bits/stdc++.h>
using namespace std;
struct Student { //学生(结构体)
int Tall; //身高
int Weight; //体重
bool operator<(const Student &other) const
{//重载“<”符,定义出优先队列次序。
if (Tall > other.Tall)
{
return true;
}
else if (Tall == other.Tall)
{
//身高相同,看体重。
return Weight>other.Weight; //(填空)
}
else
{
return false;
}
}
};
int main()
{ //
priority_queue<Student> pq; //利用优先队列
int m;
Student Stu;
cin >> m;
for(int i=0; i<m; i++){
cin >> Stu.Tall;
cin >> Stu.Weight;
pq.push(Stu);
}
for(int i=0; i<m; i++)
{
Stu = pq.top();
pq.pop();
cout << Stu.Tall << " " << Stu.Weight << endl;
}
}
【升序降序排序】
#include<iostream>
#include<algorithm>
using namespace std;
const int size=1001;
int a[size];
bool compare( int a, int b)
{
return a>b;//* 填空 *
}
int main()
{
int order;
cin>>order;
while( 0 != order ){
int n;
cin>>n;
for( int i=0; i<n; i++){
cin>>a[i];
}
if( order < 0 )
{
sort(a,a+n,compare);
//* 填空 *
// 降序(定义排序规则),在这里就不需要对compare函数传入参数了。
}
else
{
sort(a,a+n);
//* 填空 *
// 升序(默认)。
}
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
cin>>order;
}
return 0;
}
【判断回文 翻转字符串 字符串比较】
#include<stdio.h>
#include<string.h>
void reverse(char *p,char *q)
{
if(p>=q)
return;
else
{
char t;
t=*p;*p=*q;*q=t;
}
reverse(p+1,q-1);
}
int main()
{
char s[100],c[100];
scanf("%s",s);
strcpy(c,s);
reverse(s,s+strlen(s)-1);
if(strcmp(s,c)==0)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
return 0;
}
【1-9无重复组成三个数,且成比例】
#include<stdio.h>
int main()
{
int a[10];
int i;
for(i=102;i<333;i++)
{
int b=i*2,c=i*3;
//printf("b=%d c=%d\n",b,c);
a[0]=i/100;
a[1]=i%100/10;
a[2]=i%10;
a[3]=b/100;
a[4]=b%100/10;
a[5]=b%10;
a[6]=c/100;
a[7]=c%100/10;
a[8]=c%10;
int flag = 1;
for(int j=0;j<9;j++)
{
//printf("a%d=%d\n",j,a[j]);
for(int k=j+1;k<9;k++)
{
//printf("ak=%d\n",a[k]);
if(a[j]==a[k]||a[j]==0)
{
flag=0;
break;
}
}
}
if(flag==1)
{
for(int m=0;m<9;m++)
{
printf("%d",a[m]);
if(m==2||m==5)
{
printf(" ");
}
else if(m==8)
{
printf("\n");
}
}
}
}
}