输入有多组数据。 每组输入一个n(1<=n<=1000),然后再输入n条指令。
根据指令输出结果。 模相等的输出b较小的复数。 a和b都是非负数。
3 Pop Insert 1+i2 Pop
empty SIZE = 1 1+i2 SIZE = 0
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<sstream> using namespace std; class fushu{ public: int shibu;int xubu;int mo; fushu(int s,int x){ shibu=s;xubu=x;mo=s*s+x*x; } }; struct fushuRule{ bool operator()(const fushu & a1,const fushu & a2){ if(a1.mo!=a2.mo){ return a1.mo>a2.mo; }else{ return a1.xubu<a2.xubu; } } }; int main(){ vector<string> vs; vector<fushu> vf; int n; cin>>n; while(n--){ string stemp; cin>>stemp; if(stemp.at(0)=='P'){ if(vf.size()==0){ cout<<"empty"<<endl; }else{ sort(vf.begin(),vf.end(),fushuRule()); fushu ftemp=*vf.begin(); cout<<ftemp.shibu<<"+i"<<ftemp.xubu<<endl; vf.erase(vf.begin()); cout<<"SIZE = "<<vf.size()<<endl; } }else if(stemp.at(0)=='I'){ string s,sa="",sb=""; int i=0,ia,ib; cin>>s; while(s.at(i)!='+'){ sa=sa+s.at(i++); } i+=2; while(i<s.length()){ sb=sb+s.at(i++); } stringstream ss; ss<<sa;ss>>ia;ss.clear(); ss<<sb;ss>>ib;ss.clear(); fushu* ftemp=new fushu(ia,ib); vf.push_back(*ftemp); cout<<"SIZE = "<<vf.size()<<endl; } } return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <string> using namespace std; class Plural { public: int real; //实部 int imaginary; //虚部 int modulus; //复数的模 Plural(int real, int imaginary) { this->real = real; this->imaginary = imaginary; calModulus(); } //计算模 void calModulus() { this->modulus = this->real * this->real + this->imaginary * this->imaginary; } }; //比较函数 bool compModulus(const Plural &p1, const Plural &p2) { if (p1.modulus != p2.modulus) return p1.modulus > p2.modulus; else return p1.imaginary < p2.imaginary; } deque<Plural> col; void operating(string c) { switch (c[0]) { case 'P': if (col.empty()) { cout << "empty"; } else { cout << col[0].real << "+i" << col[0].imaginary << endl; col.pop_front(); cout << "SIZE = " << col.size(); } break; case 'I': int Apos = c.find('+'); int ipos = c.find('i'); int real = atoi(c.substr(7, Apos - 7).c_str()); int imaginary = atoi(c.substr(ipos + 1, c.size() - 1 - ipos).c_str()); Plural plural(real, imaginary); col.push_back(plural); sort(col.begin(), col.end(), compModulus); cout << "SIZE = " << col.size(); break; } } int main() { int n; cin >> n; string command; cin.ignore(); //将取到的'\n'丢弃 for (int i = 0; i < n; ++i) { getline(cin, command); operating(command); if (i != n - 1) cout << endl; } return 0; }
#include<iostream> #include<queue> using namespace std; typedef struct Complex { int a,b; string s; bool operator <(Complex x) const { return (a * a + b * b) < (x.a * x.a + x.b * x.b); } Complex(int a_,int b_,string s_):a(a_),b(b_),s(s_){} }Complex; Complex Creat(string s) { int i1,i2,a = 0,b = 0; i1 = s.find('+'); i2 = s.find('i'); for(int i = 0;i < i1;i++) { a = 10 * a + (s[i] - '0'); } for(int i = i2 + 1;i < s.size();i++) { b = 10 * b + (s[i] - '0'); } return Complex(a,b,s); } int main() { priority_queue<Complex> q; int n; while(cin >> n) { string s; while(n--) { cin >> s; if(s == "Pop") { if(q.empty()) { cout << "empty" << endl; } else { cout << q.top().s << endl; q.pop(); cout << "SIZE = " << q.size() << endl; } } else { cin >> s; Complex c = Creat(s); q.push(c); cout << "SIZE = " << q.size() << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; struct fushu{ int a,b; }; bool cmp(fushu x,fushu y) { if(x.a*x.a+x.b*x.b==y.a*y.a+y.b*y.b)return x.b>y.b; else return x.a*x.a+x.b*x.b<y.a*y.a+y.b*y.b; } int main() { int n; while(cin>>n) { fushu f[1001]; int i=-1; string s; for(int j=0;j<n;++j) { cin>>s; if(s=="Pop") { if(i==-1)cout<<"empty"<<endl; else { cout<<f[i].a<<"+i"<<f[i].b<<endl<<"SIZE = "<<i<<endl; i--; } } else { cin>>s; int a=stoi(s.substr(0,s.find('+'))); int b=stoi(s.substr(s.find('i')+1)); ++i; f[i].a=a; f[i].b=b; sort(f,f+i+1,cmp); cout<<"SIZE = "<<i+1<<endl; } } } }
#include <bits/stdc++.h> using namespace std; struct Complex{ //构建结构体来表示复数,实部和虚部 int real; int imag; Complex(int a, int b): real(a), imag(b) {} bool operator < (Complex c) const{ //重载小于号,这里直接比较的是模的平方,就不用考虑小数的问题,注意题目要求模相等输出虚部较小的复数 if(real * real + imag * imag == c.real * c.real + c.imag * c.imag ){ //模相等 return imag > c.imag; } else{ return real * real + imag * imag < c.real * c.real + c.imag * c.imag; //模不相等,重新定义结构体的比较关系 } } }; int main(){ int n; string str; while(cin >> n){ getchar(); //用getchar()吃掉回车,不然会出错 priority_queue<Complex> PQ; //优先队列 for(int i = 0; i < n; ++i){ getline(cin, str); //接收一行字符串 if(str[0] == 'P'){ //Pop的情况 if(PQ.empty()){ cout << "empty" <<endl; } else{ Complex current = PQ.top(); PQ.pop(); cout << current.real << "+i" << current.imag << endl; cout << "SIZE = " << PQ.size() << endl; } } else if(str[0] == 'I'){ //Insert的情况 int pos = str.find(' '); string nstr = str.substr(pos+1); //第一步找到空格后的那部分字符串,即a+ib那部分 int pos1 = nstr.find('+'); int a = 0, b = 0; for(int i = 0; i < pos1; ++i){ //第二步从a+ib这个字符串中提取a和b a += nstr[i] - '0'; a *= 10; } a /= 10; for(int i = pos1+2; i < nstr.size(); ++i){ b += nstr[i] - '0'; b *= 10; } b /= 10; PQ.push(Complex(a, b)); //完成操作 cout << "SIZE = " << PQ.size() << endl; } } } return 0; }
#include<iostream> #include<cstdio> #include<queue> using namespace std; struct Complex{ int real_num; int imag_num; Complex(int a,int b): real_num(a),imag_num(b){} bool operator< (Complex c) const{ return real_num*real_num+imag_num*imag_num < c.real_num*c.real_num+c.imag_num*c.imag_num; } }; int main(){ int n; while(cin>>n){ priority_queue<Complex> myPriorityQueue; for(int i=0; i<n; i++){ string str; cin>>str; if(str=="Pop"){ if(myPriorityQueue.empty()){ cout<<"empty"<<endl; } else{ Complex temp = myPriorityQueue.top(); printf("%d+i%d\n",temp.real_num,temp.imag_num); myPriorityQueue.pop(); printf("SIZE = %d\n",myPriorityQueue.size()); } } else{ int a,b; scanf("%d+i%d",&a,&b); myPriorityQueue.push(Complex(a,b)); printf("SIZE = %d\n",myPriorityQueue.size()); } } } return 0; }
#include <iostream> #include <cmath> #include <cstring> using namespace std; struct plural{ long a,b,value; }; int main(){ int n,SIZE=0; plural gather[1001]; char inst[1000]; cin>>n; for(int i=0;i<n;i++) { cin>>inst; if(strcmp(inst,"Pop")==0){// if(SIZE==0){ cout<<"empty"; } else{ plural max={0,0,0}; int maxtex; for(int i=0;i<SIZE;i++){ if(gather[i].value>max.value){ max=gather[i]; maxtex=i; } else if(gather[i].value==max.value){ if(gather[i].b<max.b){ max=gather[i]; maxtex=i; } } } cout<<max.a<<'+'<<'i'<<max.b; for(int i=maxtex;i<SIZE;i++) gather[i]=gather[i+1]; SIZE--; cout<<SIZE; } } else{ scanf("%d+i%d",gather[SIZE].a,gather[SIZE].b); gather[SIZE].value=gather[SIZE].a*gather[SIZE].a+ gather[SIZE].b*gather[SIZE].b; SIZE++; cout<<SIZE; } } return 0; }
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; struct fushu{ int re; int im; }; vector<fushu> lst; bool cmp(fushu a,fushu b){ double mo_a = sqrt(a.im*a.im+a.re*a.re); double mo_b = sqrt(b.im*b.im+b.re*b.re); if(mo_a == mo_b){ return a.im<b.im; }else if(mo_a > mo_b){ return true; }else{ return false; } } void getMax(fushu &result){ vector<fushu>::iterator it = lst.begin(); it = lst.begin(); result = *it; lst.erase(it); } int main(){ int n; char com[10]; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s",com); if(strcmp(com,"Pop") == 0){ if(lst.empty()){ printf("empty\n"); }else{ sort(lst.begin(),lst.end(),cmp); fushu result; getMax(result); printf("%d+i%d\n",result.re,result.im); printf("SIZE = %d\n",lst.size()); } } if(strcmp(com,"Insert") == 0){ int a,b; scanf("%d+i%d",&a,&b); fushu num; num.im = b; num.re = a; lst.push_back(num); printf("SIZE = %d\n",lst.size()); } } }
//虽然大神都觉得很简单,但我还是弄了老久… #include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; struct number{ int x; int y; int value; }; bool cmp(number a,number b){ if(a.value==b.value) return a.y<b.y; else return a.value>b.value; } int main(){ vector<number> num;//我在main函数外面声明的时候总是报错,不能写单独的函数再调用 number nu; int n; scanf("%d",&n); num.clear(); for(int i=0;i<n;i++){ char str[100]; memset(str,0,sizeof(str)); scanf("%s",str);//开始也是写了gets函数疯狂报错,连循环运行都不顺畅 if(str[0]=='P'){ if(!num.size()) printf("empty\n"); else{ printf("%d+i%d\n",num.begin()->x,num.begin()->y); num.erase(num.begin()); printf("SIZE = %d\n",num.size());//注意这三句话的顺序 } } else{ int a,b; scanf("%d+i%d",&a,&b); nu.x=a; nu.y=b; nu.value=nu.x*nu.x+nu.y*nu.y; num.push_back(nu);//vector容器中结构体赋值方法 printf("SIZE = %d\n",num.size()); sort(num.begin(),num.begin()+num.size(),cmp);//vector中的排序 } } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ ArrayList<Pair> pairs=new ArrayList<Pair>(); Scanner scanner=new Scanner(System.in); while(scanner.hasNext()){ int n=scanner.nextInt(); scanner.nextLine(); for(int i=0;i<n;i++){ String instructions=scanner.nextLine(); if(instructions.equals("Pop")){ if(pairs.size()==0){ System.out.println("empty"); }else{ System.out.println(pairs.get(0).expression); pairs.remove(0); System.out.println("SIZE = "+pairs.size()); } }else{ String expression=instructions.split(" ")[1]; String[] strs=expression.split("\\+"); int x=Integer.valueOf(strs[0]); int y=Integer.valueOf(strs[1].substring(1)); Pair pair=new Pair(); pair.expression=expression; pair.value=x*x+y*y; pairs.add(pair); Collections.sort(pairs,new MyComparator()); System.out.println("SIZE = "+pairs.size()); } } } } } class Pair{ String expression; int value; } class MyComparator implements Comparator<Pair>{ public int compare(Pair t1,Pair t2){ return t1.value<t2.value?1:-1; } }
package com.speical.first;
import java.util.Scanner;
/**
* 复数集合
*
* 用到了数组模拟优先队列 + 插入排序的思想
* @author Special
* @time 2018/02/05 04:54:29
*/
public class Pro193 {
static Node[] nodes = new Node[1000 + 5];
static class Node{
int a;
int b;
int model;
public Node(int a, int b) {
this.a = a;
this.b = b;
this.model = (int) Math.sqrt(a * a + b * b);
}
}
public static void insertSort(Node[] nodes, int end) {
for(int i = end; i > 0 && (nodes[i].model < nodes[i - 1].model
|| (nodes[i].model == nodes[i - 1].model && nodes[i].b > nodes[i - 1].b)); i--){
Node temp = nodes[i];
nodes[i] = nodes[i - 1];
nodes[i - 1] = temp;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()) {
int n = input.nextInt();
input.nextLine();
int index = 0;
while(n-- > 0) {
String command = input.nextLine();
if(command.charAt(0) == 'P') {
if(index == 0) {
System.out.println("empty");
}else {
index--;
System.out.println(nodes[index].a + "+i"
+ nodes[index].b);
nodes[index] = null;
System.out.println("SIZE = " + index);
}
}else if(command.charAt(0) == 'I') {
int i, num, a = 0, b = 0;
boolean flag = false;
for(i = 0; i < command.length() && command.charAt(i) != ' '; i++);
for(++i; i < command.length(); i++) {
num = 0;
while(i < command.length() &&
command.charAt(i) >= '0' && command.charAt(i) <= '9') {
num = num * 10 + (command.charAt(i++) - '0');
}
if(!flag) {
a = num;
flag = true;
}else {
b = num;
}
}
nodes[index++] = new Node(a, b);
insertSort(nodes, index - 1);
System.out.println("SIZE = " + index);
}
}
}
input.close();
}
}
#include<bits/stdc++.h> using namespace std; struct fs{ int x; int y; int l; fs(int x=0,int y=0,int l=0):x(x),y(y),l(l){} bool operator < (const fs &b) const{ if(l!=b.l) return l<b.l; else return y>b.y; } }; priority_queue<fs> q;//使用大顶堆 int main(){ int n,u,v; while(scanf("%d",&n)!=EOF){ char c[7]; int size=0; for(int i=0;i<n;i++){ scanf("%s ",c); if(c[0]=='P'){ if(q.empty()==true) printf("empty\n"); else{ printf("%d+i%d\n",q.top().x,q.top().y); q.pop(); printf("SIZE = %d\n",--size); } } else{ scanf("%d+i%d",&u,&v); q.push(fs(u,v,u*u+v*v)); printf("SIZE = %d\n",++size); } } } }
#include <iostream> #include <cstdio> #include <cstring> #include <list> struct plur { int a; int b; int mo; plur(int a, int b) : a(a), b(b), mo(a * a + b * b) {}; }; int main() { int n, a, b; char cmd[10]; while (scanf("%d", &n) != EOF) { std::list<plur> mylist; for (int i = 0; i < n; i++) { scanf("%s", cmd); if (strcmp(cmd, "Pop") == 0) { if (mylist.empty()) { printf("empty\n"); } else { plur max = mylist.front(); mylist.pop_front(); printf("%d+i%d\nSIZE = %d\n", max.a, max.b, mylist.size()); } } else { scanf("%d+i%d", &a, &b); plur input(a, b); auto it = mylist.begin(); for (; it != mylist.end(); it++) { if (input.mo > it->mo) { break; } } mylist.insert(it, input); printf("SIZE = %d\n", mylist.size()); } } } return 0; }
注意弄清复数的模是什么意思:将复数的实部与虚部的平方和的正的平方根的值称为该复数的模。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct fushu{
int a, b;
}num[1000];
int numsize; //记录复数数量
bool cmp(struct fushu a, struct fushu b)
{
int moda = sqrt(a.a*a.a+a.b*a.b);
int modb = sqrt(b.a*b.a+b.b*b.b);
if(moda==modb) return a.b>b.b;
else return moda<modb;
}
void Insert(string str){ //将a+ib形式的复数插入到数组中
int a = 0, b = 0, flag = 0;
for(int i = 0; i<str.length(); i++)
{
if(str[i]=='+'||str[i]=='i') flag = 1;
else{
if(flag == 0)
a = a*10+str[i]-'0';
else
b = b*10+str[i]-'0';
}
}
num[numsize].a = a;
num[numsize].b = b;
numsize++;
cout<<"SIZE = "<<numsize<<endl;
}
void execute(string str) //处理命令
{
if(str=="Pop")
{
if(numsize==0)
cout<<"empty"<<endl;
else{
sort(num, num+numsize, cmp);
numsize--;
cout<<num[numsize].a<<"+i"<<num[numsize].b<<endl;
cout<<"SIZE = "<<numsize<<endl;
}
}
else{
int index = str.find(" ", 0);
Insert(str.substr(index+1, str.length()-index-1));
}
}
int main()
{
//freopen("test.txt","r", stdin);
int n;
string str;
while(cin>>n)
{
getline(cin, str); //读入回车符
numsize = 0;
for(int i = 0; i<n; i++)
{
getline(cin, str);
execute(str);
}
}
return 0;
}
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1111;
struct Complex_num
{
int a, b;
// 使用queue排序时一定要用
// bool operator<(const Class &tmp)cosnt这种格式
bool operator<(const Complex_num &t) const
{
return a * a + b * b < t.a * t.a + t.b * t.b;
}
};
int main()
{
freopen("in.txt", "r", stdin);
int T;
char str[10];
priority_queue<Complex_num> Q;
scanf("%d", &T);
while (T--)
{
scanf("%s", str);
Complex_num tmp;
if (strcmp(str, "Pop") == 0)
{
if (Q.size() == 0)
printf("empty\n");
else
{
tmp = Q.top();
printf("%d+i%d\n", tmp.a, tmp.b);
Q.pop();
printf("SIZE = %d\n", int(Q.size()));
}
}
else if (strcmp(str, "Insert") == 0)
{
scanf("%d+i%d", &tmp.a, &tmp.b);
Q.push(tmp);
printf("SIZE = %d\n", int(Q.size()));
}
}
return 0;
}
格式化+结构体保存,应该很好理解的。。。 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> typedef struct { int a, b, c; }Fu; int cmp(const void *a, const void *b) { return (*(Fu*)b).c - (*(Fu*)a).c; } int main() { Fu F[100] = {0}; int n, i = 0, key = 0; char S[10], N[100]; scanf("%d", &n); while(n--) { scanf("%s", &S); if(S[0]=='P') { for(i = 0; i <= key; i++) { F[i].c = F[i].a*F[i].a + F[i].b*F[i].b; } qsort(F, key, sizeof(F[0]), cmp); if(F[0].a == 0 && F[0].b == 0) printf("empty\n"); else { printf("%d+i%d\n", F[0].a, F[0].b); for(i = 0; i <= key; i++) { F[i].a = F[i + 1].a; F[i].b = F[i + 1].b; } key --; printf("SIZE = %d\n", key); } } else if(S[0]=='I') { gets(N); sscanf(N, "%d+i%d", &F[key].a, &F[key].b); key ++; printf("SIZE = %d\n", key); } } }