2018年1月12日 星期五

[MM07-易] The Numbers(C++)

[MM07-易] The Numbers

Problem Description: 
Write a program to report the number of times a number N appears in another number M. The number N is between 10 and 99, and the number M is between 1000000 and 9999999, inclusively.
Input File Format: 
There are two numbers in the input -- N followed by M.
Output Format: 
The output has only one number, namely the number of times N appears in M.
Example

Sample Input:Sample Output:
90 9090999
11 1110111
2
4
  1. #include<iostream>    
  2. #include<string>    
  3.      
  4. using namespace std;    
  5.      
  6. int main()    
  7. {    
  8.     //[C_MM44-易] The Numbers    
  9.     //判斷一個數字N出現在另外一個數字M中的次數    
  10.     //N第一個數字和M第一個數字相同時count+1    
  11.     //N第二個數字和M第二個數字相同時count+1    
  12.     //count == N的長度時    
  13.     //代表與N相同,所以times+1    
  14.     string N, M;    
  15.     while(cin >> N >> M)    
  16.     {    
  17.         int times = 0;//計算重複次數    
  18.         for(int i=0; i < M.length();i++)    
  19.         {    
  20.             int count = 0;    
  21.             //逐一比較次數    
  22.             for(int j = 0;j < N.length();j++)    
  23.             {    
  24.                 if(M[i+j] == N[j] && i+j < M.length())    
  25.                 {    
  26.                     count++;    
  27.                 }    
  28.                 else    
  29.                 {    
  30.                     break;    
  31.                 }    
  32.             }    
  33.             if(count == N.length())    
  34.             {    
  35.                 times++;    
  36.             }    
  37.         }    
  38.         cout << times << endl;    
  39.     }    
  40. }  

沒有留言:

張貼留言