108. 虚数

来源:互联网 发布:!在c语言中是什么意思 编辑:程序博客网 时间:2024/05/17 01:17
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct Node{
    int a;   // 实部 
    int b;   // 虚部 
    int m;   // 模 
}f[101];
bool cmp(Node a,Node b){        // 对模从小到大排序 
    return a.m<b.m;    
}
int main()
{
    int t;
    while(scanf("%d",&t)!=EOF){
        while(t--){
            int n;
            scanf("%d",&n);
            int size=0;
            while(n--){
                char s[100];
                scanf("%s",s);
                if(s[0]=='P'){
                    if(size==0) printf("Empty!\n");
                    else{
                        printf("%d+i%d\n",f[size].a,f[size].b);
                        size--; 
                        if(size==0) printf("Empty!\n");
                        else printf("Size: %d\n",size);
                    }   
                }else if(s[0]=='I'){
                    size++;
                    scanf("%d+i%d",&f[size].a,&f[size].b);
                    f[size].m=f[size].a*f[size].a+f[size].b*f[size].b;
                    sort(f+1,f+size+1,cmp);
                    printf("Size: %d\n",size);   
                }    
            }    
        }
    }
    return 0;
}
0 0
原创粉丝点击