1、求两个数的最大公约数
示例:
输入:24 18
输出:6
#include <iostream> #include <math.h> using namespace std; int main() { int a, b, i, j, m; while (cin >> a >> b) { m = min(a, b); j = 0; for (i = 1; i <= m; i++) { if (a % i == 0 && b % i == 0) { if (i > j) j = i; } } cout << j << endl; } return 0; }
2、输入-组英文单词,按字典顺序(大写与小写字母具有相同大小写)
排序输出.
示例:
输入: Information Info Inform info Suite suite suit
输出: Info info Inform Information suit Suite suite
#include <iostream> #include <vector> #include <map> #include <string> #include <algorithm> using namespace std; bool cmp(string s1, string s2) { for(int i = 0; i < s1.length(); i++) { s1[i] = tolower(s1[i]); } for(int i = 0; i < s2.length(); i++) { s2[i] = tolower(s2[i]); } return s1 < s2; } int main() { int num = 0; string s, str; vector<string> v, vv; map<string, int> m; while (getline(cin, s)) { for (int i = 0; i < s.length(); i++) { if (isalpha(s[i])) str += s[i]; if (s[i] == ‘ ‘ || i + 1 == s.length()) { v.push_back(str); str = ""; } } sort(v.begin(), v.end(), cmp); for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; v.clear(); } return 0; }
3、编写程序:输入表达式,输出相应二叉树的先序遍历结果(干脆中缀转前缀 后缀都给出了 ,有示例)
输入: a+b*(c- -d)-e/f
输出: -+a*b-cd/ef
infix: a+b-a*((c+d)/e-f)+g
suffix: ab+acd+e/f-*-g+
prefix: +-+ab*a-/+cdefg
#include <iostream> #include <stack> #include <map> #include <vector> #include <algorithm> using namespace std; string toSuf(string s) { map<char, int> isp, icp; //isp--in stack preority icp--in coming preority isp[‘(‘] = 1; isp[‘*‘] = 5; isp[‘/‘] = 5; isp[‘+‘] = 3; isp[‘-‘] = 3; isp[‘)‘] = 6; icp[‘(‘] = 6; icp[‘*‘] = 4; icp[‘/‘] = 4; icp[‘+‘] = 2; icp[‘-‘] = 2; icp[‘)‘] = 1; string sufstr; stack<char> opt; //operator for (int i = 0; i < s.length(); i++) { if (isalpha(s[i]) || (s[i] >= ‘0‘ && s[i] <= ‘9‘)) { sufstr += s[i]; } else if (opt.empty() || icp[s[i]] > isp[opt.top()]) { opt.push(s[i]); } else { if (s[i] == ‘)‘) { while (opt.top() != ‘(‘) { sufstr += opt.top(); opt.pop(); } opt.pop(); } else { while (!opt.empty() && isp[opt.top()] >= icp[s[i]]) { sufstr += opt.top(); opt.pop(); } opt.push(s[i]); } } } while (!opt.empty()) { sufstr += opt.top(); opt.pop(); } return sufstr; } string toPre(string s) { map<char, int> isp, icp; //isp--in stack preority icp--in coming preority isp[‘(‘] = 6; isp[‘*‘] = 4; isp[‘/‘] = 4; isp[‘+‘] = 2; isp[‘-‘] = 2; isp[‘)‘] = 1; icp[‘(‘] = 1; icp[‘*‘] = 5; icp[‘/‘] = 5; icp[‘+‘] = 3; icp[‘-‘] = 3; icp[‘)‘] = 6; string prestr; stack<char> opt; //operator for (int i = s.length() - 1; i >= 0; i--) { if (isalpha(s[i]) || (s[i] >= ‘0‘ && s[i] <= ‘9‘)) { prestr += s[i]; } else if (opt.empty() || icp[s[i]] >= isp[opt.top()]) { opt.push(s[i]); } else { if (s[i] == ‘(‘) { while (opt.top() != ‘)‘) { prestr += opt.top(); opt.pop(); } opt.pop(); } else { while (!opt.empty() && isp[opt.top()] > icp[s[i]]) { prestr += opt.top(); opt.pop(); } opt.push(s[i]); } } } while (!opt.empty()) { prestr += opt.top(); opt.pop(); } reverse(prestr.begin(), prestr.end()); return prestr; } int main() { int l; string s, sufstr, prestr; cout << "Infix: "; while (getline(cin, s)) { sufstr = toSuf(s); prestr = toPre(s); cout << "Suffix: " << sufstr << endl << "Prefix: " << prestr << endl; cout << endl << "Infix: "; } return 0; }
原文地址:https://www.cnblogs.com/ache/p/12571719.html
时间: 2024-10-20 18:45:50