今日考完,感慨良多,我还需要不断提高个人能力T T
*原题待后补。
【模仿Excel表】
我的思路是将字符串转换成数字,再相减,注意题目要求是两列中间有多少列,所以应再减1。
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
using namespace std;
int change(string a) {
int r = 0;
for (int i = 0; i < a.length(); i++) {
r += r * 26 + a[i] - 'A';
}
return r;
}
int main() {
string s1, s2;
cin >> s1 >> s2;
int res = 0;
int len1 = s1.length();
int len2 = s2.length();
int d = abs(change(s1) - change(s2)) - 1;
cout << d << endl;
system("PAUSE");
return 0;
}
【学生分数排序】
记一次惨痛的教训,变量名一定不能记错!!考试时将res错写成下面的stu,导致结果测试错误,痛失60元。
for(int i = 0; i < cnt; i++) {
cout << stu[i].xuehao << " ";
cout << stu[i].sum << endl;
}
修改代码如下:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
#define MAX 1000
typedef struct student{
string xuehao;
int score[5] = {0};
int sum = 0;
}student;
int cmp(student a, student b){
if(a.sum != b.sum)
return a.sum > b.sum;
else {
if(a.score[2] != b.score[2])
return a.score[2] > b.score[2];
else if(a.score[1] != b.score[1])
return a.score[1] > b.score[1];
else if(a.score[0] != b.score[0])
return a.score[0] > b.score[0];
else
return a.xuehao < b.xuehao;
}
}
int main() {
int n;
cin >> n;
student stu[MAX];
student res[MAX];
int cnt = 0;
for (int i = 0; i < n; i++) {
cin >> stu[i].xuehao;
bool flag = true;
for(int j = 0; j < 3; j++) {
cin >> stu[i].score[j];
stu[i].sum += stu[i].score[j];
if(stu[i].score[j] < 80) {
flag = false;
}
}
if (flag == true){
res[cnt] = stu[i];
cnt++;
}
}
sort(res, res + cnt, cmp);
if(cnt == 0)
cout << "NULL" << endl;
for(int i = 0; i < cnt; i++) {
cout << res[i].xuehao << " ";
cout << res[i].sum << endl;
}
return 0;
}