H

来源:互联网 发布:js canvas 画图 编辑:程序博客网 时间:2024/04/20 11:20

Problem H: 《为什么会变成这样呢》

Description

“第一次有了喜欢的人,还得到了一生的挚友,两份喜悦互相重叠,这双重的喜悦又带来了更多更多的喜悦,本应已经得到了梦幻一般的幸福时光,然而,为什么,会变成这样呢?”双重的喜悦感却无法带来更多的幸福,现在,雪菜在很多喜悦感之中只想要得到两份不重叠的喜悦感(其他的喜悦感都是重叠的),你能帮她找出这两份不同的喜悦感是多少吗?

Input

第一行一个整数T,代表数据的组数(1<=T<=10),接下来T组数据,每组数据的第一行是一个整数n(1<=n<=1000000),第二行是n个整数ai(0 <= ai <= 1000000000)代表喜悦感,每两个整数之间有一个空格,题目保证有且仅有两个不同的整数出现一次,其他的整数都是出现两次。

Output

对于每组数据,输出两个整数,分别代表两份不同的喜悦感,中间有一个空格,并且喜悦感较小的先输出。

Sample Input

262 2 1 1 3 441 1 3 4

Sample Output

3 43 4


第一种;
#include<stdio.h>#include<string.h>#include<stack>using namespace std;int main(){    int m;    scanf("%d",&m);    while(m--)    {        stack<int>s;        int i,n,b,q,w;        scanf("%d",&n);        for(i=0; i<n; i++)        {            scanf("%d",&b);            if(s.empty())                s.push(b);            else            {                if(b==s.top())                    s.pop();                else                    s.push(b);            }        }        q=s.top(),s.pop(),w=s.top();        if(q>w)            printf("%d %d\n",w,q);        else            printf("%d %d\n",q,w);    }    return 0;}
第二种;
#include<stdio.h>#include<string.h>#include<stack>using namespace std;int m,i,n,b,q,w;stack<int>s;int main(){    scanf("%d",&m);    while(m--)    {        scanf("%d",&n);        for(i=0; i<n; i++)        {            scanf("%d",&b);            if(s.empty())                s.push(b);            else            {                if(b==s.top())                    s.pop();                else                    s.push(b);            }        }        q=s.top(),s.pop(),w=s.top();        if(q>w)            q^=w^=q^=w;        printf("%d %d\n",q,w);    }    return 0;}

数组比栈快

#include<stdio.h>int m;int main(){    scanf("%d",&m);    while(m--)    {        int i,n,a[1000009],q,w;        scanf("%d",&n);        for(i=0; i<n; i++)            scanf("%d",&a[i]);        int flag=1;        for(i=0; i<n-1; i++)        {            if(a[i]==a[i+1])                i++;            else            {                if(flag)                    q=a[i];                else                    w=a[i];                flag=0;            }        }        if(q>w)            printf("%d %d\n",w,q);        else            printf("%d %d\n",q,w);    }    return 0;}



0 0
原创粉丝点击