Codeforce254A(思维+txt 文件的输入输出)

题目链接https://vjudge.net/contest/345192#problem/E

在这里插入图片描述
翻译
给定2*n个数,求是否能平均的分成两堆。
如果能,输出每一堆相同数字的下标。
如果不能,输出-1。
分析
此题的关键在于题目的input:input.txt和output:output.txt
对于此输入要加

  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);

完整代码

#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    while(~scanf("%d",&n))
    {
        int a[5005]={0},a1[300002]= {0},a2[300002]= {0};
        int tot=0;
        for(int i=1; i<=2*n; i++)
        {
            int x;
            scanf("%d",&x);
            if(a[x]!=0)
            {
                a1[++tot]=a[x];
                a2[tot]=i;
                a[x]=0;
            }
            else
                a[x]=i;
        }
        if(tot!=n)
            printf("-1\n");
        else
        {
            for(int i=1; i<=n; i++)
                printf("%d %d\n",a1[i],a2[i]);
        }
    }
    return 0;
}
发布了165 篇原创文章 · 获赞 6 · 访问量 5071

猜你喜欢

转载自blog.csdn.net/lylzsx20172018/article/details/103299198
今日推荐