成绩排序
#include
#include
#include
#include
using namespace std;
struct Node {
string name;
int score;
} node[500];
bool cmp(const Node& a, const Node& b) {
return a.score < b.score;
}
bool cmp1(const Node& a, const Node& b) {
return a.score > b.score;
}
int main() {
int n, type;
while (scanf("%d%d", &n, &type) == 2) {
for (int i = 0; i < n; i++) {
cin >> node[i].name >> node[i].score;
}
if (type == 1) {
stable_sort(node, node+n, cmp);
} else if(type == 0) {
stable_sort(node, node+n, cmp1);
}
for (int i = 0; i < n; i++) {
cout << node[i].name << " " << node[i].score << endl;
}
}
return 0;
}
代理服务器
#include
#include
#include
using namespace std;
int solve(char p[][16], int n, char q[][16], int m) {
int Max = -1;
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (!strcmp(p[i], q[j])) {
Max = max(j, Max);
break;
}
}
if (j == m)
return 0;
}
if (n == 1 && Max != -1) // 只有一个代理服务器,还不能解决剩下的问题
return -1;
return 1 + solve(p, n, q+Max, m-Max);
}
int main() {
int n, m;
char p[1005][16];
char q[5005][16];
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%s", p[i]);
}
cin >> m;
for (int i = 0; i < m; i++) {
scanf("%s", q[i]);
}
cout << solve(p, n, q, m) << endl;
return 0;
}
手机键盘
#include
#include
using namespace std;
int pos[26][2] = {{1,1},{1,2},{1,3},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3},
{4,1},{4,2},{4,3},{5,1},{5,2},{5,3},{6,1},{6,2},{6,3},{6,4},
{7,1},{7,2},{7,3},{8,1},{8,2},{8,3},{8,4}}; // 保存每个字母所在的键盘及其位置
int main() {
string str = "";
while (cin >> str) {
int time = 0;
for (int i = 0 ; i < str.length(); i++) {
if (i == 0) {
time += pos[str[0]-'a'][1]; // 第一个字母不需要等待,直接加按键时间
} else {
if (pos[str[i]-'a'][0] == pos[str[i-1]-'a'][0]) {
time += 2; // 等待时间
}
time += pos[str[i]-'a'][1];
}
}
cout << time << endl;
}
return 0;
}
球的半径和体积
#include
#include
#include
using namespace std;
const double PI = acos(-1);
int main() {
int cx, cy, cz, px, py, pz;
while (scanf("%d%d%d%d%d%d", &cx, &cy, &cz, &px, &py, &pz) == 6) {
double radius = sqrt((px-cx)*(px-cx) + (py-cy)*(py-cy) + (pz-cz)*(pz-cz));
double area = PI*radius*radius*radius*4/3;
printf("%.3f %.3f\n", radius, area);
}
return 0;
}
成绩排序
#include
#include
using namespace std;
struct Stu {
int id;
int score;
};
bool cmp(const Stu& a, const Stu& b) {
if (a.score == b.score)
return a.id < b.id;
return a.score < b.score;
}
int main() {
int n;
Stu stu[105];
while (cin >> n) {
for (int i = 0; i < n; i++) {
cin >> stu[i].id >> stu[i].score;
}
sort(stu, stu+n, cmp);
for (int i = 0; i < n; i++) {
cout << stu[i].id << " " << stu[i].score << endl;
}
}
return 0;
}
二叉树遍历
#include
using namespace std;
struct TreeNode {
TreeNode* left;
TreeNode* right;
char val;
TreeNode(char c) : val(c), left(NULL), right(NULL){}
};
TreeNode* createTree(string str, int & index) { // 根据前序遍历建树
TreeNode *r = NULL;
if (index < str.length() && str[index] != '#') {
r = new TreeNode(str[index]);
r->left = createTree(str, ++index);
r->right = createTree(str, ++index);
}
return r;
}
void InOrderTraverse(TreeNode* root) {
if (root == NULL)
return;
InOrderTraverse(root->left);
cout << root->val << " ";
InOrderTraverse(root->right);
}
int main() {
string str;
while (cin >> str) {
int index = 0;
TreeNode* root = createTree(str, index);
InOrderTraverse(root);
cout << endl;
}
return 0;
}
玛雅人的密码
#include
#include
#include
#include
using namespace std;
char aim[] = {'2', '0', '1', '2'};
typedef pair<string, int> _pair;
int n;
bool judge(string str) {
int len = str.length();
for (int i = 0; i < len-3; i++) {
if (str[i] == '2' && str[i+1] == '0' && str[i+2] == '1' && str[i+3] == '2')
return true;
}
return false;
}
int BFS(string str) {
queue<_pair> q;
set<string> s;
q.push(make_pair(str, 0));
while (!q.empty()) {
_pair p = q.front();
q.pop();
string tmp = p.first;
if (judge(tmp)) {
return p.second;
} else {
for (int i = 0; i < n-1; i++) {
swap(tmp[i], tmp[i+1]);
if (!s.count(tmp)) {
q.push(make_pair(tmp, p.second+1));
s.insert(tmp);
}
swap(tmp[i], tmp[i+1]);
}
}
}
return -1;
}
int main() {
while (cin >> n) {
string str;
cin >> str;
cout << BFS(str) << endl;
}
return 0;
}
求最小最大数
#include
#include
using namespace std;
int main() {
int n, x;
int mini = 1000001;
int maxi = -1000001;
while (scanf("%d", &n) != EOF) {
while (n--) {
scanf("%d", &x);
if (x < mini)
mini = x;
if (x > maxi)
maxi = x;
}
printf("%d %d\n", maxi, mini);
}
return 0;
}