题目链接:http://lx.lanqiao.cn/problemset.page?code=BASIC
BASIC-1 闰年判断
题解
模拟。
代码
#include <bits/stdc++.h> using namespace std; int main() { int y; cin >> y; cout << ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0) ? "yes" : "no") << " "; return 0; }
BASIC-2 01字串
题解
五重循环枚举或DFS均可。
代码
#include <bits/stdc++.h> using namespace std; string s = "00000"; void dfs(int dep) { if (dep == 5) { cout << s << " "; return; } s[dep] = '0'; dfs(dep + 1); s[dep] = '1'; dfs(dep + 1); } int main() { dfs(0); return 0; }
BASIC-3 字母图形
题解
观察发现A的位置与所在行数相关,确定 $A$ 后向左右延伸出 $BCDdots$ 即可。
Tips
当行数比列数大时,枚举A时会越界,可以先预设出足够的空间。
代码
#include <bits/stdc++.h> using namespace std; const int N = 30; int n, m; vector<string> MP(N, string(N, '#')); int main() { cin >> n >> m; for (int i = 0; i < n; i++) { char ch = 'A'; for (int j = i; j < m; j++) MP[i][j] = ch++; ch = 'A'; for (int j = i; j >= 0; j--) MP[i][j] = ch++; for (int j = 0; j < m; j++) cout << MP[i][j]; cout << " "; } return 0; }
BASIC-4 数列特征
题解
模拟。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; cout << *max_element(a.begin(), a.end()) << " "; cout << *min_element(a.begin(), a.end()) << " "; cout << accumulate(a.begin(), a.end(), 0) << " "; return 0; }
BASIC-5 查找整数
题解
模拟。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int x; cin >> x; int pos = find(a.begin(), a.end(), x) - a.begin(); cout << (pos < n ? pos + 1 : -1) << " "; return 0; }
BASIC-6 杨辉三角形
题解
模拟。
代码
#include <bits/stdc++.h> using namespace std; const int N = 50; int main() { int n; cin >> n; vector<vector<int> > f(N, vector<int>(N)); for (int i = 1; i < N; i++) f[i][1] = f[i][i] = 1; for (int i = 3; i < N; i++) { for (int j = 2; j < i; j++) { f[i][j] = f[i - 1][j - 1] + f[i - 1][j]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << f[i][j] << " "[j == i]; } } return 0; }
BASIC-7 特殊的数字
题解
模拟。
代码
#include <bits/stdc++.h> #define pow3(n) (n * n * n) using namespace std; bool ok(int n) { int a = n / 100; int b = n / 10 % 10; int c = n % 10; return pow3(a) + pow3(b) + pow3(c) == n; } int main() { for (int i = 100; i <= 999; i++) { if (ok(i)) cout << i << " "; } return 0; }
BASIC-8 回文数
题解
根据回文数的性质构造出前半部分即可,注意第一位不能为0。
代码
#include <bits/stdc++.h> using namespace std; int main() { for (int i = 1; i <= 9; i++) { for (int j = 0; j <= 9; j++) { cout << i << j << j << i << " "; } } return 0; }
BASIC-9 特殊回文数
题解
同上。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 1; i <= 9; i++) { for (int j = 0; j <= 9; j++) { for (int k = 0; k <= 9; k++) { if (i + j + k + j + i == n) { cout << i << j << k << j << i << " "; } } } } for (int i = 1; i <= 9; i++) { for (int j = 0; j <= 9; j++) { for (int k = 0; k <= 9; k++) { if (i + j + k + k + j + i == n) { cout << i << j << k << k << j << i << " "; } } } } return 0; }
BASIC-10 十进制转十六进制
题解
模拟。
Tips
考虑到 $n = 0$ 的情况,用 do while() 。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; do { int m = n % 16; if (m <= 9) s += '0' + m; else s += 'A' + m - 10; n /= 16; } while (n); reverse(s.begin(), s.end()); cout << s << " "; return 0; }
BASIC-11 十六进制转十进制
题解
模拟。
代码
#include <bits/stdc++.h> using namespace std; long long hex_to_dec(const string &hex) { long long res = 0; for (int i = 0; i < hex.size(); i++) { res = res * 16 + (hex[i] <= '9' ? (hex[i] - '0') : (10 + hex[i] - 'A')); } return res; } int main() { string str; cin >> str; cout << hex_to_dec(str) << " "; return 0; }
BASIC-12 十六进制转八进制
题解
模拟。
代码
#include <bits/stdc++.h> using namespace std; string hex_to_bin(const string &hex) { string bin; for (int i = 0; i < hex.size(); i++) { if (hex[i] == '0') bin += "0000"; if (hex[i] == '1') bin += "0001"; if (hex[i] == '2') bin += "0010"; if (hex[i] == '3') bin += "0011"; if (hex[i] == '4') bin += "0100"; if (hex[i] == '5') bin += "0101"; if (hex[i] == '6') bin += "0110"; if (hex[i] == '7') bin += "0111"; if (hex[i] == '8') bin += "1000"; if (hex[i] == '9') bin += "1001"; if (hex[i] == 'A') bin += "1010"; if (hex[i] == 'B') bin += "1011"; if (hex[i] == 'C') bin += "1100"; if (hex[i] == 'D') bin += "1101"; if (hex[i] == 'E') bin += "1110"; if (hex[i] == 'F') bin += "1111"; } while (bin[0] == '0') bin.erase(0, 1); if (bin.size() % 3) bin = string(3 - bin.size() % 3, '0') + bin; return bin; } string bin_to_oct(const string &bin) { string oct; for (int i = 0; i < bin.size(); i += 3) { string s = bin.substr(i, 3); if (s == "000") oct += "0"; if (s == "001") oct += "1"; if (s == "010") oct += "2"; if (s == "011") oct += "3"; if (s == "100") oct += "4"; if (s == "101") oct += "5"; if (s == "110") oct += "6"; if (s == "111") oct += "7"; } return oct; } void solve() { string str; cin >> str; cout << bin_to_oct(hex_to_bin(str)) << " "; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
BASIC-13 数列排序
题解
模拟。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); for (int i = 0; i < n; i++) cout << a[i] << ' '; return 0; }