首页 > 试题广场 >

复数集合

[编程题]复数集合
  • 热度指数:22234 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    一个复数(x+iy)集合,两种操作作用在该集合上:     1、Pop 表示读出集合中复数模值最大的那个复数,如集合为空 输出  empty  ,不为空就输出最大的那个复数并且从集合中删除那个复数,再输出集合的大小SIZE;     2 Insert a+ib  指令(a,b表示实部和虚部),将a+ib加入到集合中 ,输出集合的大小SIZE;     最开始要读入一个int n,表示接下来的n行每一行都是一条命令。

输入描述:
输入有多组数据。
每组输入一个n(1<=n<=1000),然后再输入n条指令。


输出描述:
根据指令输出结果。

模相等的输出b较小的复数。
a和b都是非负数。
示例1

输入

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;
 }

编辑于 2019-01-08 10:37:52 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
struct complex{
    int real;
    char op;
    int img;
};
bool com(complex a,complex b)
{
    int x=a.real*a.real+a.img*a.img;
    int y=b.real*b.real+b.img*b.img;
    if(x<y)
    return true;
    else 
    {
        if(x==y)
        if(a.img>b.img)
        return true;
    }
    return false;
}
int main()
{
    vector<complex> vec;
    int N;
    string st;
    char ch;
    while(cin>>N)
    {
        for(int i=0;i<N;i++)
        {
            cin>>st;
            if(st=="Insert")
            {
                complex cp;
                cin>>cp.real>>cp.op>>ch>>cp.img;
                vec.push_back(cp);
                cout<<"SIZE = "<<vec.size()<<endl;
                sort(vec.begin(),vec.end(),com);
            }
            else
            {
                if(vec.empty())
                cout<<"empty"<<endl;
                else
                {
                    complex c=vec.back();
                    vec.pop_back();
                    cout<<c.real<<c.op<<"i"<<c.img<<endl;
                    cout<<"SIZE = "<<vec.size()<<endl;
                }
            }
        }
    }
    return 0;
}

发表于 2018-08-20 09:10:54 回复(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;
}

发表于 2018-02-11 11:54:14 回复(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;
}

发表于 2021-02-01 22:55:03 回复(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;
            }
        }
    }
}


发表于 2021-01-29 09:54:36 回复(0)
#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;
}

发表于 2020-03-23 12:45:09 回复(0)
根据王道书上的思路,采用queue中的优先队列数据结构,每次访问top便可以找到当前最大,但目前仍存在瑕疵,望大家多多指点。
#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;
}


发表于 2020-02-18 16:31:28 回复(1)
段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起
【调了半天一直是这个错,,,,求大神帮忙!!!这到底是怎么回事啊?】
#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;
}

发表于 2019-03-14 11:39:48 回复(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());         }     }
} 

编辑于 2019-02-02 22:13:26 回复(0)
//虽然大神都觉得很简单,但我还是弄了老久…
#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;
}


发表于 2019-01-23 15:47:12 回复(0)
//运用scanf这个函数来合理处理Pop与Insert a+ib的指令问题。
//因为每一次赋值完按enter,会有一个回车键停留缓冲区自动进入下一次赋值。
//输入Pop指令,%s可以自动过滤掉enter,输入Insert指令,enter会发生冲突,故选择用空格过滤掉了开头的enter。
//对于Pop指令非空状态。先遍历,找出最大的数flag标记,每次遍历前更新flag=0放在开头。
//然后,将数组整体向前移动一位,注意此时最大长度为SIZE-2不要越界,即为删除。
#include <stdio.h>
#include <string.h>
int main(){
    int n;
    int arr1[1000],arr2[1000];
    int a,b;
    int num1,num2;
    int SIZE=0;
    char g[10];
    scanf("%d",&n);
    while(n--){
        if(scanf(" Insert %d+i%d",&a,&b)==2)
           {
               arr1[SIZE]=a;
               arr2[SIZE]=b;
               SIZE++;
               printf("SIZE = %d\n",SIZE);
           }
       
         else if(scanf("%s",g)==1 && strlen(g)==3)
           {
             if(SIZE==0) printf("empty\n");
             else { 
                     int flag=0;
                     for(int j=1;j<SIZE;j++){
                     num1=arr1[flag]*arr1[flag]+arr2[flag]*arr2[flag];
                     num2=arr1[j]*arr1[j]+arr2[j]*arr2[j];
                     if( (num1<num2) || (num1==num2 && arr2[flag]>arr2[j]) )
                         flag=j;
                     }
                     int x=arr1[flag];
                     int y=arr2[flag];
                     for(int s=flag;s<SIZE-1;s++){
                     arr1[s]=arr1[s+1];
                     arr2[s]=arr2[s+1];
                     }
                     SIZE--;
                     printf("%d+i%d\nSIZE = %d\n",x,y,SIZE);
                  }
           }
        else printf("input error\n");
     }

}
发表于 2018-09-21 19:01:48 回复(1)
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;
    }
}

发表于 2018-09-13 17:10:51 回复(0)
#include<cstdio>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>

typedef struct node {
    int a;
    int b;
    double mod;
    int flag;
}node;

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        node mynode[10000] = {};
        int count = 0;
        int realcount = 0;
        for (int i = 0; i < n; i++) {
            char c;
            c = getchar();
            while (c == ' ' || c == '\n') {
                c = getchar();
            }
            if (c == 'P') {
                scanf("op");
                if (realcount == 0)
                    printf("empty\n");
                else {
                    int local = 0;
                    double big = 0;
                    for (int x = 0; x < 1001; x++) {
                        if (mynode[x].flag == 1) {
                            if (mynode[x].mod > big) {
                                big = mynode[x].mod;
                                local = x;
                            }
                        }
                    }
                    printf("%d+i%d\n", mynode[local].a, mynode[local].b);
                    mynode[local].flag = 0;
                    realcount--;
                    printf("SIZE = %d\n", realcount);
                }
            }
            else if (c == 'I') {
                int aa = 0;
                int bb = 0;
                scanf("nsert %d+i%d",&aa,&bb);
                mynode[count].a = aa;
                mynode[count].b = bb;
                mynode[count].flag = 1;
                mynode[count].mod = (double)sqrt( (double)(aa*aa+bb*bb));
                count++;
                realcount++;
                printf("SIZE = %d\n", realcount);
            }
        }
    }
}
发表于 2018-02-08 23:03:56 回复(2)
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();
    }    

}
发表于 2018-02-05 17:25:34 回复(0)
#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);
            }
        }
    }
}
编辑于 2019-03-22 13:50:42 回复(1)
#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;
}

发表于 2017-06-03 22:14:19 回复(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;
}
发表于 2018-03-21 20:26:15 回复(5)
为啥没有说明size后和=后有空格,哭

发表于 2017-09-03 15:14:20 回复(3)

使用小根堆时间复杂度O(n*logn)

#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;
}
编辑于 2019-01-31 20:54:01 回复(5)
格式化+结构体保存,应该很好理解的。。。
#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);
        }
    }
}

发表于 2018-11-14 16:16:53 回复(0)

问题信息

难度:
128条回答 10335浏览

热门推荐

通过挑战的用户

查看代码
复数集合