#P1350. Day3 数组与字符串阅读(8题)

Day3 数组与字符串阅读(8题)

第 1 题

写出以下程序运行后的输出结果(只填数字):

int a[5] = {1, 2, 3, 4, 5};
for (int i = 1; i < 5; i++)
	a[i] += a[i - 1];
cout << a[4];

{{ input(1) }}

第 2 题

写出以下程序运行后的输出结果(只填数字):

int a[6] = {2, 1, 4, 1, 3, 5};
int ans = 0;
for (int i = 0; i < 6; i++)
	if (a[i] > 2) ans += a[i];
cout << ans;

{{ input(2) }}

第 3 题

写出以下程序运行后的输出结果(只填数字):

int a[3][3] = {
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < 3; i++)
	sum += a[i][2 - i];
cout << sum;

{{ input(3) }}

第 4 题

写出以下程序运行后的输出结果(只填数字):

string s = "csp2026";
int cnt = 0;
for (char c : s)
	if (c >= '0' && c <= '9') cnt++;
cout << cnt;

{{ input(4) }}

第 5 题

写出以下程序运行后的输出结果:

string s = "abcdef";
cout << s.substr(1, 4);

{{ input(5) }}

第 6 题

写出以下程序运行后的输出结果(只填数字):

string s = "ababa";
int cnt = 0;
for (int i = 0; i + 1 < (int)s.size(); i++)
	if (s[i] == s[i + 1]) cnt++;
cout << cnt;

{{ input(6) }}

第 7 题

写出以下程序运行后的输出结果(只填数字):

string s = "abccba";
bool ok = true;
for (int i = 0; i < (int)s.size() / 2; i++)
	if (s[i] != s[s.size() - 1 - i]) ok = false;
cout << ok;

{{ input(7) }}

第 8 题

写出以下程序运行后的输出结果(数字之间不加空格):

int a[5] = {3, 1, 4, 1, 5};
for (int i = 0; i < 2; i++) {
	int t = a[i];
	a[i] = a[4 - i];
	a[4 - i] = t;
}
for (int x : a) cout << x;

{{ input(8) }}