ACGO 欢乐赛#35题解
2024-12-10 20:43:36
发布于:广东
T1:114514
此题非常简单,只需要输出n与114514的和就行了
代码:
#include <iostream>
using namespace std;
int main() {
int A;
cin >> A;
cout << A + 114514 << endl;
return 0;
}
T2:偶数的数量
思路:遍历整个数组,判断每一个元素是否是偶数
代码:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int count = 0;
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
if (num % 2 == 0) {
count++;
}
}
cout << count << endl;
return 0;
}
T3:6的个数
思路:把整数n当做一个字符串输入,然后遍历一遍即可
代码
#include <iostream>
#include <string>
using namespace std;
int main() {
string n;
cin >> n;
int count = 0;
for (char c : n) {
if (c == '6') {
count++;
}
}
cout << count << endl;
return 0;
}
T4:复杂的线性代数问题
根据题目要求判断即可
代码
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
bool f = true;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int num;
cin >> num;
if (j > i && num != 0) {
f = false;
break;
}
}
if (!f) {
break;
}
}
if (f) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
T5:复杂的子串问题
这一题,我们可以使用substr函数,来划分子串
代码
#include <iostream>
#include <string>
using namespace std;
int main() {
string S;
cin >> S;
int a, b;
cin >> a >> b;
cout << S.substr(a - 1, b - a + 1) << endl;
return 0;
}
T6:进制转换问题
用标准库函数即可
代码
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int n;
cin >> n;
stringstream ss;
ss << oct << n;
string s = ss.str();
int result = stoi(s, nullptr, 16);
cout << result << endl;
return 0;
}
这里空空如也
有帮助,赞一个