求助,关于C语言的字符输入
  • 板块学术版
  • 楼主Chinshyo
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/7/20 20:40
  • 上次更新2023/10/27 19:13:54
查看原帖
求助,关于C语言的字符输入
312820
Chinshyo楼主2022/7/20 20:40

请问C语言的getchar函数是如何使用的。我这里用了getchar函数,整个程序就在读入后RE了。改成Cin就能输出结果。

// Problem: P1196 [NOI2002] 银河英雄传说
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1196
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

int fa[30005], dis[30005], child[30005];

int find(int x) {
	if(fa[x] == x) {
		return x;	
	} else {
		fa[x] = find(fa[x]);	
		dis[x] = dis[x] + dis[fa[x]];
		return fa[x];
	}
}

void merge(int x, int y) {
	int px = find(x), py = find(y);
	fa[px] = py;
	child[py] = child[py] + child[px];
	dis[x] = child[y];
}

int main() {
	for(int i = 1; i <= 30003; i++) {
		fa[i] = i;
		dis[i] = 0;
		child[i] = 1;
	}
	
	int t;
	scanf("%d", &t);
	for(int i = 1; i <= t; i++) {
		char c;
		c = getchar();
		int x, y;
		scanf("%d%d", &x, &y);
		if(c == 'M') {
			merge(x, y);
		} else {
			if(find(x) == find(y)) printf("%d\n", abs(dis[find(x)] - dis[find(y)] - 1));
			else printf("%d\n", -1);
		}
	}
	return 0;
}

其中

char c;
c = getchar();
2022/7/20 20:40
加载中...