/*************************************************************************
> File Name: vector_rotate.cpp
> Author: SongLee
************************************************************************/
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s = "abcdefghijklmn";
cout << "The origin is: " << s << endl;
// 左移个数
int i;
cin >> i;
if(i > s.size())
{
i = i%s.size();
}
// 将前i个元素临时保存
string tmp(s, 0, i);
// 将剩余的左移i个位置
for(int j=i; j<s.size(); ++j)
{
s[j-i] = s[j];
}
s = s.substr(0, s.size()-i) + tmp;
cout << "The result is: "<< s << endl;
return 0;
}
/*************************************************************************
> File Name: vector_rotate_1.cpp
> Author: SongLee
************************************************************************/
#include<iostream>
#include<string>
using namespace std;
void rotateOnce(string &s)
{
char tmp = s[0];
int i;
for(i=1; i<s.size(); ++i)
{
s[i-1] = s[i];
}
s[i-1] = tmp;
}
int main()
{
string s = "abcdefghijklmn";
cout << "The origin is: " << s << endl;
// 左移个数
int i;
cin >> i;
if(i > s.size())
{
i = i%s.size();
}
// 调用函数i次
while(i--)
{
rotateOnce(s);
}
cout << "The result is: "<< s << endl;
return 0;
}
/*************************************************************************
> File Name: vector_rotate_2.cpp
> Author: SongLee
************************************************************************/
#include<iostream>
#include<string>
using namespace std;
// 欧几里德(辗转相除)算法求最大公约数
int gcd(int i, int j)
{
while(1)
{
if(i > j)
{
i = i%j;
if(i == 0)
{
return j;
}
}
if(j > i)
{
j = j%i;
if(j == 0)
{
return i;
}
}
}
}
int main()
{
string s = "abcdefghijklmn";
cout << "The origin is: "<< s << endl;
// 左移个数
int i;
cin >> i;
if(i > s.size())
{
i = i%s.size();
}
// 移动
char tmp;
int times = gcd(s.size(), i);
for(int j=0; j<times; ++j)
{
tmp = s[j];
int pre = j; // 记录上一次的位置
while(1)
{
int t = pre+i;
if(t >= s.size())
t = t-s.size();
if(t == j) // 直到tmp原来的位置j为止
break;
s[pre] = s[t];
pre = t;
}
s[pre] = tmp;
}
cout << "The result is: "<< s << endl;
return 0;
}
reverse(0, i-1) /*cbadefgh*/ reverse(i, n-1) /*cbahgfed*/ reverse(0, n-1) /*defghabc*/
/*************************************************************************
> File Name: vector_rotate.cpp
> Author: SongLee
************************************************************************/
#include<iostream>
#include<string>
using namespace std;
void reverse(string &s, int begin, int end)
{
while(begin < end)
{
char tmp = s[begin];
s[begin] = s[end];
s[end] = tmp;
++begin;
--end;
}
}
int main()
{
string s = "abcdefghijklmn";
cout << "The origin is: "<< s << endl;
int i;
cin >> i;
if(i > s.size())
{
i = i%s.size();
}
reverse(s, 0, i-1);
reverse(s, i, s.size()-1);
reverse(s, 0, s.size()-1);
cout << "The result is: "<< s << endl;
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有