#32575: C++雙重迴圈題


alex950301 (alex0301)

學校 : 新北市立新莊高級中學
編號 : 141423
來源 : [210.71.71.205]
最後登入時間 :
2024-04-16 16:27:37
c929. 蝸牛老師的點名單-續 | From: [61.64.1.159] | 發表日期 : 2022-10-21 23:55

可以試著用兩個for解決,以測資1作範例:

分別比對 and  和   appleandbanana

 

app and

ppl and

ple and

.

.

.

and and (可以建立一個n儲存有幾個相同,如果 n = a.size()  => 輸出endl ,b[i] 中的 i 要加上 a.size() - 1 ,避免重複輸出)

.

.

ana and

 

如果以上敘述很攏統,在下面附上參考解答

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include <iostream>
using namespace std;
int main(){
    string a , b ;
    getline( cin , a ) ; 
    getline( cin , b ) ;
    for( int i = 0 ; i < b.size() ; i++ ) {
        int I = i , n = 0 ; 
        for( int j = 0 ; j < a.size() ; j++ ) {
            if( b[I] == a[j] ) n++ ;
            I++ ;
        }
        if( n == a.size() ) {
            cout << endl ;
            i += a.size() - 1 ;
        }
        else cout << b[i] ;
    }
    cout << endl ;
    return 0;
}

 

 
ZeroJudge Forum