Colors in Mars
简单进制转换,没什么说的。
#include"bits/stdc++.h"
using namespace std;
int main(){
int val1, val2, val3;
cin >> val1 >> val2 >> val3;
string str1 = " ", str2 = " ", str3 = " ";
char msg[] = "0123456789ABC";
str1[1] = msg[val1 % 13];
str1[0] = msg[val1 / 13];
str2[1] = msg[val2 % 13];
str2[0] = msg[val2 / 13];
str3[1] = msg[val3 % 13];
str3[0] = msg[val3 / 13];
cout << "#" << str1 << str2 << str3;
}
Median
利用2个queue来抛掉前(sum - 1) / 2个元素。 放入0x7fffffff防止越界。
#include"bits/stdc++.h"
using namespace std;
int main(){
queue <int> v1, v2;
int n1, n2, sum = 0;
cin >> n1;
sum += n1;
for(int i = 0; i < n1; ++i){
int temp;
cin >> temp;
v1.push(temp);
}
cin >> n2;
sum += n2;
for(int i = 0; i < n2; ++i){
int temp;
cin >> temp;
v2.push(temp);
}
int t = (sum - 1) / 2;
v1.push(0x7fffffff);
v2.push(0x7fffffff);
while(t--){
if(v1.front() < v2.front())
v1.pop();
else v2.pop();
}
cout << min(v1.front(), v2.front());
}
List Sorting
简单结构排序,没什么说的。
#include"bits/stdc++.h"
using namespace std;
struct stud{
int id;
char name[10];
int score;
};
stud studs[100005];
int n, acc = 0;
bool cmp1(stud a, stud b){
return a.id < b.id;
}
bool cmp2(stud a, stud b){
if(strcmp(a.name, b.name) != 0) return strcmp(a.name, b.name) < 0;
else return a.id < b.id;
}
bool cmp3(stud a, stud b){
if(a.score != b.score) return a.score < b.score;
else return a.id < b.id;
}
int main(){
scanf("%d %d", &n, &acc);
for(int i = 0; i < n; ++i)
scanf("%d %s %d", &studs[i].id, studs[i].name, &studs[i].score);
if(acc == 1)
sort(studs, studs + n, cmp1);
else if(acc == 2)
sort(studs, studs + n, cmp2);
else if(acc == 3)
sort(studs, studs + n, cmp3);
for(int i = 0; i < n; ++i)
printf("%06d %s %d\n", studs[i].id, studs[i].name, studs[i].score);
}
Travel Plan
dijkstra算法,比较模板。
#include"bits/stdc++.h"
using namespace std;
int costs[600][600];
int diss[600][600];
int oprev[600];
int cost[600];
int dis[600];
int vis[600];
void getans(int p){
if(p == -1) return;
else{
getans(oprev[p]);
if(oprev[p] != -1) cout << " ";
cout << p;
}
}
int main(){
int n, roadn, from, dist;
scanf("%d %d %d %d", &n, &roadn, &from, &dist);
memset(costs, 0x3f, 600 * 2400);
memset(diss, 0x3f, 600 * 2400);
memset(vis, 0, 2400);
memset(dis, 0x3f, 2400);
memset(oprev, -1, 2400);
memset(cost, 0x3f, 2400);
for(int i = 0; i < roadn; ++i){
int a, b, c, d;
cin >> a >> b >> d >> c;
diss[a][b] = diss[b][a] = d;
costs[a][b] = costs[b][a] = c;
}
cost[from] = 0;
dis[from] = 0;
oprev[from] = -1;
while(true){
int p = -1;
int mindis = 0x3f3f3f3f;
for(int i = 0; i < n; ++i){
if(mindis > dis[i] && !vis[i]){
p = i;
mindis = dis[i];
}
}
if(p == -1) break;
vis[p] = -1;
for(int i = 0; i < n; ++i){
if(diss[i][p] != 0x3f3f3f3f && (diss[i][p] + dis[p] < dis[i] || (diss[i][p] + dis[p] == dis[i] && costs[i][p] + cost[p] < cost[i]))){
dis[i] = diss[i][p] + dis[p];
cost[i] = costs[i][p] + cost[p];
oprev[i] = p;
}
}
}
getans(dist);
cout << " " << dis[dist] << " " << cost[dist];
}