?
目錄
P9240 [藍橋杯 2023 省 B] 冶煉金屬 - 洛谷 (luogu.com.cn)
?P8748 [藍橋杯 2021 省 B] 時間顯示 - 洛谷 (luogu.com.cn)
P10900 [藍橋杯 2024 省 C] 數字詩意 - 洛谷 (luogu.com.cn)
P10424 [藍橋杯 2024 省 B] 好數 - 洛谷 (luogu.com.cn)
P8754 [藍橋杯 2021 省 AB2] 完全平方數 - 洛谷 (luogu.com.cn)
P9240 [藍橋杯 2023 省 B] 冶煉金屬 - 洛谷 (luogu.com.cn)
可以設兩個數組a和b,a中的數除以區間內的數永遠等于b中的數,現在要找的就是這個區間。
a[i]/區間=b[i]可以轉化為a[i]/b[i]=區間
先找區間內最大的數,該數如果要滿足條件,那么該數就是a[i]/b[i]每一項中最小的數,遍歷一遍即可找到
再找區間內最小的數,只要最大的數依次向下減一逐個驗證即可
#include<iostream>
#include<algorithm>
#include<vector>
#include<climits>
using namespace std;
int main() {int n;cin >> n;vector<int> a(n);vector<int> b(n);int t = INT_MAX;int max = 0;for (int i =0; i < n; i++) {cin >> a[i] >> b[i];t = min(t, a[i] / b[i]);}for (int k = t; k >= 0; k--) {for (int i = 0; i < n; i++) {if (a[i] / k != b[i]) {cout << (k + 1) << " " << t << endl;return 0;}}}
}
?P8748 [藍橋杯 2021 省 B] 時間顯示 - 洛谷 (luogu.com.cn)
簡單的時間轉化計算,唯一要注意的點是怎么把0打印出來
#include<iostream>
using namespace std;
#define int long long
signed main() {int n;cin >> n;n /= 1000;n = n % (24 * 60 * 60);int hour = n / 3600;n %= 3600;int minute = n / 60;int second = n % 60;printf("%02lld:%02lld:%02lld", hour, minute, second);return 0;
}
P10900 [藍橋杯 2024 省 C] 數字詩意 - 洛谷 (luogu.com.cn)
除1以外的奇數都符合,怎么找?奇數除以2,比如13/2==6,加上1,6+7=13
偶數的話,可以分解為奇數,比如24=8*3,3是符合的,那么這個偶數也是符合的,可以用等差數列的原理來證明
除了2的倍數,逐級往下分最后只能分出來1,但1是不符合的
所以問題轉變成了找2的倍數有多少個
這里提供一種方法,2的倍數(假設為t)轉換成2進制后只有一個1,其它位置都是0,所以只要判斷(t & (t - 1)是否等于0,等于的話就是2的倍數
#include <iostream>
#define int long long
using namespace std;
signed main() {int n, t;cin >> n;int count = 0;for (int i = 0; i < n; i++) {cin >> t;if ((t & (t - 1)) == 0) count++;}cout << count << endl;return 0;
}
P10424 [藍橋杯 2024 省 B] 好數 - 洛谷 (luogu.com.cn)
感覺在函數中用while循環是最方便的
#include<iostream>
using namespace std;
bool jud(int n) {//奇數false,偶數truebool flag = false;while (n) {if (flag) {//偶數位是奇數則不是好數if ((n % 10) % 2 != 0)return false;}else {//奇數位是偶數則不是好數if ((n % 10) % 2 == 0)return false;}n /= 10;flag = !flag;}return true;
}
int main() {int n;cin >> n;int count = 0;for (int i = 1; i <= n; i++) {if (jud(i))count++;}cout << count << endl;return 0;
}
P8754 [藍橋杯 2021 省 AB2] 完全平方數 - 洛谷 (luogu.com.cn)
#include <iostream>
using namespace std;
#define int long long
signed main() {int n;cin >> n;int res = 1;for (int i = 2; i * i <= n; i++) {int count = 0;while (n % i == 0) {count++;n /= i;}if (count % 2 == 1) res *= i;}if (n > 1) res *= n;cout << res;return 0;
}
?