http://acm.hdu.edu.cn/showproblem.php?pid=1094
#include <iostream> #include <vector> int main() { using namespace std; vector<int> vecrow; vector<int> vecout; while (char c = getchar()) { if (c == '\n') /*行末 或者 空行回车结束*/ { if (vecrow.size() == 0) break; else { int sum = 0; for (auto it = vecrow.begin(); it != vecrow.end(); it++) { sum += *it; } vecout.push_back(sum); vecrow.clear(); } } else { ungetc(c,stdin); /*回退读取*/ int fornum; cin >> fornum; int rowele; for (int i = 0; i < fornum; i++) { cin >> rowele; /*最后一次 for 循环结束 ,仍剩余行末的换行符在 stdin */ vecrow.push_back(rowele); } } } if (vecout.size() != 0) { for (auto it = vecout.begin(); it != vecout.end(); it++) { cout << *it << endl; } } return 0; }
https://acm.hdu.edu.cn/showproblem.php?pid=1095
只修改上面的 if 对应的 else :
else { if (c != ' ') { ungetc(c, stdin); int rowele; cin >> rowele; vecrow.push_back(rowele); } /*ungetc(c,stdin); int fornum; cin >> fornum; int rowele; for (int i = 0; i < fornum; i++) { cin >> rowele; vecrow.push_back(rowele); }*/ }
https://acm.hdu.edu.cn/showproblem.php?pid=1096
int rownum; vector<int> vecrow; vector<int> vecout; char c = getchar(); if ( c == '\n') return 0; else { ungetc(c, stdin); cin >> rownum; if (rownum > 0) { for (int i = 0; i < rownum; i++) { int rowhead; cin >> rowhead; if (rowhead > 0) { for (int j = 0; j < rowhead; j++) { int rowele; cin >> rowele; vecrow.push_back(rowele); } int sum = 0; for (auto it = vecrow.begin(); it != vecrow.end(); it++) { sum += *it; } vecout.push_back(sum); vecrow.clear(); } else { vecout.push_back(0); } } /*打印*/ for (auto it = vecout.begin(); it != vecout.end(); it++) { cout << *it << endl; } } }
https://acm.hdu.edu.cn/showproblem.php?pid=2000
#include <iostream> #include <list> #include <vector> #include <algorithm> int main() { using namespace std; list<char> lcRow; vector<char> vcOut; while (char c = getchar()) { if (c == '\n') { if (lcRow.size() == 0) break; else { lcRow.sort(); /*排序*/ for (list<char>::iterator it = lcRow.begin(); it != lcRow.end(); it++) { vcOut.push_back(*it); vcOut.push_back(' '); } vcOut[vcOut.size() - 1] = '\n'; lcRow.clear(); } } else { lcRow.push_back(c); } } for (vector<char>::iterator it = vcOut.begin(); it != vcOut.end(); it++) { cout << *it; } }
https://acm.hdu.edu.cn/showproblem.php?pid=2001
输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。 对于每组输入数据,输出一行,结果保留两位小数。
#include <iostream> #include <vector> #include <math.h> int main( ) { using namespace std; vector<float> vcLocate; vector<float> vcOut; while (char c = getchar()) { if (c == '\n') { if (vcLocate.size() == 0) break; else //计算一组坐标 { float span = sqrt( pow( (vcLocate[0] - vcLocate[2] ), 2) + pow((vcLocate[1] - vcLocate[3]),2) ); vcOut.push_back(span); vcLocate.clear(); } } else { if (c != ' ') { ungetc(c, stdin); float element; cin >> element; vcLocate.push_back(element); } } } cout.setf(ios::fixed); cout.precision(2); for (auto it = vcOut.begin(); it != vcOut.end(); it++) { cout << *it << endl; } return 0; }
https://acm.hdu.edu.cn/showproblem.php?pid=2003
#include <iostream> #include <list> #include <vector> #include <math.h> #include <algorithm> int main() { using namespace std; vector<float> vcout; float num; char c = getchar(); if (c == '\n') return 0; else ungetc(c , stdin); while (cin >> num) { vcout.push_back(num); c = getchar(); //读取每行末尾的 换行符 c = getchar(); if (c == '\n') // 连续两个换行符 表示结束, 打印结果。 { for (auto it = vcout.begin(); it != vcout.end(); it++) { cout << fabs(*it) << endl; } } else ungetc(c, stdin); } return 0; }
https://acm.hdu.edu.cn/showproblem.php?pid=2004
int main() { using namespace std; vector<int> vcscore; char c = getchar(); if (c == '\n') return 0; else ungetc(c , stdin); int num; while (cin >> num) { vcscore.push_back(num); c = getchar(); //读取每行末尾的 换行符 c = getchar(); if (c == '\n') // 连续两个换行符 表示结束, 打印结果。 { for (auto it = vcscore.begin(); it != vcscore.end(); it++) { if (90 <= *it && *it <= 100) cout << 'A' << endl; else if (80 <= *it && *it <= 89) cout << 'B' << endl; else if (70 <= *it && *it <= 79) cout << 'C' << endl; else if (60 <= *it && *it <= 69) cout << 'D' << endl; else if (0 <= *it && *it <= 59) cout << 'E' << endl; else cout << "Score is error!" << endl; } } else ungetc(c, stdin); } return 0; }
标签:记录,int,vecrow,back,++,算法,push,include,刷题 From: https://www.cnblogs.com/Huae/p/17229633.html