csppass
连续 00 XP登录 / 注册
201922判断题贪心算法普及/提高

程序(二,贪心配对更新):m>0时输出是否一定小于2n

题目

阅读下面的程序,回答问题。假设输入的n和m都是正整数,x和y都是在[1,n]范围内的整数。 #include<cstdio> using namespace std; int n, m; int a[100], b[100]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) a[i] = b[i] = 0; for (int i = 1; i <= m; ++i) { int x, y; scanf("%d%d", &x, &y); if (a[x] < y && b[y] < x) { if (a[x] > 0) b[a[x]] = 0; if (b[y] > 0) a[b[y]] = 0; a[x] = y; b[y] = x; } } int ans = 0; for (int i = 1; i <= n; ++i) { if (a[i] == 0) ++ans; if (b[i] == 0) ++ans; } printf("%d", ans); return 0; } 判断题:当m>0时,输出的值一定小于2n。( )

考点拆解
程序阅读与结果判断
易错提醒
阅读程序题要按变量变化顺序手推,不要跳步
选择题要检查单位、边界和题目中的否定词