目录
6、向量
1)完数与盈数(清华大学复试上机题)
7、队列
1)约瑟夫问题 No. 2
8、栈
1)简单计算器(浙江大学复试上机题)
2)堆栈的使用(吉林大学复试上机题)
3)计算表达式(上海交通大学复试上机题)
6、向量
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> myVector;
for(int i = 0;i < 5;i++){
myVector.push_back(i);
}
//头部插入3个15
myVector.insert(myVector.begin(), 3, 15);
myVector.pop_back();
for(int i = 0;i < myVector.size();i++){
cout<<myVector[i]<<" ";
}
cout<<endl;
cout<<"The 5th element of myVector:"<<myVector[4]<<endl;
cout<<myVector.size()<<endl;
//删除第5后序的元素
myVector.erase(myVector.begin() + 5, myVector.end());
//定义迭代器
vector<int>::iterator it;
for(it = myVector.begin();it != myVector.end();it++){
cout<<*it<<endl;
}
myVector.clear();
return 0;
}
1)完数与盈数(清华大学复试上机题)
题目描述:
一个数如果恰好等于它的各个因子(该数本身除外)之和,如 6 = 3 + 2 + 1,那么称该数为“完数”;若因子之和大于该数,则称其为“盈数”。求出 2 到 60 之间的所有“完数”和“盈数”。
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> E;
vector<int> G;
for(int i = 2;i <= 60;i++){
int sum = 0;
for(int j = 1;j < i;j++){
if(i % j == 0) sum += j;
}
if(sum == i) E.push_back(i);
else if(sum > i) G.push_back(i);
}
cout<<"E: ";
for(int i = 0;i < E.size();i++)
cout<<E[i]<<" ";
cout<<endl;
cout<<"G: ";
for(int i = 0;i < G.size();i++)
cout<<G[i]<<" ";
return 0;
}
7、队列
#include <bits/stdc++.h>
using namespace std;
int main(){
queue<int> q;
for(int i = 0;i < 10;i++)
q.push(i);
cout<<q.front()<<endl;
cout<<q.back()<<endl;
cout<<q.size()<<endl;
int sum = 0;
while(!q.empty()){
sum += q.front();
q.pop();
}
cout<<sum<<endl;
if(q.empty()){
cout<<"myQueue is empty"<<endl;
}
return 0;
}
1)约瑟夫问题 No. 2
题目描述:
n 个小孩围坐成一圈,并按顺时针编号为1, 2,..., n ,从编号为 p 的小孩顺时针依次报数,由1报到 m,报到 m 时,这名小孩从圈中出去;然后下一名小孩再从 1 报数,报到m 时再出去。以此类推,直到所有小孩都从圈中出去。请按出去的先后顺序输出小孩的编号。
#include <bits/stdc++.h>
using namespace std;
int main(){
//n个人,从编号p开始,以m为周期
int n,p,m;
queue<int> q;
while(cin>>n>>p>>m){
if(n == 0 && p == 0 && m == 0) break;
int t;
for(int i = 1;i <= n;i++)
q.push(i);
//从编号p开始
for(int i = 1;i < p;i++){
t = q.front();
q.pop();
q.push(t);
}
//action
while(!q.empty()){
for(int j = 1;j < m;j++){
t = q.front();
q.pop();
q.push(t);
}
t = q.front();
q.pop();
cout<<t;
if(q.size() != 0) cout<<",";
}
}
return 0;
}
8、栈
#include <bits/stdc++.h>
using namespace std;
int main(){
stack<int> s;
for(int i = 0;i < 10;i++)
s.push(i);
cout<<s.top()<<endl;
cout<<s.size()<<endl;
int sum = 0;
while(!s.empty()){
sum += s.top();
s.pop();
}
cout<<sum<<endl;
if(s.empty())
cout<<"myStack is empty"<<endl;
return 0;
}
1)简单计算器(浙江大学复试上机题)
题目描述:
读入一个只包含+、-、*、/的非负整数计算表达式,计算该表达式的值。
输入描述:
测试输入包含若干测试用例,每个测试用例占一行,每行不超过 200 个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有 0 时输入结束,相应的结果不要输出。
输出描述:
对每个测试用例输出一行,即该表达式的值,精确到小数点后 2 位。
#include <bits/stdc++.h>
using namespace std;
//优先级
int priority(char c){
if(c == '#') return 0;
else if(c == '$') return 1;
else if(c == '+' || c == '-') return 2;
else return 3;
}
//获得下个数字
double getNumber(string str, int &index){
double number = 0;
while(isdigit(str[index])){
number = number * 10 + str[index] - '0';
index++;
}
return number;
}
double calculate(double x, double y, char op){
double result = 0;
if(op == '+') result = x + y;
else if(op == '-') result = x - y;
else if(op == '*') result = x * y;
else if(op == '/') result = x / y;
return result;
}
int main(){
string str;
while(getline(cin,str)){
if(str == "0") break;
int index = 0;
stack<char> oper;
stack<double> data;
str += '$';
oper.push('#');
while(index < str.size()){
if(str[index] == ' ') index++;
else if(isdigit(str[index])) data.push(getNumber(str,index));
else{
if(priority(oper.top()) < priority(str[index])){
oper.push(str[index]);
index++;
}else{
double y = data.top();
data.pop();
double x = data.top();
data.pop();
data.push(calculate(x,y,oper.top()));
oper.pop();
}
}
}
printf("%.2f\n",data.top());
}
return 0;
}
2)堆栈的使用(吉林大学复试上机题)
题目描述:
堆栈是一种基本的数据结构。堆栈具有两种基本操作方式:push 和 pop。push 将一个值压入栈顶,pop 将栈顶的值弹出。现在我们来验证一下堆栈的使用。
#include <bits/stdc++.h>
using namespace std;
int main(){
stack<int> s;
int n,t;
char ch;
while(cin>>n){
for(int i = 0;i < n;i++){
cin>>ch;
if(ch == 'A'){
if(s.empty()) cout<<"E"<<endl;
else cout<<s.top()<<endl;
}
if(ch == 'P'){
cin>>t;
s.push(t);
}
if(ch == 'O' && !s.empty()){
s.pop();
}
}
}
return 0;
}
3)计算表达式(上海交通大学复试上机题)
题目描述:
对于一个不存在括号的表达式进行计算。
#include <bits/stdc++.h>
using namespace std;
int getLevel(char ch){
int level;
if(ch == '#') level = 0;
else if(ch == '$') level = 1;
else if(ch == '+' || ch == '-') level = 2;
else if(ch == '*' || ch == '/') level = 3;
return level;
}
double calculate(double x, double y,char ch){
double res;
if(ch == '+') res = x + y;
else if(ch == '-') res = x - y;
else if(ch == '*') res = x * y;
else if(ch == '/') res = x / y;
return res;
}
int main(){
string str;
while(getline(cin,str)){
str += '$';
int index = 0;
stack<double> num;
stack<char> op;
op.push('#');
while(index < str.length()){
if(isdigit(str[index])){
double number = 0;
while(isdigit(str[index])){
number = number * 10 + str[index] - '0';
index++;
}
num.push(number);
}else{
char ch = str[index];
if(getLevel(op.top()) < getLevel(ch)){
op.push(ch);
index++;
}else{
double y = num.top();
num.pop();
double x = num.top();
num.pop();
double res = calculate(x,y,op.top());
num.push(res);
op.pop();
}
}
}
cout<<num.top()<<endl;
}
return 0;
}