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
#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;
}