北京邮电大学_2011网院___考研计算机_复试上机

1,查找

题目描述:

    读入一组字符串(待操作的),再读入一个int n记录记下来有几条命令,总共有2中命令:1、翻转  从下标为i的字符开始到i+len-1之间的字符串倒序;2、替换  命中如果第一位为1,用命令的第四位开始到最后的字符串替换原读入的字符串下标 i 到 i+len-1的字符串。每次执行一条命令后新的字符串代替旧的字符串(即下一条命令在作用在得到的新字符串上)。

    命令格式:第一位0代表翻转,1代表替换;第二位代表待操作的字符串的起始下标int i;第三位表示需要操作的字符串长度int len。

输入:

输入有多组数据。
每组输入一个字符串(不大于100)然后输入n,再输入n条指令(指令一定有效)。

输出:

根据指令对字符串操作后输出结果。

样例输入:
bac
2
003
112as
样例输出:
cab
cas
总结:一开始没读清题目,还以为替换的时候长度不一样,走了些弯路;by the way:翻转的时候设置前后两个指针,很不错!!

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


int main()
{
	string str;
	int n;

	while(cin>>str>>n)
	{
		string commond;
		for(int k=0;k<n;k++)
		{
			cin>>commond;
			int i,j,len;
			char tmp;
			int len_c = commond.length();
			int len_s = str.length();

			//翻转
			if(commond[0]=='0'){
				i = (int)(commond[1]-'0');
				len = (int)(commond[2]-'0');

				for(i,j=i+len-1;i<j;i++,j--)
				{
					tmp = str[j];
					str[j] = str[i];
					str[i] = tmp;
				}

			}
			//替换
			if(commond[0]=='1')
			{
				i = (int)(commond[1]-'0');
				len = (int)(commond[2]-'0');

				for(j=i;j<i+len;j++)
				{
					str[j] = commond[j-i+3];
				}

			}

			//输出
			cout<<str<<endl;
		}
	
	}


	return 0;
}


2,复数集合

题目描述:

    一个复数(x+iy)集合,两种操作作用在该集合上:

    1、Pop 表示读出集合中复数模值最大的那个复数,如集合为空 输出  empty  ,不为空就输出最大的那个复数并且从集合中删除那个复数,再输出集合的大小SIZE;

    2 Insert a+ib  指令(a,b表示实部和虚部),将a+ib加入到集合中 ,输出集合的大小SIZE;

    最开始要读入一个int n,表示接下来的n行每一行都是一条命令。

输入:

输入有多组数据。
每组输入一个n(1<=n<=1000),然后再输入n条指令。

输出:

根据指令输出结果。

样例输入:
3
Pop
Insert 1+i2
Pop
样例输出:
empty
SIZE = 1
1+i2
SIZE = 0
提示:

模相等的输出b较小的复数。

a和b都是非负数。

总结:我再调试,现在休息

Output Limit Exceed  

#include<stdio.h>
#define N 1024

struct plu{
	int a;
	int b;
};

int main()
{
	int n;

	while(scanf("%d",&n))
	{
		int len = 0;
		struct plu plus[N];
		char c[10];
		int i,j,k;

		for(int i=0;i<n;i++)
		{
			scanf("%s",c);

			if(c[0] == 'P')
			{
				if(len == 0)
				{
					printf("empty\n");
				}else{
					int max = len -1;
					for(j=len-2;j>=0;j--)
					{
						if(plus[max].a < plus[j].a || (plus[max].a == plus[j].a && plus[max].b < plus[j].b ) )
						{
							max = j;
						}
					}
					printf("%d+i%d\n",plus[max].a,plus[max].b);
					if(max != len-1)
					{
						plus[max] = plus[len-1];
					}
					len --;
					printf("SIZE = %d\n",len);
				}
				
			}
			if(c[0] == 'I')
			{
				scanf("%d+i%d",&k,&j);
				struct plu tmp;
				tmp.a = k;
				tmp.b = j;
				plus[len++] = tmp;
				printf("SIZE = %d\n",len);
			}
		}	
	}

	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值