1005 Spell It Right
给一个大数要求出每一位的和并以英文输出
#include <bits/stdc++.h>
using namespace std;
map<int, string> mp;
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
mp[0] = "zero";
mp[1] = "one";
mp[2] = "two";
mp[3] = "three";
mp[4] = "four";
mp[5] = "five";
mp[6] = "six";
mp[7] = "seven";
mp[8] = "eight";
mp[9] = "nine";
string s;
cin >> s;
int sum = 0;
for (auto x : s) {
sum += (int) (x - '0');
}
vector<int> nums;
if (sum == 0) nums.push_back(0);
while (sum) {
nums.push_back(sum % 10);
sum /= 10;
}
reverse(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); ++i) {
int x = nums[i];
cout << mp[x] << (i == nums.size() - 1 ? "" : " ");
}
}
1006 Sign In and Sign Out
n个人打卡上下班,问最早到的人和最晚下班的人
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct node {
string name;
int start;
int end;
};
node persons[N];
int getTime(string s) {
int hh = (s[0] - '0') * 10 + (s[1]- '0');
int mm = (s[3] - '0') * 10 + (s[4]- '0');
int ss = (s[6] - '0') * 10 + (s[7]- '0');
return hh * 3600 + mm * 60 + ss;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int n;
cin >> n;
for(int i = 0; i < n; ++ i) {
string name, tmps, tmpe;
cin >> name >> tmps >> tmpe;
persons[i].name = name;
persons[i].start = getTime(tmps);
persons[i].end = getTime(tmpe);
}
sort(persons, persons +n,[&](auto x, auto y){
return x.start < y.start;
});
cout << persons[0].name << " ";
sort(persons, persons +n,[&](auto x, auto y){
return x.end > y.end;
});
cout << persons[0].name;
}
1007 Maximum Subsequence Sum
给n个数问最大连续子区间和,但注意特判细节
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 10010;
ll a[N];
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
ll n;
cin >> n;
for (ll i = 1; i <= n; ++i) {
cin >> a[i];
}
ll pos = 1, len = 0;
ll sum = 0;
ll res = 0, resl = 1, resr = 1;
for (ll i = 1; i <= n; ++i) {
sum += a[i];
len++;
if (sum <= 0) {
pos = i + 1;
len = 0;
sum = 0;
} else {
if (sum > res) {
res = sum;
resl = pos;
resr = pos + len - 1;
}
}
}
bool flag = 1;
for(int i = 1; i<= n; ++ i) {
if(a[i]>= 0) flag = 0;
}
if(flag) cout << 0 << " " << a[1] << " " << a[n] << endl;
else if(res == 0) cout << 0 << " "<< 0 << " "<< 0 << endl;
else cout << res << " " << a[resl] << " " << a[resr] << endl;
return 0;
}
1008 Elevator
按顺序坐电梯,小模拟
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 10010;
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
ll n;
cin >> n;
int p = 0;
int ans = 5 * n;
for(int i = 1; i<= n; ++ i) {
int x;
cin >> x;
if(x < p) ans += abs(x - p) * 4;
else ans += abs(x - p) * 6;
p = x;
}
cout << ans << endl;
return 0;
}
1009 Product of Polynomials
求两个多项式的乘积,按格式输出
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 10010;
struct node {
int exp;
double num;
};
vector<node> a, b;
map<int, double> mp;
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
ll n;
cin >> n;
for (int i = 0; i < n; ++i) {
int x;
double k;
cin >> x >> k;
a.push_back({x, k});
}
cin >> n;
for (int i = 0; i < n; ++i) {
int x;
double k;
cin >> x >> k;
b.push_back({x, k});
}
for (auto[ax, ak] : a) {
for (auto[bx, bk] : b) {
mp[ax + bx] += ak * bk;
}
}
vector<node> res;
for (auto[x, k] : mp) {
if (fabs(k) < 1e-7) continue;
res.push_back({x, k});
}
reverse(res.begin(), res.end());
cout << res.size();
for (auto[x, k] : res) {
cout << " " << x << " " << fixed << setprecision(1) << k;
}
return 0;
}
1010 Radix
给两个数,给出其中一个数的进制,问这两个数是否可能相等,输出另一个数的进制。
进制问题有一个注意点就是出现的数字要比进制小
对不起我刚开始觉得进制只会有二三十,wa了发
实际上进制可能会非常大,是给出数十进制上限这种级别 1e15,只能二分
不过不二分也有24分)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 10010;
ll getNum(char ch) {
if (ch >= '0' && ch <= '9') return (ll) (ch - '0');
return (ll) (ch - 'a' + 10);
}
__int128 getDecimal(string x, ll p) {
__int128 res = 0;
ll pp = 1;
while (x.size()) {
ll num = getNum(x.back());
x.pop_back();
res += num * pp;
pp = pp * p;
}
return res;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
string x, y;
ll op, p;
cin >> x >> y >> op >> p;
if (op == 2) swap(x, y);
__int128 nummm = getDecimal(x, p);
ll mx = 0;
for (auto ch : x) {
mx = max(mx, getNum(ch));
}
if (mx >= p) {
cout << "Impossible";
return 0;
}
mx = 0;
for (auto ch : y) {
mx = max(mx, getNum(ch));
}
// bool flag = 0;
// for (ll i = max(2ll, mx + 1); i <= 1000000000; ++i) {
// __int128 tmp = getDecimal(y, i);
// if (nummm == tmp) {
// cout << i;
// flag = 1;
// break;
// }
// if (tmp > nummm || tmp < 0) break;
// }
ll l = mx + 1, r = 1e16, ans = -1;
while(l <= r) {
ll mid = (l + r) >> 1;
__int128 tmp = getDecimal(y, mid);
if(nummm <= tmp || tmp < 0) {
r = mid - 1;
if(nummm == tmp )ans = mid;
}
else {
l = mid + 1;
}
}
if (ans == -1) cout << "Impossible" << endl;
else cout << ans << endl;
return 0;
}