|
发表于 2005-12-2 10:07:38
|
显示全部楼层
应该符合题目要求了,只用了move函数中只用了i和tmp两个辅助变量。时间复杂度为O(n).
- #include<iostream>
- void move(int a[], int n, int k)
- {
- int i = (0+k)%n;
- int tmp;
- while(i != 0) {
- //swap(a[0], a[i]);
- tmp = a[0];
- a[0] = a[i];
- a[i] = tmp;
-
- i = (i+k)%n;
- }
- }
- int main()
- {
- int a[10] = {0,1,2,3,4,5,6,7,8,9};
- std::cout <<"a[10]=";
- for(int i = 0; i < 10; i++)
- std::cout << a[i] << " ";
- std::cout << std::endl;
- move(a, 10, 3);
- std::cout <<"a[10]=";
- for(int i = 0; i < 10; i++)
- std::cout << a[i] << " ";
- std::cout << std::endl;
- }
复制代码 应该符合题目要求了,只用了move函数中只用了i和tmp两个辅助变量。时间复杂度为O(n). |
|