时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:8393

解决:1551

题目描述:

一个复数(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年北京邮电大学网院研究生机试真题

思路:

定义一个复数结构体,同时定义比较操作,对结构体数组进行插入排序。

我写的代码有点复杂了,没必要非要用链表。

代码:

#include <stdio.h>
#include <stdlib.h> #define N 1000 struct node {
int x;
int y;
struct node *next;
}; int size; int squareSum(int x, int y)
{
return x*x+y*y;
} struct node *insert(struct node *head, int x, int y)
{
if (head == NULL)
{
head = (struct node *)malloc(sizeof(struct node));
head->x = x;
head->y = y;
head->next = NULL;
printf("SIZE = %d\n", ++size);
return head;
}
struct node *p = head, *p0;
p0 = p;
while (p && squareSum(p->x, p->y) <= squareSum(x, y))
{
if (squareSum(p->x, p->y) == squareSum(x, y) && p->x > x)
break;
p0 = p;
p = p->next;
}
struct node *pnew = (struct node *)malloc(sizeof(struct node));
pnew->x = x;
pnew->y = y;
pnew->next = p;
printf("SIZE = %d\n", ++size);
if (p == head)
return pnew;
p0->next = pnew;
return head;
}
struct node * pop(struct node *head, int *x, int *y)
{
if (head == NULL)
{
printf("empty\n");
return head;
}
if (head->next == NULL)
{
printf("%d+i%d\n", head->x, head->y);
printf("SIZE = %d\n", --size);
return NULL;
}
struct node *p=head, *p0;
do {
p0 = p;
p = p->next;
} while (p->next != NULL);
printf("%d+i%d\n", p->x, p->y);
printf("SIZE = %d\n", --size);
p0->next = NULL;
return head;
} int main(void)
{
int n, i, x, y;
struct node *head;
char command[N]; while (scanf("%d", &n) != EOF)
{
head = NULL;
size = 0;
for(i=0; i<n; i++)
{
scanf("%s", command);
if (command[0] == 'P')
{
head = pop(head, &x, &y);
}
else
{
scanf("%d+i%d", &x, &y);
head = insert(head, x, y);
}
}
} return 0;
}
/**************************************************************
Problem: 1178
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/

九度OJ 1178:复数集合 (插入排序)的更多相关文章

  1. 【九度OJ】题目1178:复数集合 解题报告

    [九度OJ]题目1178:复数集合 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1178 题目描述: 一个复数(x+iy)集合,两种 ...

  2. 【九度OJ】题目1170:找最小数 解题报告

    [九度OJ]题目1170:找最小数 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1170 题目描述: 第一行输入一个数n,1 < ...

  3. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  4. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  5. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  6. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  7. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  8. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  9. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

随机推荐

  1. Codeforces 509E(思维)

                                                                                                         ...

  2. Web测试框架SeleniumBase

    前几天逛GitHub发现一个基于Selenium和unittest单元测试框架的一个测试框架SeleniumBase. Github地址:https://github.com/seleniumbase ...

  3. 关于bug的沟通

    关于BUG的沟通 一个人要去做一件事情,一般来说是按照自己的意愿去做的,如果不是自己想做而是被要求这么做的话,心里一定会留下点不愉快,特别是那种有自信有自己主见的人,比如说开发人员,当测试人员发现一个 ...

  4. 2016北京集训测试赛(十六)Problem B: river

    Solution 这题实际上并不是构造题, 而是一道网络流. 我们考虑题目要求的一条路径应该是什么样子的: 它是一个环, 并且满足每个点有且仅有一条出边, 一条入边, 同时这两条边的权值还必须不一样. ...

  5. Retrofit 使用flatmap操作符时处理错误、异常

    在实际项目(Retrofit+RxJava框架)中,有时需要先登录,获取token后再去获取用户信息,此时我们使用flatmap操作符比较好. 在RESTResult对象里,包括请求返回的状态:失败还 ...

  6. Azkban上传文件报错installation Failed.Error chunking

    azkaban 上传文件报错Caused by: java.sql.SQLException: The size of BLOB/TEXT data inserted in one transacti ...

  7. Aliyun-CentOS7.3 Init

    Aliyun-CentOS7.3 Init 一.概述 查看系统版本 $ cat /etc/redhat-release $ uname -a 修改主机名 $ vi /etc/hostname $ re ...

  8. 2016.12.5 在Eclipse中为实现类impl自动生成对应接口

    参考来自:http://jingyan.baidu.com/article/ab69b270d63f572ca6189f51.html 在Spring应用中,常常会用到“接口+实现类”的形式,即要实现 ...

  9. 过滤器Filter_03_多个Filter的执行顺序

    过滤器Filter_03_多个Filter的执行顺序 学习了:https://www.cnblogs.com/HigginCui/p/5772514.html 按照在web.xml中的顺序进行filt ...

  10. 更新到mysql 5.7后解决0000-00-00日期问题

    更新到mysql 5.7后解决0000-00-00日期问题 学习了:http://www.07net01.com/2016/04/1479450.html mysql 5.7 默认开始用以下sql m ...