原題鏈接:P1953 易語言
(我最近怎麼總在做大模擬大搜索題)
分別處理兩種情況。
如果只有一個1或0
直接設一個cnt爲這個值,每次輸入一個新名字之後把數字替換成cnt,最後cnt++即可。
注意數字可能不止一位,輸出一次cnt之後可以整一個bool變量阻止以後的輸出。
如果是要改名
把名稱和後綴分別存起來,掃一遍名稱找出初始數字,賦給cnt。
同樣,初始數字可能不止一位,要每次乘10加n。
然後不用管輸入的是啥,每有一個輸入輸出一遍初始名稱,數字部分替換成cnt即可。然後輸出第二個名稱時檢測到小數點(誤)直接輸出後綴名之後break。每次循環後cnt++。
然後就WA掉20分
如果數字在後綴名末尾,這樣會直接把第二個名稱中的數字break掉。這時候之前整的那個bool變量就可以用了。如果數字輸出沒被阻止(即還沒輸出過數字),直接輸出一次cnt就行。
源碼:
//MiserWeyte is now "mzWyt"
#include <bits/stdc++.h>
using namespace std;
string opt1, opt2;
string name1, name2;
int main(){
cin >> opt1;
if(opt1[0] == '0' || opt1[0] == '1'){ // 只更換序號
int cnt = opt1[0] - '0';
while(cin >> name1 >> name2){
bool notend;
notend = true;
for(int i=0; i<name1.length(); i++){ // 輸入文件文件名
if(name1[i]>='0' && name1[i] <= '9'){ // 把數字部分替換爲cnt
if(notend){ // 防止數字部分有多位,只在第一位時輸出一次
cout << cnt;
notend = false;
}
}
else cout << name1[i];
}
cout << ' ';
notend = true;
for(int i=0; i<name2.length(); i++){ //輸出文件文件名同上
if(name2[i]>='0' && name2[i] <= '9'){
if(notend){
cout << cnt;
notend = false;
}
}
else cout << name2[i];
}
cout << '\n';
cnt ++;
}
}
else{ // 更換名稱
cin >> opt2;
int cnt = 0;
for(int i=0; i<opt1.length(); i++){ // 防止初始數字有多位
if(opt1[i] >= '0' && opt1[i] <= '9'){
cnt *= 10;
cnt += opt1[i] - '0';
}
}
while(cin >> name1 >> name2){
bool notend;
notend = true;
for(int i=0; i<opt1.length(); i++){ // 同只改數字的代碼,只不過每次以opt1爲模板
if(opt1[i] >= '0' && opt1[i] <= '9'){
if(notend){
cout << cnt;
notend = false;
}
}
else cout << opt1[i];
}
cout << ' ';
notend = true;
for(int i=0; i<opt1.length(); i++){ //輸出文件文件名
if(opt1[i] >= '0' && opt1[i] <= '9'){
if(notend){
cout << cnt;
notend = false;
}
}
else{
cout << opt1[i];
if(opt1[i] == '.'){ // 點後面的直接替換成指定後綴
cout << opt2;
break;
}
}
}
if(notend) cout << cnt; // 若還沒輸出過數字,則數字在後綴名後方,輸出一次
cout << '\n';
cnt ++;
}
}
return 0;
}