可以留言提意见……
2.2顺序表
#include<iostream>
#include<vector>
using namespace std;
#define N 100
#define INF 0x3f3f3f3f
int arr[N];
int top;
int n;
void show(){
for (int i = 0; i < top; i++)cout << arr[i] << " "; cout << endl;
}
int pop_min(){
int MIN = INF;
vector<int>G;
int pos = 0;
for (int i = 0; i < top; i++){
if (MIN>arr[i]){
G.clear();
G.push_back(i);
MIN = arr[i];
}
else if (arr[i] == MIN){
G.push_back(i);
}
}
for (int i = 0; i < G.size(); i++){
while (top >= 1 && arr[top - 1] == MIN){
top--;
G.pop_back();
}
if (i < G.size())
{
pos = G[i];
if (top - 1 >= 0)
{
arr[pos] = arr[top - 1];
top--;
}
}
}
n = top;
return MIN;
}
void pop_x(int x){
int cnt = 0;
for (int i = 0; i < top; i++){
if (arr[i] == x){
n--;
continue;
}
arr[cnt++] = arr[i];
}
top = n;
}
void pop_range(int s, int t){
int cnt = 0;
for (int i = 0; i < top; i++){
if (arr[i]>s&&arr[i] < t){
n--;
continue;
}
arr[cnt++] = arr[i];
}
top = n;
}
int main(){
cin >> n;
for (int i = 0; i < n; i++)cin >> arr[i];
top = n;
cout << pop_min() << endl;
show();
pop_x(5);
show();
pop_range(4, 8);
show();
}
3.不带头结点的单链表的倒置
#include<iostream>
using namespace std;
template<class T>
class LinkNode{
public:
T date;
LinkNode<T>*next;
};
template<class T>
class LinkList{
private:
int size;
LinkNode<T> *head,*tail;
public:
LinkList();
~LinkList();
void show();
int getSize();
void insertTail(const T&item);
void reverse();
};
template<class T>
LinkList<T>::LinkList(){
size = 0;
head=tail = NULL;
}
template<class T>
LinkList<T>::~LinkList(){
if (size != 0){
while (head != NULL){
LinkNode<T>*p = head->next;
delete head;
head = p;
}
}
}
template<class T>
void LinkList<T>::show(){
LinkNode<T> *p = head;
while (p != NULL){
cout << p->date << " ";
p = p->next;
}
cout << endl;
}
template<class T>
int LinkList<T>::getSize(){
return size;
}
template<class T>
void LinkList<T>::insertTail(const T&item){
LinkNode<T>*p = new LinkNode<T>();
p->date = item; p->next = NULL;
if (head == NULL){
head = tail = p;
tail->next = NULL;
}
else{
tail->next = p;
tail = p;
}
size++;
}
template<class T>
void LinkList<T>::reverse(){
if (size == 1)return;
LinkNode<T>*p, *q;
if (size == 2){
tail->next = head;
head->next = NULL;
p = head;
head = tail;
tail = p;
}
else{
p = head->next;
LinkNode<T> *next = p;
head->next = NULL;
q = head;
while (next != NULL){
next = next->next;
p->next = q;
q = p;
p = next;
}
p = head;
head = tail;
tail = p;
}
}
int main(){
LinkList<int> list;
for (int i = 0; i < 5; i++){
list.insertTail(i);
}
list.show();
list.reverse();
list.show();
cout << list.getSize() << endl;
}
12.十进制转换2到9进制
n是十进制数,rad要转换的进制类型
#include<iostream>
using namespace std;
int n, rad;
void translate(int n){
if (n == 0)return;
translate(n / rad);
cout << n % rad;
}
int main(){
while (cin >> n >> rad){
translate(n);
cout << endl;
}
}
13。匹配括号
查看ASCII表,()差为2,【】{}差为1
题看错了,代码不对
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
char str[100];
int len;
bool judge(int i){
if (i == len / 2 - 1)return true;
if (str[i]>42 && abs(str[i] - str[len - i - 1]) == 2 || abs(str[i] - str[len-i-1])==1)
return judge(i - 1);
return false;
}
int main(){
while (cin >> str){
len = strlen(str);
if (len>=2&&judge(len - 1))
cout << "YES" << endl;
else cout << "NO" << endl;
}
14.输入n个数,合并成一条链
/后来发现貌似有点问题,先不改了/
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
class node{
public:
int date;
node*next;
};
class List{
public:
node*head;
node*tail;
int size;
bool flag;
public:
bool empty();
List();
~List();
static void add(int&item);
};
bool List::empty(){
if (size)return false;
return true;
}
List::List(){
head = tail = NULL;
size = 0;
flag = false;
}
List::~List(){
if (!flag)
{
while (size){
size--;
node*p = head;
delete head;
head = p->next;
}
head = tail = NULL;
}
}
List list[11];
void List::add(int&item){
node*p = new node();
p->date = item;
p->next = NULL;
if (!list[item].head){
list[item].head = list[item].tail = p;
}
else{
list[item].tail->next = p;
list[item].tail = p;
}
list[item].size++;
}
void combine(){
for (int i = 0; i < 10; i++){
if (!list[i].empty()){
if (!list[10].head){
list[10].head = list[i].head;
list[10].tail = list[i].tail;
}
else{
list[10].tail->next = list[i].head;
list[10].tail = list[i].tail;
}
list[10].size += list[i].size;
}
}
}
void show(int i){
node*p = list[i].head;
if (!p)return;
while (p){
cout << p->date << " ";
p = p->next;
}
cout << endl;
}
int main(){
int n;
while (cin >> n){
for (int i = 0; i < n; i++)
{
int x; cin >> x; List::add(x);
}
combine();
show(10);
list[10].flag = true;
for (int i = 0; i < 10; i++)list[i].~List();
}
}
15。环形队列,不想写链表
#include<iostream>
#include<string.h>
#include<math.h>
#include<cstring>
using namespace std;
#define N 10
int front, rear;
int tag;
int circle[N];
int vis[N];
void pop(){
if (tag==0)cout << "Empty" << endl;
else{
vis[front] = 0;
front > 0 ? (--front) : (front = N - 1);
tag--;
}
}
void push(int&item){
if (tag==N)cout << "FULL" << endl;
else{
tag++;
rear>0 ? (--rear) : (rear = N - 1);
vis[rear] = 1;
circle[rear] = item;
}
}
void show(){
int i = rear;
if (tag == 0){
cout << "Empty" << endl;
return;
}
while (i != front){
cout << circle[i] << " ";
if (i + 1 == N)i = 0;
else i++;
}
cout << endl;
}
int main(){
int date;
front = rear = tag = 0;
int switcher;
cout << "输入数字对应执行函数0->pop(),1->push(),2->show()" << endl;
while (cin >> switcher){
if (switcher==1){
cin >> date;
push(date);
}
else if (switcher == 0)
pop();
else show();
}
}
2.71计算器
拒绝class
这个除法是按照c中的‘/’来做的,只取整,不会有小数产生。
水一水应该可以过。
//12.14改,原来的不能计算负数,不能算小数,orz
#include<iostream>
#include<stack>
#include<string>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100
string s;
int p = 0;
int i = 0;
stack<char>Stack;
stack<double>cal;
double read(int pos){
char s1[N]; int len1 = 0;
char s2[N]; int len2 = 0;
int op = 1;
int dou = 0;
for (i = pos; i==pos||s[i]!=')'&&i < s.size()&&s[i]!='+'&&s[i]!='-'&&s[i]!='*'&&s[i]!='/'; i++){
if (s[i] == '.')dou = 1;
else if (s[i] == '-')op = -1;
else if (dou){
s2[len2++] = s[i];
}
else s1[len1++] = s[i];
}
double res = atoi(s1);
double tem = atoi(s2);
while (tem >= 1)tem /= 10;
res += tem;
return res*op;
}
void opStack(char c){
if (Stack.empty())return;
double first = cal.top(); cal.pop();
if (Stack.empty())return;
double second = cal.top(); cal.pop();
if (c == '+')cal.push(first + second);
else if (c == '-')cal.push(second - first);
else if (c == '*')cal.push(second*first);
else if(first!=0)cal.push(second / first);
else {
cout << "error" << endl;
return;
}
}
void solve(){
int len = s.size();
for (i = 0; i < len; i++){
if (s[i] == '(')Stack.push(s[i]);
else if (s[i] == ')'){
while (!Stack.empty()){
char c = Stack.top(); Stack.pop();
if (c == '('){
break;
}
else opStack(c);
}
}
else if (s[i] >= '0'&&s[i] <= '9' || (s[i] == '-'&&(i==0||s[i-1]=='('))){
double temp = read(i); i--;
cal.push(temp);
}
else{
while (!Stack.empty() && Stack.top() != '(' && (Stack.top() == '*' || Stack.top() == '/' || s[i] == '+' || s[i] == '-')){
opStack(Stack.top()); Stack.pop();
}
Stack.push(s[i]);
}
}
while (!Stack.empty()){
opStack(Stack.top()); Stack.pop();
}
cout << cal.top() << endl;
}
void input(){
cin >> s;
}
int main(){
while (1)
{
i = 0;
while (!Stack.empty())Stack.pop();
while (!cal.empty())cal.pop();
input();
solve();
}
}
/*#include<iostream>
#include<string.h>
#include<math.h>
#include<string>
#include<stack>
#include<queue>
using namespace std;
#define N 10
string str;
stack<char> s;
queue<string>q;
bool flag;
string opera(string&a, string&b,string&op){
string temp;
int n1 = atoi(a.c_str());
int n2 = atoi(b.c_str());
int sum;
if (op == "+")
sum = n1 + n2;
else if (op == "-")
sum = n1 - n2;
else if (op == "*")
sum = n1*n2;
else {
if (n2!=0)
sum = n1 / n2;
else {
cout << "WA" << endl;
flag = true;
}
}
char str[10];
itoa(sum, str,10);
temp += str;
return temp;
}
bool translate(){
string res;
int len = str.size();
if (str[len - 1] == '=')str[len - 1] = '\0', len--;
int i = 0;
while (i < len){
string temp;
q;
if (isdigit(str[i])){
while (i < len&&isdigit(str[i])){
temp += str[i++];
}
q.push(temp);
}
else if (str[i] == '(')s.push(str[i++]);
else if (str[i] == ')'){
if (s.empty()){
return false;
}
else{
while (!s.empty() && s.top() != '('){
string temp;
temp += s.top();
q.push(temp);
s.pop();
}
if (s.empty())return false;
else s.pop();
}
i++;
}
else{
while (!s.empty() && s.top() != '(' && !((s.top() == '+' || s.top() == '-') && (str[i] == '*' || str[i] == '/'))){
string temp; temp += s.top();
q.push(temp);
s.pop();
}
s.push(str[i++]);
}
}
while (!s.empty()){
string temp;
temp = s.top();
if (temp == "(")return false;
q.push(temp);
s.pop();
}
return true;
}
void calculate(){
stack<string>Stack;
while (!q.empty()){
string temp = q.front(); q.pop();
if (isdigit(temp[0]))Stack.push(temp);
else{
string s1 = Stack.top(); Stack.pop();
string s2 = Stack.top(); Stack.pop();
Stack.push(opera(s2, s1, temp));
}
}
if (!flag)
{
cout << Stack.top() << endl;
Stack.pop();
}
}
int main(){
while (cin >> str){
while (s.empty() == false)s.pop();
while (q.empty() == false)q.pop();
flag = false;
if(translate())
calculate();
else cout << "WA" << endl;
}
}*/
2.72银行叫号
#include<iostream>
#include <queue>
#include <vector>
#include <windows.h>
using namespace std;
#define N 100
int serveTime = 4;//服务时间
int timeInterval = 1;//时间间隔
class node{
public:
int Time;
int NormalArr;
int VIPArr;
int OfficialArr;
class node(){
Time = NormalArr = VIPArr = OfficialArr = 0;
}
};
node Arr[N];
class User//用户类
{
public:
int id;//用户ID
int isWait;//等待时间
int arriveTime;//到达时间
int serveTime;//开始服务时间
char *type;//用户类型
User(){ type = NULL; }//构造函数
User(int id, int isWait, int arriveTime, int serveTime, char *type)
{
this->id = id;
this->isWait = isWait;
this->arriveTime = arriveTime;
this->serveTime = serveTime;
this->type = type;
}
void getServed()//服务完毕
{
cout << this->type << this->id << "已经被服务完毕,离开银行。" << endl;
}
};
//普通用户
class NormalUser : public User
{
public:
NormalUser(){}
NormalUser(int id, int isWait, int arriveTime, int serveTime) :User(id, isWait, arriveTime, serveTime, "普通用户")
{}
void getServed()
{
cout << this->type << this->id << "已经被服务完毕,离开银行。" << endl;
}
};
//VIP用户
class VIPUser : public User
{
public:
VIPUser() :User(){}
VIPUser(int id, int isWait, int arriveTime, int serveTime) :User(id, isWait, arriveTime, serveTime, "VIP用户"){}
void getServed()
{
cout << this->type << this->id << "已经被服务完毕,离开银行。" << endl;
}
};
//公用用户
class OfficialUser : public User
{
public:
OfficialUser() :User(){}
OfficialUser(int id, int isWait, int arriveTime, int serveTime) :User(id, isWait, arriveTime, serveTime, "公用用户"){}
void getServed()
{
cout << this->type << this->id << "已经被服务完毕,离开银行。" << endl;
}
};
//窗口类
class BankWindow
{
public:
bool isBusy;//窗口是否繁忙
int id;//窗口ID
User client;//用户
char *type;//窗口类型
BankWindow(){ type = NULL; }//构造函数
BankWindow(bool isBusy, int id, User client, char *type)
{
this->isBusy = isBusy;
this->id = id;
this->client = client;
this->type = type;
}
void HandleUser()//处理用户服务
{
cout << this->type << this->id << "正在为" << this->client.type << this->client.id << "服务" << endl;
}
};
//普通窗口
class NormalBankWindow : public BankWindow
{
public:
NormalBankWindow() :BankWindow(){}
NormalBankWindow(bool isBusy, int id, User client) :BankWindow(isBusy, id, client, "普通窗口"){}
};
//VIP窗口
class VIPBankWindow : public BankWindow
{
public:
VIPBankWindow() :BankWindow(){}
VIPBankWindow(bool isBusy, int id, User client) :BankWindow(isBusy, id, client, "VIP窗口"){}
};
//公用窗口
class OfficialBankWindow : public BankWindow{
public:
OfficialBankWindow() :BankWindow(){}
OfficialBankWindow(bool isBusy, int id, User client) :BankWindow(isBusy, id, client, "公用窗口"){}
};
//模拟类
class Simulater
{
public:
friend class node;
queue<NormalUser> NormalUserQueue;//普通用户队列
queue<VIPUser> VIPUserQueue;//VIP用户队列
queue<OfficialUser> OfficialUserQueue;//公用用户队列
vector<NormalBankWindow> nbw;//普通窗口链表
vector<VIPBankWindow> vbw;//VIP窗口链表
vector<OfficialBankWindow> obw;//公用窗口链表
Simulater(){}//构造函数
int NormalCnt;
int VIPCnt;
int OfficialCnt;
int Cnt;
bool IsServed(int time, User user)
{
if (time - user.serveTime >= serveTime)
return true;
return false;
}
//用户入队
void customerEnter(User user)
{
if (user.type == "普通用户")
{
this->NormalUserQueue.push((NormalUser&)user);
}
else if (user.type == "VIP用户")
{
this->VIPUserQueue.push((VIPUser&)user);
}
else
{
this->OfficialUserQueue.push((OfficialUser&)user);
}
}
//模拟用户进入银行
void simulaterCustomerEnter(User user)
{
this->customerEnter(user);
cout << user.type << user.id << "进入银行" << endl;
}
//模拟叫号
void simulaterCallCustomer(int time)
{
//检查普通窗口是否有空闲的
for (int j = 0; j<nbw.size(); j++)
{
//窗口空闲或者该窗口的用户刚好办完业务
if (nbw[j].isBusy&&this->IsServed(time, nbw[j].client) || !(nbw[j].isBusy))
{
//窗口的用户刚好办完业务,该用户离开银行
if (nbw[j].isBusy&&this->IsServed(time, nbw[j].client))
{
nbw[j].client.getServed();
}
nbw[j].isBusy = false;
nbw[j].client.id = -1;
//有普通用户正在排队,呼叫该用户到空闲窗口办理业务
if (!this->NormalUserQueue.empty()){
NormalUser user = this->NormalUserQueue.front();
this->NormalUserQueue.pop();
this->callCustomer(user, nbw[j], time);
}
}
//窗口正在服务用户
else{
nbw[j].HandleUser();
}
}
//检查VIP窗口是否有空闲的
for (int j = 0; j<vbw.size(); j++)
{
//窗口空闲或者该窗口的用户刚好办完业务
if (vbw[j].isBusy&&this->IsServed(time, vbw[j].client) || !(vbw[j].isBusy))
{
//窗口的用户刚好办完业务,该用户离开银行
if (vbw[j].isBusy&&this->IsServed(time, vbw[j].client))
{
vbw[j].client.getServed();
}
vbw[j].isBusy = false;
vbw[j].client.id = -1;
//没有VIP用户正在排队
if (this->VIPUserQueue.empty()){
//有普通用户正在排队
if (!this->NormalUserQueue.empty()){
NormalUser user = this->NormalUserQueue.front();
this->NormalUserQueue.pop();
this->callCustomer(user, vbw[j], time);
}
}
//有VIP用户正在排队
else{
VIPUser user = this->VIPUserQueue.front();
this->VIPUserQueue.pop();
this->callCustomer(user, vbw[j], time);
}
}
//窗口正在服务用户
else{
vbw[j].HandleUser();
}
}
//检查公用窗口是否有空闲的
for (int j = 0; j<obw.size(); j++)
{
//窗口空闲或者该窗口的用户刚好办完业务
if (obw[j].isBusy&&this->IsServed(time, obw[j].client) || !(obw[j].isBusy))
{
//窗口的用户刚好办完业务,该用户离开银行
if (obw[j].isBusy&&this->IsServed(time, obw[j].client))
{
obw[j].client.getServed();
}
obw[j].isBusy = false;
obw[j].client.id = -1;
//没有公用用户正在排队
if (this->OfficialUserQueue.empty())
{
//有普通用户正在排队
if (!this->NormalUserQueue.empty()){
NormalUser user = this->NormalUserQueue.front();
this->NormalUserQueue.pop();
this->callCustomer(user, obw[j], time);
}
}
//有公用用户正在排队
else
{
OfficialUser user = this->OfficialUserQueue.front();
this->OfficialUserQueue.pop();
this->callCustomer(user, obw[j], time);
}
}
//窗口正在服务用户
else
{
obw[j].HandleUser();
}
}
}
//显示呼叫用户
void callCustomer(User user, BankWindow &window, int time){
if (window.isBusy)
return;
else{
//请user到窗口window办理业务
window.isBusy = true;
user.serveTime = time;
user.isWait = time - user.arriveTime;
window.client = user;
cout << "请" << user.id << "号" << user.type << "到" << window.type << window.id << "办理业务" << endl;
}
}
//初始化
void Initialize()
{
//初始化三个普通窗口,1个VIP窗口,1个公用窗口
NormalUser user(-1, 0, 0, 0);
NormalBankWindow nbw1(false, 1, user);
NormalBankWindow nbw2(false, 2, user);
NormalBankWindow nbw3(false, 3, user);
VIPBankWindow vbw1(false, 4, user);
OfficialBankWindow obw1(false, 5, user);
nbw.push_back(nbw1);
nbw.push_back(nbw2);
nbw.push_back(nbw3);
vbw.push_back(vbw1);
obw.push_back(obw1);
NormalCnt = VIPCnt = OfficialCnt = 0;
Cnt = 0;
}
//操作
void Operation(){
this->Initialize();
cout << "秒: " << "普通用户数: " << "VIP用户数: " << "办公用户: " << endl;
while(cin >> Arr[Cnt].Time >> Arr[Cnt].NormalArr >> Arr[Cnt].VIPArr >> Arr[Cnt].OfficialArr)
Cnt++;
}
//模拟主函数
void simulate(){
int id = 1;
// this->Initialize();
int temp = 0;
for (int i = 0; i <= 12; i += timeInterval)
{
cout << i << "s:" << endl;
if (temp<Cnt&&i == Arr[temp].Time){
for (int j = 0; j<Arr[temp].NormalArr; j++)
{
NormalUser nu(id++, 0, i, 0);
this->simulaterCustomerEnter(nu);
}
for (int j = 0; j<Arr[temp].VIPArr; j++)
{
VIPUser vu(id++, 0, 0, 0);
this->simulaterCustomerEnter(vu);
}
for (int j = 0; j<Arr[temp].OfficialArr; j++)
{
OfficialUser ou(id++, 0, 0, 0);
this->simulaterCustomerEnter(ou);
}
temp++;
}
/* //在0s,2s,6s时,有新客户进入银行,模拟用户进入银行
if (i == 0)
{
for (int j = 0; j<3; j++)
{
NormalUser nu(id++, 0, i, 0);
this->simulaterCustomerEnter(nu);
}
VIPUser vu(id++, 0, 0, 0);
this->simulaterCustomerEnter(vu);
OfficialUser ou(id++, 0, 0, 0);
this->simulaterCustomerEnter(ou);
}
if (i == 2)
{
for (int j = 0; j<5; j++)
{
NormalUser nu(id++, 0, i, 0);
this->simulaterCustomerEnter(nu);
}
}
if (i == 6)
{
for (int j = 0; j<5; j++)
{
NormalUser nu(id++, 0, i, 0);
this->simulaterCustomerEnter(nu);
}
VIPUser vu(id++, 0, i, 0);
this->simulaterCustomerEnter(vu);
}*/
//模拟叫号
this->simulaterCallCustomer(i);
//检查是否有用户正在排队
if (NormalUserQueue.empty() && VIPUserQueue.empty() && OfficialUserQueue.empty())
{
cout << "没有用户正在排队!" << endl;
}
else
{
cout << "有" << NormalUserQueue.size() + VIPUserQueue.size() + OfficialUserQueue.size() << "个用户正在排队!" << endl;
}
//睡眠1s
// Sleep(1000);
}
}
};
int main()
{
Simulater simulater;
simulater.Operation();
simulater.simulate();
return 0;
}
16.求next数组值
0 0 0 1 1 2 0 0 1 2 3 4 5 6 0 1 2
17。字符串比较
#include<iostream>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100
#define mem(arr,a) memset(arr,a,sizeof(arr))
int Strcmp(string s, string t){
if (s == t)return 0;
else{
int len_s = s.size();
int len_t = t.size();
int len = min(len_s, len_t);
for (int i = 0; i < len; i++){
if (s[i]>t[i])return 1;
else if (s[i] < t[i])return -1;
}
if (len_s < len_t)return -1;
else return 1;
}
}
int main(){
string s, t;
cin >> s >> t;
int flag = Strcmp(s, t);
if (flag == 0)cout << "相同" << endl;
else if (flag == 1)cout << "s大" << endl;
else cout << "t大" << endl;
}
18.在str中查找substr最后出现的位置
#include<iostream>
#include<string.h>
#include<cstring>
using namespace std;
#define N 100
#define mem(arr,a) memset(arr,a,sizeof(arr))
int F[N];
char B[N];
char A[N];
int n;
int find(){
n = strlen(A);
int m = strlen(B);
int i, j;
F[0] = 0;
for (i = 1; i<m; i++)
{
j = F[i - 1];//F[i],0到i前后缀相同的长度
while (B[j] != B[i] && j>0)j = F[j - 1];
if (B[j] == B[i])F[i] = j + 1;
else F[i] = 0;
}
j = 0;
int pos = -1;
for (i = 0; i < n; i++){
while (j>0 && B[j] != A[i])j = F[j - 1];
if (A[i] == B[j])j++;
if (j == m){
j = F[j - 1];
// cout << i - m + 1 << endl;
pos = i - m + 1;
}
}
return pos;
}
int normal(string str, string substr){
int len = str.size();
int len_sub = substr.size();
int pos = -1;
for (int i = 0; i < len; i++){
int j = 0;
while (j<len_sub&&str[i+j] == substr[j]){
j++;
}
if (j == len_sub)pos = i;
}
return pos;
}
int main(){
cout << "在A中查找B" << endl;
cin >> A;
cin >> B;
int pos = find();
if (pos == -1)cout << "no find" << endl;
else cout << pos << endl;
}