C++
$\color{gold}{— > 蓝桥杯辅导课题解}$
思路:
$根据题意,不用显示出年月日,只需要显示出时分秒即可,毫秒也不用显示,直接舍去$
#include <iostream>
#include <climits> // LONG_LONG_MAX头文件
using namespace std;
typedef long long ll; // 由于最大为10的18次方,用long long 来存
//关于long long
// cout << LONG_LONG_MAX << endl; // long long 的最大值为:922 3372 0368 5477 5807 (10的19次方)
ll n;
int main () {
cin >> n;
n /= 1000; // 从毫秒转换成秒数
n %= 24 * 60 * 60; // 把经过了多少天的秒数去掉,剩下的就是一天之内的秒数
printf ("%02d:", n / 60 / 60); // 输出小时数
n %= 60 * 60;
printf ("%02d:", n / 60); // 输出分钟数
n %= 60;
printf ("%02d", n); // 输出秒数
return 0;
}
蓝桥杯12届(2021)C++ B组 填空题:
A.空间
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << 256 * 1024 * 1024 / 4;
return 0;
}
答案:67108864
B.卡片
法一:
#include <bits/stdc++.h>
using namespace std;
int s[15];
bool check(int x) {
while (x) {
int t = x % 10;
x /= 10;
if (-- s[t] < 0) return false;
}
return true;
}
int main() {
for (int i = 0; i < 10; i ++) s[i] = 2021;
for (int i = 1; ; i ++ ) {
if (!check(i)) {
cout << i - 1;
return 0;
}
}
return 0;
}
法二:
#include <bits/stdc++.h>
using namespace std;
int s[15];
int main() {
for (int i = 0; i < 10; i ++) s[i] = 2021;
int i;
for (i = 1; ; i ++ ) {
int j = i;
while (j) {
if (s[j % 10] > 0) s[j % 10] --;
else break;
j /= 10;
}
if (j) break;
}
cout << i - 1;
return 0;
}
答案:3181
C.直线
法一:
#include <bits/stdc++.h>
using namespace std;
const int N = 200000;
int n;
struct Line {
double k, b;
bool operator< (const Line& t) const {
if (k != t.k) return k < t.k;
return b < t.b;
}
}l[N];
int main()
{
for (int x1 = 0; x1 < 20; x1 ++ )
for (int y1 = 0; y1 < 21; y1 ++ )
for (int x2 = 0; x2 < 20; x2 ++ )
for (int y2 = 0; y2 < 21; y2 ++ )
if (x1 != x2) {
double k = (double)(y2 - y1) / (x2 - x1);
double b = y1 - k * x1;
l[n ++ ] = {k, b};
}
sort(l, l + n);
int res = 1;
for (int i = 1; i < n; i ++ )
if (fabs(l[i].k - l[i - 1].k) > 1e-8 || fabs(l[i].b - l[i - 1].b) > 1e-8)
res ++ ;
cout << res + 20 << endl;
return 0;
}
法二:
#include<bits/stdc++.h>
using namespace std;
struct Point {
double x, y;
}p[25 * 25];
map<pair<double, double>, int> line;
int main() {
int cnt = 0;
int row = 21, col = 20; // row :行 col : 列
for (int i = 0; i < row; i ++)
for (int j = 0; j < col; j ++)
p[cnt].x = i, p[cnt ++].y = j;
int linenum = row + col; // 定义直线条数
for (int i = 0; i < cnt; i ++) {
for (int j = 0; j < cnt; j++) {
if (p[i].x == p[j].x || p[i].y == p[j].y)
continue;
double k = (p[j].y - p[i].y) / (p[j].x - p[i].x);
double b = (p[i].x * p[j].y - p[j].x * p[i].y) / (p[i].x - p[j].x);
if (line[{k, b}] == 0) {
line[{k, b}] = 1;
linenum ++;
}
}
}
cout << linenum;
return 0;
}
答案:40257
D.货物摆放
法一:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll n = 2021041820210418;
ll factor[N];
ll len, ans;
int main() {
for (ll i = 1; i * i <= n; i ++) {
if (n % i == 0) {
factor[len ++] = i;
if (i != n / i) factor[len ++] = n / i;
}
}
for (int a = 0; a < len; a ++) {
for (int b = 0; b < len; b ++) {
if (factor[a] * factor[b] > n) continue;
for (int c = 0; c < len; c ++) {
if (factor[a] * factor[b] * factor[c] == n)
ans ++;
}
}
}
cout << ans;
return 0;
}
法二:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n = 2021041820210418;
vector<ll> d;
for (ll i = 1; i * i <= n; i ++ )
if (n % i == 0)
{
d.push_back(i);
if (n / i != i) d.push_back(n / i);
}
int res = 0;
for (auto a: d)
for (auto b: d)
for (auto c: d)
if (a * b * c == n)
res ++ ;
cout << res << endl;
return 0;
}
答案:2430
E.路径
#include <bits/stdc++.h>
using namespace std;
const int N = 2200, M = N * 50;
int n;
int h[N], e[M], w[M], ne[M], idx;
int q[N], dist[N];
bool st[N];
int gcd(int a, int b) // 欧几里得算法
{
return b ? gcd(b, a % b) : a;
}
void add(int a, int b, int c) // 添加一条边a->b,边权为c
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void spfa() { // 求1号点到n号点的最短路距离
int hh = 0, tt = 0;
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
q[tt ++ ] = 1;
st[1] = true;
while (hh != tt) {
int t = q[hh ++ ];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; i != -1; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[t] + w[i]) {
dist[j] = dist[t] + w[i];
if (!st[j]) { // 如果队列中已存在j,则不需要将j重复插入
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
}
int main() {
n = 2021;
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++ )
for (int j = max(1, i - 21); j <= min(n, i + 21); j ++ ) {
int d = gcd(i, j);
add(i, j, i * j / d);
}
spfa();
printf("%d\n", dist[n]);
return 0;
}
答案:10266837