2018年北理复试上机

1)输入一个字符串,输出其最长回文串的长度,以及最长回文串的个数,不区分大小写。例如
       输入:aB     输出: 1 2
       输入:aBA   输出: 3 1
       输入:aaaa  输出: 4 1

#include <cstdio>
#include <iostream>
#include <string>
#define MaxSize 100
using namespace std;

bool Ishhuiwen(char ch[],int pos,int max) //str中从位置pos开始的长度为max的字符串为回文,返回true,否则false
{
	bool result=true;
	int i,j;
	i=pos;
	j=pos+max-1;
	while(i<j)
	{
		if(ch[i++]!=ch[j--])
		{
			result=false;
			break;
		}
	}
	return result;
}


int main()
{
	char ch[MaxSize];
	char c;
	int count=0,n=0;
	int max,i;
	c=getchar();
	//--1:i=0,qidian getlength,let max=length,if huiwen getmax,n=1,i++,if(i+max)>length,return ,else 
	while(c!='\n')
	{
		if(c>='A' && c<='Z')
			c+=('a'-'A');
		ch[n++]=c;
		c=getchar();
	}
	for(max=n;max>=1;max--)
	{
		for(i=0;i+max<=n;i++)    //这里注意是=,,带入想想i=0,max==length,是成立的
		{
			if(Ishhuiwen(ch,i,max))
				count++;

		}
		if(count>0)
			break;
	}
	printf("%d   %d\n",max,count);

	return 0;

}
/*
bool Ishhuiwen(string str,int pos,int max) //str中从位置pos开始的长度为max的字符串为回文,返回true,否则false
{
	bool result=true;
	int i,j;
	i=pos;
	j=pos+max-1;
	while(i<j)
	{
		if(str[i++]!=str[j--])
		{
			result=false;
			break;
		}
	}
	return result;
}


int main()
{
	string str;
	cin>>str;
	int n=0;
	int max=str.length();
	//--1:i=0,qidian getlength,let max=length,if huiwen getmax,n=1,i++,if(i+max)>length,return ,else 
	for(int i=0;i<str.length();i++)
	{
		if(str[i]>='A' && str[i]<='Z')
			str[i]+=('a'-'A');
	}
	for(max=str.length();max>=1;max--)
	{
		for(i=0;i+max<=str.length();i++)    //这里注意是=,,带入想想i=0,max==length,是成立的
		{
			if(Ishhuiwen(str,i,max))
				n++;

		}
		if(n>0)
			break;
	}
	cout<<max<<"   "<<n<<endl;

	return 0;

}
*/

上下两种写法,因为考虑可能会有空格;注释里面的没考虑空格

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;
string str;

bool IsHuiwen(int i,int j)
{
	while(i<=j)
	{
		if(str[i++]!=str[j--])
			return false;
	}
	return true;
}

int main()
{
	int max=1,n;
	cin>>str;
	n=0;
	int i,j;
	for(i=0;i<str.length();i++)
	{
		if(str[i]>='A' && str[i]<='Z')
			str[i]+=('a'-'A');
	}
	//对于每个i,从i+max开始,找到最长回文长度,若长度大于max=1,重新机,若找不到,继续
	for(i=0;i<str.length() && i+max<=str.length();i++)
	{

		for(j=str.length()-1;((j-i)+1)>=max;j--)
		{
			if(IsHuiwen(i,j))
				break;
		}
		if(((j-i)+1)==max)
			n++;
		else if(((j-i)+1)>max)
		{
			max=j-i+1;
			n=1;
		}

	}
	cout<<max<<" "<<n<<endl; 
	return 0;

}


  2)输入m、n(6<=m<=n<=50),则把[m,n]中的所有偶数表示成两个素数相加的形式。序按照素数出现的次数从多到输出这些素数及其出现的次数,输出次少输出;若出现次数相同,按照素数从大到小输出;若偶数有多种素数相加形式,则把所有的情况都输出,每种情况占一行。(叙述极其复杂,尽量表达了)
       输入:6  7      输出:3 2
       输入:14 15   输出: 7 2
                                      11 1 3 1
       输入:8 10     输出: 5 3 3 1
                                      3 2 5 1 7 12

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct
{
	int p;  //素数
	int n;  //该素数的个数
}number;

	int m,n;
	int num[55]={0};
	int p[50];
	
	vector<number> vi;

bool cmp(number a,number b)
{
	if(a.n!=b.n)
		return a.n>b.n;
	return a.p>b.p;
}

int Getprim(int num[],int p[])
{
	int n=0;
	for(int i=2;i<=50;i++)
	{
		if(num[i]==0)  //找到为素数
		{
			p[n++]=i;
			for(int j=i+i;j<=50;j+=i)
				num[j]++;
		}

	}
	return n;

}

void GetResult(int x ) //当前的偶数为x
{
	if(x>n)
	{
		sort(vi.begin(),vi.end(),cmp);
		for(vector<number>::iterator it=vi.begin();it!=vi.end();it++)
		{
			printf("素数:%d  个数:%d   ",(*it).p,(*it).n);
		}
		printf("\n");
		return ;
	}
	else
	{
		for(int i=0;p[i]*2<=x;i++)
		{
			int s1=p[i],f1=0;
		//fenwei2sushu
			int s2=x-p[i],f2=0;
			number a;
			if(num[s2]==0)
			{
				
				for(vector<number>::iterator it=vi.begin();it!=vi.end();it++)
				{
					if((*it).p==s1)
					{
						f1=1;
						(*it).n++;
					}
					if((*it).p==s2)
					{
						f2=1;
						(*it).n++;
					}

				}
				if(f1==0)
				{
					s1==s2?a.n=2:a.n=1;
					a.p=s1;
					vi.push_back(a);
				}
				if(f2==0 && s1!=s2)
				{
					a.n=1;
					a.p=s2;
					vi.push_back(a);
				}

				//diaoyongxiayige 
			GetResult(x+2 );
		//jieshu ciugaivi
			for( it=vi.begin();it!=vi.end();)
			{
				if((*it).p==s1)
				{	
					(*it).n--;

				}
				if((*it).p==s2)
				{
					(*it).n--;
				}
				if((*it).n==0)
					vi.erase(it);
				else
					it++;

			}
				
			}
		
		
		//xiayixunhuan
		}
	}

}

 


int main()
{
	

	int count=0;  //50以内素数的个数
	int n_odd=0;
	count=Getprim(num,p);
	scanf("%d%d",&m,&n);
	if(n<m)
	{
		int t=n;
		n=m;
		m=t;
	}
	if(m%2==1)
		m++;
	GetResult(m);




	

}
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int p[55];   //hash
int su[50]; //存储素数
int n=0;  //素数的个数
typedef struct 
{
	int value;
	int num;
}number;
void Getsushu()
{
	int j;
	for(int i=0;i<55;i++)
	{
		p[i]=0;
	}
	for(i=2;i<55;i++)
	{
		if(p[i]==0)
		{
			su[n++]=i;
			for(j=2;i*j<55;j++)
				p[i*j]++;
		}
	}
}
void Print(vector<number> vi)
{
	for(vector<number>::iterator it=vi.begin();it!=vi.end();it++)
	{
		printf("%d %d  ",(*it).value,(*it).num);
	}
	printf("\n");

}
void GetResult(vector<number> vi,int m,int n)
{
	vector<number> vi_temp=vi;
	int flag1=0;
	int flag2=0;
	if(m>n)
	{
		Print(vi);
		return ;
	}
	//先拆,在调用
	for(int i=0;su[i]<=m/2;i++)
	{
		if(p[m-su[i]]==0)  //成功拆为两个素数
		{
			number a;
			for(vector<number>::iterator it=vi.begin();it!=vi.end();it++)
			{
				if((*it).value==su[i])
				{
					flag1=1;
					(*it).num++;
				}
				if((*it).value==(m-su[i]))
				{
					flag2=1;
					(*it).num++;
				}
			}
			if(flag1==0)
			{
				a.value=su[i];
				a.num=1;
				if(su[i]==(m-su[i]))
				{
					a.num=2;
					
				}
				vi.push_back(a);

			}
			if(flag2==0 && su[i]!=(m-su[i]))
			{
				a.value=m-su[i];
				a.num=1;
				vi.push_back(a);
			}
			GetResult(vi,m+2,n);
		}
		vi=vi_temp;
		

	}

}
int main()
{
	Getsushu();
	int n,m;
	cin>>m>>n;
	if(m%2==1)
		m++;
	vector<number> vi;
	GetResult(vi,m,n);
	

}

 

 

软件2018年题

 

1.      输入n(0<=n<=9)打印上底和高都是n的等腰梯形,例如n等于4就打印

   ****

  ******

 ********

**********

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	int i,j,n;
	cin>>n;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n-i-1;j++)
		{
			printf(" ");
		}
		for(j=0;j<n+2*i;j++)
		{
			printf("*");
		}
	printf("\n");
	}

}

 

2.      分别输入两个集合(字符串输入),按升序输出两个集合的交集,测试数据里集合不会有重复的字符。(注意集合内部去重,例如A:abac B:abde,则集合A保证a是唯一的)
比如abcd abef
输出ab

#include <iostream>
#include <cstdio>
#include <string>
#include <set>
using namespace std;

int main()
{
	set<char> sa;
	set<char> sb;
	int i;
	string a;
	string b;
	cout<<"请输入两个集合(空集合输入#)"<<endl;
	cin>>a;
	cin>>b;
	if(a=="#" || b=="#")
		return 0;
	for(i=0;i<a.length();i++)
	{
		sa.insert(a[i]);

	}
	for(i=0;i<b.length();i++)
	{
		sb.insert(b[i]);
	}
	set<char>::iterator it1,it2;
	for(it1=sa.begin(),it2=sb.begin();it1!=sa.end()&&it2!=sb.end();)
	{
		if((*it1)==(*it2))
		{
			cout<<(*it1);
			it1++;
			it2++;
		}
		else if((*it1)>(*it2))
		{
			it2++;
		}
		else if((*it1)<(*it2))
		{
			it1++;
		}

	}
	printf("\n");
	return 0;
}

 

3.      输入一个字符串,每个字符作为元素去重并升序排序生成一个链表(必须是链表),最后用递归逆序输出链表(注意,可能输入例如:abc  sd1abc95)

#include <iostream>
#include <cstdio>
using namespace std;

typedef struct  node
{
	char key;
	struct node *next;

}Node,*LNode;

void Insert(LNode &L,char c)
{
	LNode p;
	if(L==NULL)
	{
		L=(LNode )malloc(sizeof(Node));
		L->next=NULL;
		p=(LNode )malloc(sizeof(Node));
		p->next=NULL;
		p->key=c;
		L->next=p;
		return ;
	}
	LNode q;
	q=L;
	p=L->next;
	while(p)
	{
		if(p->key==c)  //重复
			return;
		if(p->key>c)
			break;
		q=p;
		p=p->next;

	}
	LNode r=(LNode )malloc(sizeof(Node));
	r->key=c;
	r->next=NULL;
	q->next=r;
	r->next=p;
	return ;

}
void Print(LNode L)
{
	if(L==NULL)
		return;

	Print(L->next);
	printf("%c",L->key);
}
int main()
{
	LNode L=NULL;
	char c;
	c=getchar();
	while(c!='\n')
	{
		Insert(L,c);
		c=getchar();
	}
	Print(L->next);
	printf("\n");
	return 0;

}

 

4.      输入一串数字,数字之间以空格分开,最后以换行符结束输入,用链表实现奇数从大到小,偶数从小到大输出,(若不用链表则最多得一半分),例如输入1 2 3 4,输出

3 1

2 4

#include <iostream>
#include <cstdio>
using namespace std;

typedef struct  node
{
	int key;
	struct node *next;

}Node,*LNode;

void InsertOdd(LNode &L,int c)
{
	LNode p;
	if(L==NULL)
	{
		L=(LNode )malloc(sizeof(Node));
		L->next=NULL;
		p=(LNode )malloc(sizeof(Node));
		p->next=NULL;
		p->key=c;
		L->next=p;
		return ;
	}
	LNode q;
	q=L;
	p=L->next;
	while(p)
	{
		if(p->key<c)
			break;
		q=p;
		p=p->next;

	}
	LNode r=(LNode )malloc(sizeof(Node));
	r->key=c;
	r->next=NULL;
	q->next=r;
	r->next=p;
	return ;

}
void InsertEven(LNode &L,int c)
{
	LNode p;
	if(L==NULL)
	{
		L=(LNode )malloc(sizeof(Node));
		L->next=NULL;
		p=(LNode )malloc(sizeof(Node));
		p->next=NULL;
		p->key=c;
		L->next=p;
		return ;
	}
	LNode q;
	q=L;
	p=L->next;
	while(p)
	{
		if(p->key>c)
			break;
		q=p;
		p=p->next;

	}
	LNode r=(LNode )malloc(sizeof(Node));
	r->key=c;
	r->next=NULL;
	q->next=r;
	r->next=p;
	return ;

}
void Print(LNode L)
{
	LNode p=L;
	while(p)
	{
		printf("%d  ",p->key);
		p=p->next;
	}
}
int main()
{
	LNode LO=NULL,LE=NULL;
	char c;
	int a;
	
	while(1)
	{
		scanf("%d",&a);
		c=getchar();
		if(a%2==0)
			InsertEven(LE,a);
		else
			InsertOdd(LO,a);
		if(c=='\n')
			break;
	}
	Print(LO->next);
	printf("\n");
	Print(LE->next);
	printf("\n");

	return 0;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值