九度题目1178 复数集合 北邮网研2011机试

本文介绍了一道关于复数集合操作的编程题,包括读取最大模值复数及插入复数的操作,并提供了C++与C语言实现的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个题烦了很久,终于在一个九度论坛的帖子里找到了一组测试用例,全部通过了之后,再提交,就AC了。哈哈。。。

题目1178:复数集合

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5351

解决:940

题目描述:

    一个复数(x+iy)集合,两种操作作用在该集合上:

    1、Pop 表示读出集合中复数模值最大的那个复数,如集合为空 输出  empty  ,不为空就输出最大的那个复数并且从集合中删除那个复数,再输出集合的大小SIZE;

    2 Insert a+ib  指令(a,b表示实部和虚部),将a+ib加入到集合中 ,输出集合的大小SIZE;

    最开始要读入一个int n,表示接下来的n行每一行都是一条命令。

输入:

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

输出:

根据指令输出结果。

样例输入:
3
Pop
Insert 1+i2
Pop
样例输出:
empty
SIZE = 1
1+i2
SIZE = 0
提示:

模相等的输出b较小的复数。

a和b都是非负数。

来源:
2011年北京邮电大学网院研究生机试真题


先贴上C++版的代码

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct Complex Complex;
struct Complex
{
	int a;
	int b;
	int mo;//模值还是定义为一个字段比较好
};
bool cmp(Complex x,Complex y){
    if(x.mo!=y.mo) return x.mo<y.mo;//升序,如果x.mo<y.mo为true,x排在y前面,所以要删除的数是mo大的
    else return x.b>y.b;//升序,如果x.b>y.b为True,x排在y前面,所以要删除的是b小的
}
int main()
{
	int n=0;
	vector<Complex> v;
	int a=0,b=0;
	while (~scanf("%d",&n)){
		Complex plex={0,0};
		v.clear();
		for(int i=0;i<n;i++){
			char cmd[101]={0};
			scanf("%s",cmd);
			if(cmd[0]=='P'){
				if(v.empty()){
					printf("empty\n");
					continue;
				}else{
					sort(v.begin(),v.end(),cmp);
					plex=*(v.end()-1);//最后一个元素是*(v.end()-1),谨记!!!!顺便想想遍历vector的循环怎么写
					v.pop_back();
					printf("%d+i%d\n",plex.a,plex.b);
					printf("SIZE = %d\n",v.size());
				}
			}else if(cmd[0]=='I'){
				scanf("%d+i%d",&a,&b);
				plex.a=a;
				plex.b=b;
				plex.mo=a*a+b*b;
				v.push_back(plex);
				printf("SIZE = %d\n",v.size());
			}
		}
	}
	return 0;
}
/**************************************************************
    Problem: 1178
    User: 爱理momoko
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1524 kb
****************************************************************/

再贴上C语言版的代码


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Complex Complex;
struct Complex
{
    int a;
    int b;
    int mo;
}complex[1001];
int cmp(const void *x,const void *y){
    if(((Complex *)x)->mo!=((Complex *)y)->mo) return ((Complex *)x)->mo-((Complex *)y)->mo;
    else return ((Complex *)x)->b<((Complex *)y)->b;
}
int main()
{
    int n=0,a=0,b=0;;
    char cmd[101]={0};
    while (~scanf("%d",&n)){
        Complex plex={0,0};
        int size=0;
        memset(complex,0,sizeof(complex));
        for(int i=0;i<n;i++){
            scanf("%s",cmd);
            if(cmd[0]=='P'){
                if(size==0){
                    printf("empty\n");
                }else{
                    qsort(complex,size,sizeof(Complex),cmp);
                    printf("%d+i%d\n",complex[size-1].a,complex[size-1].b);
                    complex[size-1].a=0;
                    complex[size-1].b=0;
                    complex[size-1].mo=0;
                    size--;
                    printf("SIZE = %d\n",size);
                }
            }else if(cmd[0]=='I'){
                scanf("%d+i%d",&a,&b);
                plex.a=a;
                plex.b=b;
                plex.mo=a*a+b*b;
                complex[size]=plex;
                size++;
                printf("SIZE = %d\n",size);
            }
        }
    }
    return 0;
}
/**************************************************************
    Problem: 1178
    User: 爱理momoko
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:924 kb
****************************************************************/

测试用例:

  1. 30
  2. Pop
  3. Insert 2+i1
  4. Insert 4+i3
  5. Insert 3+i3
  6. Insert 1+i2
  7. Insert 1+i2
  8. Insert 1+i1
  9. Insert 6+i8
  10. Pop
  11. Pop
  12. Insert 3+i3
  13. Pop
  14. Pop
  15. Pop
  16. Pop
  17. Pop
  18. Pop
  19. Pop
  20. Pop
  21. Pop
  22. Pop
  23. Pop
  24. Insert 2+i1
  25. Insert 4+i3
  26. Insert 3+i3
  27. Pop
  28. Pop
  29. Pop
  30. Pop
  31. Insert 2+i1



结果应该是:
  1. 30
  2. Pop
  3. Insert 2+i1
  4. Insert 4+i3
  5. Insert 3+i3
  6. Insert 1+i2
  7. Insert 1+i2
  8. Insert 1+i1
  9. Insert 6+i8
  10. Pop
  11. Pop
  12. Insert 3+i3
  13. Pop
  14. Pop
  15. Pop
  16. Pop
  17. Pop
  18. Pop
  19. Pop
  20. Pop
  21. Pop
  22. Pop
  23. Pop
  24. Insert 2+i1
  25. Insert 4+i3
  26. Insert 3+i3
  27. Pop
  28. Pop
  29. Pop
  30. Pop
  31. Insert 2+i1
  32. empty
  33. SIZE = 1
  34. SIZE = 2
  35. SIZE = 3
  36. SIZE = 4
  37. SIZE = 5
  38. SIZE = 6
  39. SIZE = 7
  40. 6+i8
  41. SIZE = 6
  42. 4+i3
  43. SIZE = 5
  44. SIZE = 6
  45. 3+i3
  46. SIZE = 5
  47. 3+i3
  48. SIZE = 4
  49. 2+i1
  50. SIZE = 3
  51. 1+i2
  52. SIZE = 2
  53. 1+i2
  54. SIZE = 1
  55. 1+i1
  56. SIZE = 0
  57. empty
  58. empty
  59. empty
  60. empty
  61. empty
  62. SIZE = 1
  63. SIZE = 2
  64. SIZE = 3
  65. 4+i3
  66. SIZE = 2
  67. 3+i3
  68. SIZE = 1
  69. 2+i1
  70. SIZE = 0
  71. empty
  72. SIZE = 1


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值