8 20 Smith -1 -16 8 0 0 120 39 0 John 116 -2 11 0 0 82 55(1) 0 Josephus 72(3) 126 10 -3 0 47 21(2) -2 Bush 0 -1 -8 0 0 0 0 0 Alice -2 67(2) 13 -1 0 133 79(1) -1 Bob 0 0 57(5) 0 0 168 -7 0
Josephus 5 376 John 4 284 Alice 4 352 Smith 3 167 Bob 2 325 Bush 0 0
while (scanf("%s %c %c", str, &oldchar, &newchar) == 3) /* 或!= EOF , 但前者更好 */ { ; //处理 }关于cin的返回值,其实这么说有点错误的,因为 cin是个对象,没有所谓返回值之说,一般来说其他对象重载了>>操作符,才有了所谓的返回值,很多时候 >>输入操作符返回流对象的引用,cin >> x 返回istream&,cout << x返回oostream&,暂且不说对象,就说c++的内嵌类型,如int,char之类的。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <algorithm> #include<iomanip> using namespace std; #define SIZE 1000 typedef struct _score_info { string name; int ac_num; //接受了的题目的数目 int time_score; //时间分 }S_INFO; S_INFO info[SIZE]; string score[12]; int num_of_question;//问题的数目 int num;//每次罚的分数 bool cmp(const S_INFO &l, const S_INFO &r) { if (l.ac_num == r.ac_num) { if (l.time_score == r.time_score) { return l.name < r.name; //小的在前 } else return l.time_score < r.time_score; //按照时间分来排序,小的在前 } else { return l.ac_num > r.ac_num; //按照题目数目来排序,大的在前面 } } void fill_info(int index) {//解析字符,得到信息 int sum = 0; //得到的时间总分 int count = 0; //解决的题目数 for (int i = 0; i < num_of_question; ++i) { string::size_type str_index = score[i].find('('); //找到下标 if (str_index != string::npos) { count++; string substr = score[i].substr(0, str_index); sum += atoi(substr.c_str()); substr = score[i].substr(str_index + 1, score[i].length() - (str_index + 2)); sum += atoi(substr.c_str()) * num; } else { int temp = atoi(score[i].c_str()); if (temp > 0) { sum += temp; count++; } } } info[index].ac_num = count; info[index].time_score = sum; } int main() { cin >> num_of_question >> num; int index = 0; while(cin >> info[index].name) { int sum = 0; //得到的时间总分 int count = 0; //解决的题目数 info[index].ac_num = info[index].time_score = 0; for (int j = 0; j < num_of_question; ++j) { cin >> score[j]; string::size_type str_index = score[j].find('('); //找到下标 if (str_index != string::npos) //找到了 { count++; string substr = score[j].substr(0, str_index); sum += atoi(substr.c_str()); substr = score[j].substr(str_index + 1, score[j].length() - (str_index + 2)); sum += atoi(substr.c_str()) * num; } else //没找到 { int temp = atoi(score[j].c_str()); if (temp > 0) { sum += temp; count++; } } } info[index].ac_num = count; info[index].time_score = sum; index++; } sort(info, info + index, cmp); for (int j = 0; j < index; ++j) { //printf("%-10s %2d %4d\n", info[j].name.c_str(), info[j].ac_num, info[j].time_score); cout.flags(ios::left); cout << setw(10) <<info[j].name; cout << " " << setiosflags(ios::right) << setw(2) << info[j].ac_num; cout << " " << setiosflags(ios::right) << setw(4) << info[j].time_score; cout << endl; } system("pause"); return 0; }