CCF CSP202203-1未初始化警告【100分】

这篇博客展示了使用C++和Java编程语言实现的一个简单算法,该算法读取一系列整数对,并计算在这些整数对中不重复的第二个元素的数量。程序通过维护一个哈希映射来跟踪已出现的元素,从而达到计数目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 C++版

#include<iostream>
#include<algorithm>
#include<stack> 
#include<unordered_map> 
using namespace std;

int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	unordered_map<int,int> m;
	m[0] = 0;
	int count = 0;
	while(k--){
		int x,y;
		scanf("%d%d",&x,&y);
		if(m.count(y)==0){
			count++; 
		}
		m[x] = y;
	}
	cout<<count<<endl; 
}

Java版 

import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        map.put(0,0);
        int count = 0;
        while (k!=0){
            int x = sc.nextInt();
            int y = sc.nextInt();
            if(!map.containsKey(y)){
                count++;
            }
            map.put(x,y);
            k--;
        }
        System.out.println(count);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值