1001 A+B Format (20 分)
字符串处理,把整数转回字符串有简便方法需要记录,然后处理符号和逗号
#include<bits/stdc++.h>
using namespace std;
void s2i(string s, int &n) {
stringstream ss;
ss<<s;
ss>>n;
}
void i2s(string &s, int n) {
stringstream ss;
ss<<n;
ss>>s;
}
int main() {
int a,b;
cin >> a >> b;
int sum = a+b;
string s,t;
i2s(s, sum);
int l = s.length();
int i, cnt = 0;
if(sum >= 0) i = 0;
else i = 1;
for(int j = l-1; j >= i; j--,cnt++) {
if(cnt && cnt % 3 == 0) t += ',';
t += s[j];
// cout<<t<<endl;
}
if(sum < 0) t += '-';
reverse(t.begin(), t.end());
cout<<t;
return 0;
}
1002 A+B for Polynomials (25 分)
注意点一:只保留非零项,有可能两项相加后系数为0的项,记得去除(10分)
注意点二:保留一位小数(2分)
#include<bits/stdc++.h>
using namespace std;
map<int,double> mp;
set<pair<int,double> >st;
int main() {
int n,cnt = 2;
while(cnt--) {
cin >> n;
for(int i = 0; i < n; i++) {
int a; double b;
cin >> a >> b;
mp[a] += b;
}
}
for(map<int,double>::reverse_iterator it = mp.rbegin(); it != mp.rend(); it++) {
if((*it).second)st.insert(make_pair(-(*it).first, (*it).second));
}
cout<<st.size();
for(set<pair<int,double> >::iterator it = st.begin(); it != st.end(); it++) {
printf(" %d %.1lf",-(*it).first, (*it).second);
}
return 0;
}
/*
2 1 2.44 0 3.2
2 2 1.57 1 0.57
*/
1003 Emergency (25 分)
dfs模板题了属于是,也可以理解为栈或队列(队列就是bfs)的处理,找一共有几条最短路并且记录沿途的队伍数
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5005;
int ver[N],head[N],nxt[N],edge[N],tot,team[N];
bool vis[N];
int n,m,st,ed;
ll ans = 0x9f9f9f9f;
int num, teams;
void add(int x, int y, int z) {
ver[++tot] = y, edge[tot] = z;
nxt[tot] = head[x], head[x] = tot;
}
void dfs(int cur, int cost, int t) {
if(cost > ans) return;
if(cur == ed) {
if(ans == cost) {
num++;
teams = max(teams, t);
}
else{
num = 1;
ans = cost;
teams = t;
}
return;
}
for(int i = head[cur] ; i; i = nxt[i]) {
int y = ver[i], z = edge[i];
if(vis[y]) continue;
vis[y] = 1;
dfs(y, cost+z, t+team[y]);
vis[y] = 0;
}
return;
}
int main() {
cin >> n >> m >> st >> ed;
for(int i = 0; i < n; i++) cin >> team[i];
for(int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
add(x, y, z);
add(y, x, z);
}
dfs(st, 0, team[st]);
cout<<num<<" "<<teams;
return 0;
}
1004 Counting Leaves (30 分)
这个代码默认所有数字都是按顺序输进来的,事实证明也确实是这样,我写的另一个考虑顺序的反而不对,不知道为什么错了。。。
链式前向星存储这棵树,然后再顺着从根结点遍历一遍算一下该节点所在的层数,最后统计答案啊,因为n很小吗,这样写也没什么问题,完全不用担心超时。
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
int tot = 0, ver[N] = {0}, head[N] = {0}, nxt[N] = {0},cen[N];
struct node{
int d, floor;
};
void add(int x, int y) {
ver[++tot] = y;
nxt[tot] = head[x], head[x] = tot;
}
int main() {
int n, m;
cin >> n;
if(n) {
cin >> m;
tot = 0, ver[N] = {0}, head[N] = {0}, nxt[N] = {0}, cen[N] = {0};
node nodes[N];
for(int i = 0; i < m; i++) {
int a,b,k;
cin >> a >> k;
for(int j = 0; j < k; j++) {
cin >> b;
add(a,b);
}
}
nodes[1].floor = 0;
int ma = -1;
for(int i = 1; i <= n; i++) {
// cout<<i<<":";
if(head[i] == 0) nodes[i].d = 1;
else {
nodes[i].d = 0;
for(int j = head[i]; j; j = nxt[j]) {
int y = ver[j];
// cout<<y<<" ";
nodes[y].floor = nodes[i].floor + 1;
}
}
// cout<<endl;
}
for(int i = 1; i <= n; i++) {
ma = max(ma, nodes[i].floor);
if(nodes[i].d) cen[nodes[i].floor]++;
}
for(int i = 0; i <= ma; i++) {
if(i) cout<<" ";
cout<<cen[i];
}
}
return 0;
}
/*
9 4
01 3 02 03 04
02 2 05 06
03 2 07 08
04 1 09
*/
1005 Spell It Right (20 分)
字符串处理罢了
#include<bits/stdc++.h>
using namespace std;
string num[15] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
void i2s(int n, string &s) {
stringstream ss;
ss << n;
ss >> s;
}
int main() {
string s,t;
cin >> s;
int sum = 0;
for(int i = 0; i < s.length(); i++) {
sum += s[i]-'0';
}
i2s(sum, t);
for(int i = 0; i < t.length(); i++) {
if(i) cout<<" ";
int ind = t[i] - '0';
cout<<num[ind];
}
return 0;
}
1006 Sign In and Sign Out (25 分)
没啥好说的,string直接比较得结果
#include<bits/stdc++.h>
using namespace std;
int main() {
string in = "9999", out = "0000", pin, pout;
string a,b,c;
int m;
cin >> m;
for(int i = 0; i < m; i++) {
cin >> a >> b >> c;
if(b < in) pin = a, in = b;
if(c > out) pout = a, out = c;
}
cout<<pin<<" "<<pout;
return 0;
}
1007 Maximum Subsequence Sum (25 分)
求前缀和,只需要着重关注前缀和最小的位置,最后求出的和最大的序列s[i-j]一定在s[1-j]中i是前缀和最小的位置,所以答案一定是某个阶段的最小前缀和位置,只要一次用序列中每个数字减去当前序列中的最小前缀和就可以求出答案,并且每次求出更大的序列和就保存位置
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int arr[N],pre[N];
const int INF = 0x3f3f3f3f;
int main() {
int k;
cin >> k;
int mi = 0, map, mip, p, ans = -INF;
for(int i = 1; i <= k; i++) {
cin >> arr[i];
pre[i] = pre[i-1] + arr[i];
int temp = pre[i] - mi;
if(temp > ans) {
ans = temp;
map = i;
mip = p;
}
if(mi > pre[i]) {
mi = pre[i];
p = i;
}
}
if(ans < 0) cout<<"0 "<<arr[1]<<" "<<arr[k];
else cout<<ans<<" "<<arr[mip+1]<<" " <<arr[map];
return 0;
}
/*
12
-10 1 2 3 4 -51 4 12 -23 3 8 -21
4
-1 -2 -3 -4
*/
1008 Elevator (20 分)
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int now = 0, total = 0;
for(int i = 0; i < n; i++) {
int f;
cin >> f;
if(f > now) total += (f - now)*6 + 5;
else total += (now - f)*4 + 5;
now = f;
}
cout<<total;
return 0;
}
1009 Product of Polynomials (25 分)
参考1002,就是map的处理,k都<10,所以直接嵌套两层循环即可
#include<bits/stdc++.h>
using namespace std;
map<int,double> mp1, mp2;
set<pair<int,double> >st;
int main() {
int n,m;
cin >> n;
for(int i = 0; i < n; i++) {
int a; double b;
cin >> a >> b;
mp1[a] = b;
}
cin >> m;
for(int i = 0; i < m; i++) {
int a,b; double c,d;
cin >> a >> c;
for(auto it : mp1) {
b = a + it.first;
d = c * it.second;
mp2[b] += d;
}
}
for(map<int,double>::reverse_iterator it = mp2.rbegin(); it != mp2.rend(); it++) {
if((*it).second)st.insert(make_pair(-(*it).first, (*it).second));
}
cout<<st.size();
for(set<pair<int,double> >::iterator it = st.begin(); it != st.end(); it++) {
printf(" %d %.1lf",-(*it).first, (*it).second);
}
return 0;
}
1010 Radix (25 分)
有一个点必须二分处理,不二分查找必超时,其他没啥,也就是处理字符串,但是我写的很复杂(垃圾代码)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void s2i(ll &n, string s) {
stringstream ss;
ss << s;
ss >> n;
}
string radix_to(string s, int a, int b) {//从a进制转换为b进制
ll num = 0, l = s.length(), c, d = 1;
string t,p;
for(int i = l-1; i >= 0; i--) {
if(s[i] >= 'a') c = s[i] - 'a' + 10;
else c = s[i] - '0';
num = num + d * c;
d = d * a;
}
if(num == 0) t = "0";
else {
while(num) {
c = num % b;
if(c > 9) t += c - 10 + 'a';
else t += c + '0';
num /= b;
}
reverse(t.begin(), t.end());
}
return t;
}
int cmp(string a, string b){
if(a.length() > b.length()) return 1;
else if(a.length() < b.length()) return -1;
int l = a.length();
for(int i = 0; i < l; i++) {
if(a[i] < b[i]) return -1;
else if(a[i] > b[i]) return 1;
}
return 0;
}
int main() {
string a,b,t,last=",";
int tag, r;
cin >> a >> b >> tag >> r;
if(tag == 2) t = a, a = b, b = t;
string t1 = radix_to(a, r, 10);
// cout<<"a:"<<t1<<endl;
char ch = '0';
int ma;
for(int i = 0; i < b.length(); i++) {
ch = max(ch, b[i]);
}
if(ch >= 'a') ma = ch-'a' + 10;
else ma = ch - '0';
ma = max(ma+1, 2);
ll low = ma, high;
s2i(high, t1);
high = max(high, low);
while(low <= high) {
ll mid = (low + high) / 2;
t = radix_to(b, mid, 10);
int f = cmp(t1, t);
if(f < 0) {
high = mid - 1;
}
else if(f == 0) {
cout<<mid;
return 0;
}
else low = mid + 1;
}
cout<<"Impossible";
return 0;
}