要交CG的兄弟们别抄啊,撞上了严nan谁都不会放过的

好久没写博客了,这次是老师布置的作业,做出来一种,觉得写得很不好,第一种方法把情况都写死在代码里了。

上代码

#include<iostream>
#include<map>
using namespace std;
int main(){
map<char,int>mp1;
map<char,int>mp2;
while(true){
cout<<"请输入源站点、目标站点、1号站接收的接口和2号站接收的接口以空格隔开"<<endl;
char destinatios,source;
int inport1,inport2;
cin>>source>>destinatios>>inport1>>inport2;
mp1[source]=inport1;
mp2[source]=inport2;
if(mp1[destinatios]==0&&mp2[destinatios]==0){
cout<<"查表失败,向所有站点广告"<<endl;
continue;
}
if(inport1==mp1[destinatios]){
cout<<"站点1不需要转发!"<<endl;
continue;
mp1[source]=inport1;
}else cout<<"站点1要转发!"<<endl;
if(inport2==mp2[destinatios]){
cout<<"站点2不需要转发!"<<endl;
mp2[source]=inport2;
continue;
}else cout<<"站点2要转发!"<<endl;
}
return 0;
}

  

第二种就较友好点,我把网桥的数量可以自定义,我认为这样才是真正的实现了学习的需求

上代码

#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main(){
int n;
cout<<"请输入桥数,默认从左到右为0 1 2 n-1"<<endl;
cout<<"请输入源站点左右桥号、起始位置、目标位置。"<<endl;
cout<<"假设网络的两个端点都是网桥,即所有源站点均在两个网桥中间"<<endl;
cin>>n;//桥数
vector<map<char,int> >v(n);
while(true){
char destinatios,source;
int start1,start2;
cin>>start1>>start2>>source>>destinatios;
int f=0;
if(v[start1][destinatios]==0&&v[start2][destinatios]==0){
cout<<"-----------查找失败!向所有站点广播-----------"<<endl;
for(int i=0;i<=start1;i++)
v[i][source]=2;
for(int i=start2;i<n;i++)
v[i][source]=1;
continue;
}
if(v[start1][destinatios]==2){
cout<<"-----------"<<start1<<"号站点不需要转发-----------"<<endl;
f=1;
}
if(v[start2][destinatios]==1){
cout<<"-----------"<<start2<<"号站点不需要转发-----------"<<endl;
if(f==1){
cout<<"-----------目标站点就在"<<start1<<"和"<<start2<<"之间-----------"<<endl;
continue;
}
}
if(v[start1][destinatios]==1){
while(v[start1][destinatios]==1){
cout<<"-----------"<<start1<<"号站点已向左转发-----------"<<endl;
v[start1][source]=2;
start1--;
}
continue;
}
if(v[start2][destinatios]==1){
while(v[start2][destinatios]==1){
cout<<"-----------"<<start2<<"号站点已向右转发-----------"<<endl;
v[start2][source]=1;
start2--;
}
continue;
}
}
}

  

  

接下来做的是要加上时间戳,我对时间函数的调用不是很熟悉,就只是实现了这个功能,没有追求精确无误,功能没有问题,但是不是严格得按照打印输出一样显示超过五秒就删除

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <time.h>
#include<iostream>
#include<map>
#include<vector>
#include<time.h>
using namespace std;
int n;
vector<map<char,int> >v(100);
vector<map<char,int> >shijian(100);
void ff(){
for(int k=0;k<n;k++){
for(map<char,int>::iterator it=shijian[k].begin();it!=shijian[k].end();it++){
clock_t start;
start = clock();
char sou=it->first;
if(start-it->second-shijian[k][sou]>5*60){
v[k][sou]=0;
cout<<sou<<"在"<<k<<"号站点的时间大于5秒,记录已被清除"<<endl;
}
}
}
}
int main(){ cin>>n; while(true){
clock_t start;
char des,sou;
int start1,start2;
cin>>start1>>start2>>sou>>des;
int f=0;
if(v[start1][des]==0&&v[start2][des]==0){
cout<<"查找失败,向所有人广播!"<<endl;
for(int i=0;i<=start1;i++){
v[i][sou]=2;
start = clock();
shijian[i][sou]=start;
cout<<i<<"号站点"<<sou<<"的最新时间是"<<shijian[i][sou]<<endl;
}
for(int j=start2;j<n;j++){
v[j][sou]=1;
start = clock();
shijian[j][sou]=start;
cout<<j<<"号站点"<<sou<<"的最新时间是"<<shijian[j][sou]<<endl;
}
ff();
continue;
}
if(v[start1][des]==2){
cout<<"-------------"<<start1<<"号站点不需要转发----------"<<endl;
f=1;
}
if(v[start2][des]==1){
cout<<"------------"<<start2<<"号站点不需要转发------------"<<endl;
if(f==1){
cout<<"------------目标站点就在"<<start1<<"和"<<start2<<"之间----------"<<endl;
ff();
continue;
}
}
if(v[start1][des]==1){
while(v[start1][des]==1){
cout<<"----------"<<start1<<"号站点已向左转发---------"<<endl;
v[start1][sou]=2;
start = clock();
shijian[start1][sou]=start;
cout<<start1<<"号站点"<<sou<<"的最新时间是"<<shijian[start1][sou]<<endl;
start1--;
}
ff();
continue;
}
if(v[start2][des]==1){
while(v[start2][des]==1){
cout<<"----------"<<start2<<"号站点已向右转发---------"<<endl;
v[start2][sou]=1;
start = clock();
shijian[start2][sou]=start;
cout<<start2<<"号站点"<<sou<<"的最新时间是"<<shijian[start2][sou]<<endl;
start2--;
}
ff();
continue;
}
ff();
}
}

  

最新文章

  1. GJM :用JIRA管理你的项目(三)基于LDAP用户管理 [转载]
  2. 各种数据库分页sql
  3. 使用 AFNetworking 进行 XML 和 JSON 数据请求
  4. HTML基础学习笔记
  5. Unity3D ShaderLab法线贴图
  6. Gym 100507H Pair: normal and paranormal (贪心)
  7. Java网络编程(TCP客户端)
  8. Dubbo在Spring和Spring Boot中的使用
  9. 为sublime安装package control 解决乱码问题 Mac版
  10. Jekyll搭建过程详解
  11. 设置MyEclipse中代码的换行长度
  12. Windows Server 2012 R2在桌面上显示计算机/网络图标
  13. boost.asio源码阅读(2) - task_io_service
  14. lvs简单使用
  15. LeetCode算法题-Number of Lines To Write String(Java实现)
  16. python操作三大主流数据库(11)redis的安装和简单使用
  17. 爬虫系列2:scrapy项目入门案例分析
  18. [Python3]subprocess.check_output() 在python3的输出为bytes而非string,在实际使用过程中得增加一个解码过程decode(),不然会有问题
  19. 解决 vuex mapGetters 语法报错 (Unexpected token )
  20. JAVA 9 新特性

热门文章

  1. Python3+Apache+Django+CentOS
  2. 解决gitosis中authorized_keys不自动更新问题
  3. 安卓利用Handlers,AsyncTask和Loaders运行后台程序
  4. Selenium2+python自动化之数据驱动(ddt)
  5. 【LeetCode C++】Two Sum
  6. set集合排序
  7. [leetcode] 1. Valid Palindrome
  8. 20145233《网络对抗》Exp5 MSF基础应用
  9. Sublime Text 3 Settings
  10. mvc4验证码&amp;输出图像的处理方式