[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
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 |
- #include<iostream>
- #include<string>
- using namespace std;
- int main()
- {
- //[C_MM44-易] The Numbers
- //判斷一個數字N出現在另外一個數字M中的次數
- //N第一個數字和M第一個數字相同時count+1
- //N第二個數字和M第二個數字相同時count+1
- //count == N的長度時
- //代表與N相同,所以times+1
- string N, M;
- while(cin >> N >> M)
- {
- int times = 0;//計算重複次數
- for(int i=0; i < M.length();i++)
- {
- int count = 0;
- //逐一比較次數
- for(int j = 0;j < N.length();j++)
- {
- if(M[i+j] == N[j] && i+j < M.length())
- {
- count++;
- }
- else
- {
- break;
- }
- }
- if(count == N.length())
- {
- times++;
- }
- }
- cout << times << endl;
- }
- }
沒有留言:
張貼留言