题解 | 合并表记录

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

using System;
public class Program {
    public static void Main() {
        int count;
        A[] a;
        int x, y;
        count = int.Parse(Console.ReadLine());
        a = new A[count];
        for (int i = 0; i < count; i++) {
            var s = Console.ReadLine();
            x = int.Parse(s.Split()[0]);
            y = int.Parse(s.Split()[1]);
            for (int j = 0; j <= i; j++) {
                if (!a[j].flag) {

                    a[j].x = x;
                    a[j].y = y;
                    a[j].flag = true;
                    break;
                } else if (a[j].x == x) {
                    a[j].y += y;
                    break;
                }
            }

        }
        Array.Sort(a,(c,b)=>c.x.CompareTo(b.x));
         
        for (int i = 0; i < count; i++)
        {
            if( a[i].flag)
            Console.WriteLine(a[i].x +" "+a[i].y);
        }   
    }
    public struct A {
        public int x, y;
        public bool flag;
    }
}

全部评论

相关推荐

04-25 18:13
五邑大学 Java
无面如何呢:用心包装一下自己的实习
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务