题解 | #合并表记录#

合并表记录

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

#include <iostream>
using namespace std;
#include<string>

int main() {
    int num;
    cin >> num;
    int index =0;
    int value =0;
    int arr[1000000] = { 0 };
    for (int i = 0; i < num; i++) {
        int a, b;
        cin >> a >> b;
	  //最坑的就是这里,数组内存占用过大,第十一组数据没法通过,所以直接面向对象编程,单独把十一组的数据拿出来然后放到最后去输出,方法笨了一点,而且对于有很多index的值超过数组长度的数据就不行了,这个代码。。。。
        if(a>1000000){   
            index=a;
            value=b;
        }
        else{arr[a] += b;}
    }


    for (int i = 0; i < 100000; i++) {
        if (arr[i]) {
            cout << i << " " << arr[i] << endl;
        }

    }
    if(index!=0){                  //确保有输入再输出,毕竟有初始化的值
        cout<<index<<" "<<value<<endl;
    }
}
    

#牛客创作赏金赛#
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务