基础算法学习1

一、算法题:

二、代码

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 using namespace std;
 5 int f(int n, int m) {
 6     n = n % m;
 7     vector<int> v;
 8     for(;;) {
 9         v.push_back(n);
10         n *= 10;
11         n = n % m;
12         if (n == 0) return 0;
13         if (find(v.begin(), v.end(), n) != v.end()) {
14             return v.size()-(find(v.begin(), v.end(), n)-v.begin());
15         }
16     }
17 }
18 int main() {
19     int n, m;
20     cin >> n >> m;
21     cout << f(n, m) << endl;
22     return 0;
23 }

三、知识点

  1、STL标准库模板中vector容器相比于数组的优点:随时分配所需内存,并且具有很多方便使用的库函数。

  2、algorithm头文件中find()方法适用于在vector容器中寻找所给参数n所在的位置。

  3、学会使用vector容器迭代功能(vector<int>::iterator = v.begin();iterator != v.end(); iterator++)

猜你喜欢

转载自www.cnblogs.com/panboo/p/10068339.html
今日推荐