计算机思维week2 实验题

化学:无序烷烃基

这道题貌似可以用以前的无向图来写,但是泄题的时候没有想到,导致修改了很多次也比较麻烦,这道题判断的时候主要是判断所有点中那个邻接点数最多的点的邻接点数,当为3的时候,判断最大邻接点的那个点邻接的三个点的各自的邻接个数。先创建两个数组来分别存储图和邻接点的个数,还有一个判断是否来过,这里写复杂了,我好像没有用无向图,加大了难度。

#include<iostream>
#include<string.h>
using namespace std;

int main()
{
   int n;
   cin>>n;
   int T[7][7],now[7][1];
   bool vis[7][7];
   for(int i=0;i<n;i++)
   {
   	int a=0;
   	memset(T,0,sizeof(T));
   	memset(vis,false,sizeof(vis));
   	memset(now,0,sizeof(now));
   		int x,y;
   		for(int t=0;t<5;t++)
   		{
   			cin>>x>>y;
   			T[x][y]=1;
		   }
	   int max=0,temp,ans=0;
	   for(int t=1;t<=6;t++)
	   {
	   	 for(int r=1;r<7;r++)
	   	 {
	   	 	if((T[t][r]==1||T[r][t]==1)&&vis[t][r]==false)
	   	 	{
	   	 		vis[t][r]=true,vis[r][t]=true;
			    now[t][0]++;
			    now[r][0]++;
		//	    cout<<now[t][0]<<" "<<now[r][0]<<" "<<t<<" "<<r<<endl;
				}
			}
			if(now[t][0]==3&&now[t][0]==max)
			{
				ans=1;
			}
			if(now[t][0]>max)
			{
				max=now[t][0];
			//	cout<<"  dsd dsd "<<t<<endl;
				temp=t;
			}
			
		//	now=0;
	   }
	   memset(vis,false,sizeof(vis));//因为多组数据所以要重置数组
	   if(max==2)//下面是进行判断输出
	   {
	   	cout<<"n-hexane"<<endl;
	   	continue;
	   }
	   if(max==3)
	   {
	   	 if(ans==1)
	   	 {
	   	 	cout<<"2,3-dimethylbutane"<<endl;
	   	 	ans=0;
	   	 	continue;
			}
		int gg[7];
		memset(gg,0,sizeof(gg));//重置数组
		int e=0,w=0;
		for(int u1=1;u1<=6;u1++)
		{
			if(T[temp][u1]==1||T[u1][temp]==1)
			{
				gg[u1]=u1;
			}
		}
		int u2=0;
        while(u2<=6)
        {
        	u2++;
        	if(gg[u2]==u2)
        	{
        		for(int p=1;p<=6;p++)
        		{
        			if(T[u2][p]==1||T[p][u2]==1&&(vis[u2][p]==false||vis[p][u2]==false))
        			{
        				vis[u2][p]=true;
        				vis[p][u2]=true;
        			//	cout<<"    "<<u2<<"   "<<p<<endl;
        			   e++;	
        			   
        			   //cout<<"   e e e e e"<<e<<endl;
					}
				}
			}
		}
		if(e==4)
		{
			cout<<"2-methylpentane"<<endl;
			continue;
		}
		if(e==5)
		
		{
			cout<<"3-methylpentane"<<endl;
			continue;
		}
	   }
	   if(max==4)
	   {
	   	cout<<"2,2-dimethylbutane"<<endl;
	   }
    }	
}

OJ提交问题

这道题也不难,但是码力太弱也是磨蹭了好久才写出来。
不过也学到了好多新的,atoi字符串转数字,cout.unsetf()来取消自动的向右对齐**(如果不用的话在最后的循环输出中设置的向左对齐输出只会在第一遍起到作用,在后面的循环中会失去作用,向右对齐)**。其他的就简单了,因为我用的是字符串数组来存放输入的各题所用的时间,所以我写了一个转化函数ju将输入的不规范时间字符串转化为规范的数字,然后用sort函数进行排序

#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<iomanip>
using namespace std;
int n,m,q=0;
int tg[1005];
struct s
{
	int x=0,y;string z;
}AA[1005];
int cmp(s dd,s cc)
{
	return{(dd.x>cc.x)||((dd.x==cc.x)&&(dd.y<cc.y))||(dd.x==cc.x&&dd.y==cc.y&&(dd.z<cc.z))};
}
int ju(string a,int i1)//返回时间 
{
    if(a[0]=='-'||a[0]=='0')
    {
    	return 0;
	}
	else
	{
		AA[i1].x++;
			for(int i=0;i<a.length();i++)
	{
		if(a[i]=='(')
		{
			string b="";
			for(int t=0;t<i;t++)
			{
				b=b+a[t];
			}
			string c="";
			for(int t=i+1;t<a.length();t++)
			{
				if(a[t]==')')
				{
					break;
				}
				c=c+a[t];
			}
			int result=atoi(b.c_str())+(atoi(c.c_str()))*m;
			//cout<<result<<"  sds"<<endl;
			return result;
		}
	}
	return atoi(a.c_str());
	}

}

int main()
{
	cin>>n>>m;
    string A[1005];
    //string B[1005];
    string B[1005][n];
    int C[1005];
    memset(C,0,sizeof(C));
    int i=0;
    while(cin>>A[i])
    {
    	AA[i].z=A[i];
    	for(int t=0;t<n;t++)
    	{
    		cin>>B[i][t];
    		C[i]=ju(B[i][t],i)+C[i];
		}
		AA[i].y=C[i];
		i++;
	}
	sort(AA,AA+i,cmp);
	//i=0;
	for(int i1=0;i1<i;i1++)
	{
		cout.unsetf(ios::right);
	   cout<<setiosflags(ios::left)<<setw(10)<<AA[i1].z<<" ";
	   cout<<setiosflags(ios::right)<<setw(2)<<AA[i1].x<<" ";
	   cout<<setiosflags(ios::right)<<setw(4)<<AA[i1].y<<endl;
	}
}

扑克牌问题

这道题是真的体现出了我的码力的蒟蒻程度,明明很简答的问题被我写的很复杂,最后都没心情改了,很多重复的代码都可以放在一个循环里面,可是前面再循环的时候忘了把后面的代码放进去,有重写了四遍,真的是犯傻了。但是这道题又很简答,没啥说的买就是稍微麻烦点。

#include<iostream>
#include<algorithm>
using namespace std;
struct s
{
	char x;
	char y;
}A[1005];
int cmp(s dd, s cc)
{
	int x1=dd.x-'0',x2=cc.x-'0',y1=dd.y-'0',y2=cc.y-'0';
	if(x1<x2)
	{
		return 1;
	}
	if(x1==x2&&y1<y2)
	{
		return 1;
	}
	return 0;
//	return{ (x1< x2) || ((x1 == x2) && (y1< y2)) };
}
int main()
{
	char a;
	while (cin >> a && a != '#')
	{
		string b, c;
		cin >> b;
		cin >> c;
		b = b + c;
		int u = 0;
		int t,t1;
		if(a=='E')
		{
			t1=t=0;
		}
		if(a=='S')
		{
			t1=t=6;
		}
		if(a=='W')
		{
			t1=t=4;
		}
		if(a=='N')
		{
			t1=t=2;
		 } 
		 	while (u < 13)
			{
				if (b[t] == 'S')
				{
					A[u].y=b[t+1];
					A[u].x = 'E';
					if (b[t + 1] == 'T')
					{
						A[u].y = 'A';
					}
					else if (b[t + 1] == 'J')
					{
						A[u].y = 'B';
					}
					else if (b[t + 1] == 'Q')
					{
						A[u].y = 'C';
					}
					else if (b[t + 1] == 'K')
					{
						A[u].y = 'D';
					}
					else if(b[t+1]=='A')
					{
						A[u].y = 'E';
					}
					u++;
					t = t + 8;
				}
				else
				{
					A[u].x = b[t];
					A[u].y = b[t + 1];
					if (b[t + 1] == 'T')
					{
						A[u].y = 'A';
					}
					else if (b[t + 1] == 'J')
					{
						A[u].y = 'B';
					}
					else if (b[t + 1] == 'Q')
					{
						A[u].y = 'C';
					}
					else if (b[t + 1] == 'K')
					{
						A[u].y = 'D';
					}
					else if (b[t + 1] == 'A')
					{
						A[u].y = 'E';
					}
					
					t = t + 8;
					u++;
				}
			}
			sort(A, A + 13, cmp);
			for (int p = 0; p < 13; p++)
			{
				if (A[p].x == 'E')
				{
					A[p].x = 'S';
				}
				if (A[p].y == 'A')
				{
					A[p].y = 'T';
				}
				else if (A[p].y == 'B')
				{
					A[p].y = 'J';
				}
				else if (A[p].y == 'C')
				{
					A[p].y = 'Q';
				}
				else if (A[p].y == 'D')
				{
					A[p].y = 'K';
				}
				else if (A[p].y == 'E')
				{
					A[p].y = 'A';
				}
			//	else 
			}
			cout << "South player:" << endl;
			cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
			int y = 0;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "|" << A[i].y << " " << A[i].y << "|";
				}
				else
				{
					cout << A[i].y << " " << A[i].y << "|";
				}
			}
			cout << endl;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "| " << A[i].x << " |";
				}
				else
				{
					cout << " " << A[i].x << " |";
				}
			}
			cout << endl;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "|" << A[i].y << " " << A[i].y << "|";
				}
				else
				{
					cout << A[i].y << " " << A[i].y << "|";
				}
			}
			cout << endl;
			cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
			  t=(t1+2)%8;
			  t1=t1+2;
			  u=0;
			while (u < 13)
			{
				if (b[t] == 'S')
				{
					A[u].y=b[t+1];
					A[u].x = 'E';
					if (b[t + 1] == 'T')
					{
						A[u].y = 'A';
					}
					else if (b[t + 1] == 'J')
					{
						A[u].y = 'B';
					}
					else if (b[t + 1] == 'Q')
					{
						A[u].y = 'C';
					}
					else if (b[t + 1] == 'K')
					{
						A[u].y = 'D';
					}
					else if(b[t+1]=='A')
					{
						A[u].y = 'E';
					}
					u++;
					t = t + 8;
				}
				else
				{
					A[u].x = b[t];
					A[u].y = b[t + 1];
					if (b[t + 1] == 'T')
					{
						A[u].y = 'A';
					}
					else if (b[t + 1] == 'J')
					{
						A[u].y = 'B';
					}
					else if (b[t + 1] == 'Q')
					{
						A[u].y = 'C';
					}
					else if (b[t + 1] == 'K')
					{
						A[u].y = 'D';
					}
					else if (b[t + 1] == 'A')
					{
						A[u].y = 'E';
					}
					
					t = t + 8;
					u++;
				}
			}
			sort(A, A + 13, cmp);
			for (int p = 0; p < 13; p++)
			{
				if (A[p].x == 'E')
				{
					A[p].x = 'S';
				}
				if (A[p].y == 'A')
				{
					A[p].y = 'T';
				}
				else if (A[p].y == 'B')
				{
					A[p].y = 'J';
				}
				else if (A[p].y == 'C')
				{
					A[p].y = 'Q';
				}
				else if (A[p].y == 'D')
				{
					A[p].y = 'K';
				}
				else if (A[p].y == 'E')
				{
					A[p].y = 'A';
				}
			//	else 
			}
			cout << "West player:" << endl;
			cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
			//int y = 0;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "|" << A[i].y << " " << A[i].y << "|";
				}
				else
				{
					cout << A[i].y << " " << A[i].y << "|";
				}
			}
			cout << endl;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "| " << A[i].x << " |";
				}
				else
				{
					cout << " " << A[i].x << " |";
				}
			}
			cout << endl;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "|" << A[i].y << " " << A[i].y << "|";
				}
				else
				{
					cout << A[i].y << " " << A[i].y << "|";
				}
			}
			cout << endl;
			cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
				 t=(t1+2)%8;
				 t1=t1+2;
				 u=0;
			while (u < 13)
			{
				if (b[t] == 'S')
				{
					A[u].y=b[t+1];
					A[u].x = 'E';
					if (b[t + 1] == 'T')
					{
						A[u].y = 'A';
					}
					else if (b[t + 1] == 'J')
					{
						A[u].y = 'B';
					}
					else if (b[t + 1] == 'Q')
					{
						A[u].y = 'C';
					}
					else if (b[t + 1] == 'K')
					{
						A[u].y = 'D';
					}
					else if(b[t+1]=='A')
					{
						A[u].y = 'E';
					}
					u++;
					t = t + 8;
				}
				else
				{
					A[u].x = b[t];
					A[u].y = b[t + 1];
					if (b[t + 1] == 'T')
					{
						A[u].y = 'A';
					}
					else if (b[t + 1] == 'J')
					{
						A[u].y = 'B';
					}
					else if (b[t + 1] == 'Q')
					{
						A[u].y = 'C';
					}
					else if (b[t + 1] == 'K')
					{
						A[u].y = 'D';
					}
					else if (b[t + 1] == 'A')
					{
						A[u].y = 'E';
					}
					
					t = t + 8;
					u++;
				}
			}
			sort(A, A + 13, cmp);
			for (int p = 0; p < 13; p++)
			{
				if (A[p].x == 'E')
				{
					A[p].x = 'S';
				}
				if (A[p].y == 'A')
				{
					A[p].y = 'T';
				}
				else if (A[p].y == 'B')
				{
					A[p].y = 'J';
				}
				else if (A[p].y == 'C')
				{
					A[p].y = 'Q';
				}
				else if (A[p].y == 'D')
				{
					A[p].y = 'K';
				}
				else if (A[p].y == 'E')
				{
					A[p].y = 'A';
				}
			//	else 
			}
			cout << "North player:" << endl;
			cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
			//int y = 0;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "|" << A[i].y << " " << A[i].y << "|";
				}
				else
				{
					cout << A[i].y << " " << A[i].y << "|";
				}
			}
			cout << endl;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "| " << A[i].x << " |";
				}
				else
				{
					cout << " " << A[i].x << " |";
				}
			}
			cout << endl;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "|" << A[i].y << " " << A[i].y << "|";
				}
				else
				{
					cout << A[i].y << " " << A[i].y << "|";
				}
			}
			cout << endl;
			cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
			
						t=(t1+2)%8;
						t1=t1+2;
						u=0;
			while (u < 13)
			{
				if (b[t] == 'S')
				{
					A[u].y=b[t+1];
					A[u].x = 'E';
					if (b[t + 1] == 'T')
					{
						A[u].y = 'A';
					}
					else if (b[t + 1] == 'J')
					{
						A[u].y = 'B';
					}
					else if (b[t + 1] == 'Q')
					{
						A[u].y = 'C';
					}
					else if (b[t + 1] == 'K')
					{
						A[u].y = 'D';
					}
					else if(b[t+1]=='A')
					{
						A[u].y = 'E';
					}
					u++;
					t = t + 8;
				}
				else
				{
					A[u].x = b[t];
					A[u].y = b[t + 1];
					if (b[t + 1] == 'T')
					{
						A[u].y = 'A';
					}
					else if (b[t + 1] == 'J')
					{
						A[u].y = 'B';
					}
					else if (b[t + 1] == 'Q')
					{
						A[u].y = 'C';
					}
					else if (b[t + 1] == 'K')
					{
						A[u].y = 'D';
					}
					else if (b[t + 1] == 'A')
					{
						A[u].y = 'E';
					}
					
					t = t + 8;
					u++;
				}
			}
			sort(A, A + 13, cmp);
			for (int p = 0; p < 13; p++)
			{
				if (A[p].x == 'E')
				{
					A[p].x = 'S';
				}
				if (A[p].y == 'A')
				{
					A[p].y = 'T';
				}
				else if (A[p].y == 'B')
				{
					A[p].y = 'J';
				}
				else if (A[p].y == 'C')
				{
					A[p].y = 'Q';
				}
				else if (A[p].y == 'D')
				{
					A[p].y = 'K';
				}
				else if (A[p].y == 'E')
				{
					A[p].y = 'A';
				}
			//	else 
			}
			cout << "East player:" << endl;
			cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
			y = 0;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "|" << A[i].y << " " << A[i].y << "|";
				}
				else
				{
					cout << A[i].y << " " << A[i].y << "|";
				}
			}
			cout << endl;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "| " << A[i].x << " |";
				}
				else
				{
					cout << " " << A[i].x << " |";
				}
			}
			cout << endl;
			for (int i = 0; i < 13; i++)
			{
				if (i == 0)
				{
					cout << "|" << A[i].y << " " << A[i].y << "|";
				}
				else
				{
					cout << A[i].y << " " << A[i].y << "|";
				}
			}
			cout << endl;
			cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
			cout<<endl;
		}
	}

总的来说这次实验题都还行,因为我是个蒟蒻,所以这次题的难度能增加我的能力但又不会显得非常的吃力。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值