算法刷题记录
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; }
输入 年月日 yy/mm/dd , 计算 dd 是当年第几天 https://acm.hdu.edu.cn/showproblem.php?pid=2005
#include <iostream> #include <vector> int main() { using namespace std; vector<int> vcdate; vector<int> vcout; int yy, mm, dd; int msd[12] = { 31,28, 31,30, 31,30,31,31,30,31,30,31}; char c = getchar(); if (c == '\n') return 0; else ungetc(c, stdin); while (cin >> yy>>c>>mm>>c>>dd) { vcdate.push_back(yy); vcdate.push_back(mm); vcdate.push_back(dd); yy % 4 == 0 ? msd[1] = 29 : msd[1] = 28; int daysum = 0; if (mm >= 2) { for (int i = 0; i < mm - 1; i++) { daysum += msd[i]; } } daysum += dd; vcout.push_back(daysum); vcdate.clear(); c = getchar(); //行末换行符 c = getchar(); if (c == '\n') //打印 { for (auto it = vcout.begin(); it != vcout.end(); it++) { cout << *it << endl; } //return 0; } else ungetc(c, stdin); } return 0; }
https://acm.hdu.edu.cn/showproblem.php?pid=2006
相比 http://acm.hdu.edu.cn/showproblem.php?pid=1094 只改:
if (vecrow.size() == 0) break; else { int sum = 1; int flag = 0; for (auto it = vecrow.begin(); it != vecrow.end(); it++) { if (*it % 2 != 0) // 奇数 { sum *= *it; flag = 1; // 标记有 奇数 } } flag ? vecout.push_back(sum) : vecout.push_back(0); // 无奇数行 记为 0 vecrow.clear(); }

#include <iostream> #include <vector> #include <list> int main() { using namespace std; vector<int> vecrow; vector<int> vecout; list<vector<int>> lviList; while (char c = getchar()) { if (c == '\n') /*行末 或者 空行回车结束*/ { if (vecrow.size() != 2) //限定每行两个 数 break; else { if (vecrow[0] <= vecrow[1] && 100<=vecrow[0] && vecrow[1]<=999) { for (int cerinum = vecrow[0]; cerinum <= vecrow[1]; cerinum++) { int a = cerinum / 100; int b = cerinum / 10 % 10; int c = cerinum % 10; //cout << "a= "<<a << "b= "<<b <<"c= "<< c << endl; if (cerinum == (a * a * a + b * b * b + c * c * c)) vecout.push_back(cerinum); } if (vecout.size() == 0) { vecout.push_back(0); } } else { cout << "输入不规范,记为0"<<endl; vecout.push_back(0); } lviList.push_back(vecout); vecout.clear(); vecrow.clear(); /*int sum = vecrow[0]; int tmpsqrt = vecrow[0]; for (int i = 0; i < vecrow[1] -1; i++) { tmpsqrt = sqrt(tmpsqrt); sum += tmpsqrt; } vecout.push_back(sum);*/ //vecrow.clear(); } } else { if (c != ' ') { ungetc(c, stdin); int rowele; cin >> rowele; vecrow.push_back(rowele); } } } if (lviList.size() != 0) { //cout.setf(ios::fixed); //cout.precision(2); for (auto oit = lviList.begin(); oit != lviList.end(); oit++ ) { for (auto it = (*oit).begin(); it != (*oit).end(); it++) { if ( *it < 100 || *it > 999) cout << "no " ; else cout << *it << ' '; } cout << endl; } } return 0; }
多项式求和 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ......

#include <iostream> #include <vector> #include <list> using namespace std; int main() { int elenum; cout << "input elements number:\n"; cin >> elenum; if (elenum <= 0) return 0; float element; vector<float> vecout; for (int i = 0; i < elenum; i++) { cin >> element; if (element > 0) { float sum = 0; int pre=1; for (float j = 1; j <= element; j++) { sum += pre*1 / j; pre *= -1; } vecout.push_back(sum); } else { vecout.push_back(0); } } for (auto it = vecout.begin(); it != vecout.end(); it++) { cout << *it << endl; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
2019-03-18 VMware卸载有残留,再安装时报错提示MSI Failed