关于endl的神奇现象
查看原帖
关于endl的神奇现象
299042
algobase楼主2023/12/31 11:06
#include <bits/stdc++.h>
#define int long long
using namespace std;

int a[6], cnt, num[26] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 21, 44, 69, 96, 25, 56, 89, 24, 61, 0, 1, 4, 1, 1, 4, 9 };;
char s[1009], cmp[26][10] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "a", "both", "another", "first", "second", "third" };

signed main() {
	ios::sync_with_stdio(false);
	for (int i = 1; i <= 6; i++) {
		cin >> s;
		for (int j = 0; j < 26; j++) {
			if (!strcmp(s, cmp[j])) {
				a[++cnt] = num[j];
				break;
			}
		}
	}
	sort(a+1, a+cnt+1);
	bool flag = false;
	for (int i = 1; i <= cnt; i++) {
		if (flag) {
			printf("%.2d", a[i]);
			break;
		} else {
			if (a[i]) {
>>>>			cout << a[i] << endl;
				flag = true;
			}
		}
	}
	if (!flag) {
		cout << 0;
	}
	return 0;
}

样例输出:425

为了调试程序,此程序输出:

4
25

请各位关注带有 >>>> 的这一行代码

如果将 << endl 删去,上述输出中的换行理应消失,输出 425

但实际输出 254 ,将 endl 换为 '.' 或 '-' 甚至 '\n' ,仍先出 25 再输出中间字符最后输出 4

请问各位有没有合理的解释

2023/12/31 11:06
加载中...