微信红包

时间:2017-02-12 19:58:49   收藏:0   阅读:290

用栈优化:

技术分享
#include <stdio.h>
#define MAXN 1000005
double stack[MAXN], temp;
int n, top;
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        top = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%lf", &temp);
            if(top == 0) stack[top++] = temp;
            else if(stack[top - 1] == temp) stack[top++] = temp;
            else top--;
        }
        printf("%.2lf\n", top == 0 ? -1 : stack[top - 1]);
    }
    return 0;
}
View Code

 

用2个变量代替栈:

技术分享
#include <stdio.h>
double ans, temp;
int n, size;
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        size = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%lf", &temp);
            if(size == 0) ans = temp, size++;
            else if(ans == temp) size++;
            else size--;
        }
        printf("%.2lf\n", ans);
    }
    return 0;
}
View Code

 

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!