大纲
入门
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int k = 0;
while (n != 1) {
if (n % 2 == 0) {
n /= 2;
k++;
} else {
n = 3 * n + 1;
}
}
cout << k << endl;
return 0;
}
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
int N;
cin >> N;
unordered_map<int, int> record;
int school, score;
for (int i = 0; i < N; ++i) {
cin >> school >> score;
record[school] += score;
}
school = 0, score = 0;
for (pair<int, int> item: record) {
if (item.second > score) {
score = item.second;
school = item.first;
}
}
cout << school << " " << score << endl;
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX_N = 201;
int arr[MAX_N];
int main() {
int n, x;
while (cin >> n) {
for (int i = 0; i < n; ++i)
cin >> arr[i];
cin >> x; // CLion下此处获得 x 并不是用户输入的
int j;
for (j = 0; j < n; ++j) {
if (arr[j] == x) {
break;
}
}
if (j < n)
cout << j << endl;
else
cout << -1 << endl;
}
return 0;
}
注:此题在CLion运行可能会出错,源于CLion的bug。
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int N;
char c;
cin >> N >> c;
for (int i = 0; i < N; ++i)
cout << c;
cout << endl;
for (int i = 0; i < round(N / 2.0) - 2; ++i) { // 注意 2.0
cout << c;
for (int j = 0; j < N - 2; ++j)
cout << " ";
cout << c << endl;
}
for (int i = 0; i < N; ++i)
cout << c;
cout << endl;
return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int month[13][2] = {
{0, 0}, {31, 31},
{28, 29}, {31, 31},
{30, 30}, {31, 31},
{30, 30}, {31, 31},
{31, 31}, {30, 30},
{31, 31}, {30, 30},
{31, 31}
};
int isLeap(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return 1;
else
return 0;
}
int main() {
int time1, time2;
while (cin >> time1 >> time2) {
if (time1 > time2)
swap(time1, time2);
int y1 =time1/10000, m1 = time1%10000/100, d1 = time1%100;
int y2 =time2/10000, m2 = time2%10000/100, d2 = time2%100;
int ans = 1;
while (y1 < y2 || m1 < m2 || d1 < d2) {
d1++;
if (d1 == month[m1][isLeap(y1)] + 1) {
m1++;
d1 = 1;
}
if (m1 == 13) {
y1++;
m1 = 1;
}
ans++;
}
cout << ans << endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int main() {
// freopen("input.txt", "r", stdin);
int A, B, D;
cin >> A >> B >> D;
int sum = A + B;
stack<int> S;
do {
int r = sum % D;
S.push(r);
sum /= D;
} while (sum);
while (!S.empty()) {
cout << S.top();
S.pop();
}
cout << endl;
return 0;
}
注:将 p 进制的 x 转为 10 进制
int decimal(int x, int p) {
int res = 0, product = 1;
while (x) {
res += (x % 10) * product;
x /= 10;
product *= p;
}
return res;
}
#include <iostream>
using namespace std;
bool palindrome(string &str) {
int l = 0, r = str.size() - 1;
while (l < r) {
if (str[l] != str[r])
return false;
l++;
r--;
}
return true;
}
int main() {
string str;
cin >> str;
if (palindrome(str))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
vector<string> split(string &str) {
vector<string> words;
string s;
for (int i = 0; i < str.size(); i++)
if (str[i] == ' ') {
if (s != "") {
words.push_back(s);
s = "";
}
} else {
s += str[i];
}
if (s != "")
words.push_back(s);
return words;
}
int main() {
// freopen("input.txt", "r", stdin);
string str;
getline(cin, str);
vector<string> words = split(str);
for (int i = words.size() - 1; i >= 0; i--) {
cout << words[i];
if (i)
cout << " ";
}
return 0;
}
未完待续 …