時間限制:1 秒 內存限制:32 兆 特殊判題:否 提交:554 解決:130
題目描述:
讀入一組字符串(待操作的),再讀入一個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
//北郵2011網院:1177:查找
//命令格式:第一位0代表翻轉,1代表替換;
//第二位代表待操作的字符串的起始下標int i;
//第三位表示需要操作的字符串長度int len。
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int i, j, k, n, m;
int start, len;
string s, t, temp;
ifstream cin("BUPT_1177.txt");//
while( cin >> s >> n ){
for( i=0; i<n; i++ ){
cin >> t;
start = t[1]-48;
len = t[2] - 48;
//cout << start << " " << len << endl;//
if( t[0] == '0' ){ //翻轉命令 別忘了單引號
temp = s.substr(start,len);
//cout << temp << endl;//
reverse(temp.begin(), temp.end());
}
else
temp = t.substr(3,t.length()-3);
s = s.erase(start,len);
s.insert(start,temp);
cout << s << endl;
}
}
system("pause");//
return 0;
}
九度OJ 題目1178:複數集合
時間限制:1 秒 內存限制:32 兆 特殊判題:否 提交:669 解決:103
題目描述:
一個複數(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都是非負數。
沒注意提示 WA了好幾次orz 稍微有點複雜 不過並不算難
//北郵2011網院:1178:複數集合
//Pop 表示讀出集合中複數模值最大的那個複數,如集合爲空 輸出 empty ,
//不爲空就輸出最大的那個複數並且從集合中刪除那個複數,再輸出集合的大小SIZE;
//Insert a+ib 指令(a,b表示實部和虛部),將a+ib加入到集合中 ,輸出集合的大小SIZE;
//n條指令 (1<=n<=1000)
//提示:模相等的輸出b較小的複數。 a和b都是非負數。
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
struct COMPLEX{
int a, b;
};
COMPLEX c[1000];
int StringToNum( string s ){
int i, len, r;
len = s.length();
for( i=0; i<len; i++ ){
if( i==0 )
r = s[i]-48;
else r = r*10 + s[i]-48;
}
return r;
};
int module( COMPLEX x ){
return x.a*x.a+x.b*x.b;
};
bool cmp( COMPLEX x, COMPLEX y ){
int xm = module(x), ym = module(y);
if( xm==ym )
return x.a < y.a;
return xm < ym;
};
int main()
{
int i, j, k, n, m;
int len, size;
string t, s, temp, aS, bS; //t=命令
ifstream cin("BUPT_1178.txt");//
while( cin >> n ){
size = 0;
for( i=0; i<n; i++ ){
cin >> t;
if( t == "Pop" ){
//cout << "i=" << i << " ";//
if( size == 0 )
cout << "empty\n";
else{
sort(c,c+size,cmp);
cout << c[size-1].a << "+i" << c[size-1].b << endl;
size--;
cout << "SIZE = " << size << endl;
}
}
else{ //Insert命令
cin >> s;
len = s.length();
for( j=0; j<len; j++ )
if( s[j]=='+' )
break;
aS = s.substr(0,j);
bS = s.substr(j+2); //不給出長度就默認到結尾
//cout << aS << " " << bS << endl;//
c[size].a = StringToNum(aS);
c[size].b = StringToNum(bS);
size++;
cout << "SIZE = " << size << endl;
}
}// for
}
system("pause");//
return 0;
}
附上第一題需要用的庫函數的說明 網上找的 很詳盡
===========以下內容爲轉載===========
C:
char st[100];
1. 字符串長度
strlen(st);
2. 字符串比較
strcmp(st1,st2);
strncmp(st1,st2,n); 把st1,st2的前n個進行比較。
3. 附加
strcat(st1,st2);
strncat(st1,st2,n); n表示連接上st2的前n個給st1,在最後不要加'\0'。
4. 替換
strcpy(st1,st2);
strncpy(st1,st2,n); n表示複製st2的前n個給st1,在最後要加'\0'。
5. 查找
where = strchr(st,ch) ch爲要找的字符。
where = strspn(st1,st2); 查找字符串。
where = strstr(st1,st2);
C++:
<string>
string str;
1. 字符串長度
len = str.length();
len = str.size();
2. 字符串比較
可以直接比較
也可以:
str1.compare(str2);
str1.compare(pos1,len1,str2,pos2,len2); 值爲負,0 ,正。
nops 長度到完。
3. 附加
str1 += str2;
或
str1.append(str2);
str1.append(str2.pos2,len2);
4. 字符串提取
str2 = str1.substr();
str2 = str1.substr(pos1);
str2 = str1.substr(pos1,len1);
string a=s.substr(0,4); //獲得字符串s中 從第0位開始的長度爲4的字符串
5. 字符串搜索
where = str1.find(str2);
where = str1.find(str2,pos1); pos1是從str1的第幾位開始。
where = str1.rfind(str2); 從後往前搜。
6. 插入字符串
不是賦值語句。
str1.insert(pos1,str2);
str1.insert(pos1,str2,pos2,len2);
str1.insert(pos1,numchar,char); numchar是插入次數,char是要插入的字符。
7. 替換字符串
str1.replace(pos1,str2);
str1.replace(pos1,str2,pos2,len2);
8. 刪除字符串
str.erase(pos,len)
str.clear();
9. 交換字符串
swap(str1,str2);
10. C --> C++
char *cstr = "Hello";
string str1;
cstr = cstr;
string str2(cstr);