这是代码:
#include <iostream>
using namespace std;
int main ()
{
int n;
cin >> n;
int first = n;
while (first>=10)
{
first/=10;
}
cout << first << endl;
}
在上面的代码中,我尝试获取正数的第一位数字,我想要做的是将其放在最后一位数字之后,如下所示,例如:1934 -> 9341
.
使用将数字转换为字符串std::to_string http://en.cppreference.com/w/cpp/string/basic_string/to_string,使用执行左旋转std::旋转 http://en.cppreference.com/w/cpp/algorithm/rotate并使用转换回数字std::斯托尔 http://en.cppreference.com/w/cpp/string/basic_string/stoul功能:
std::string s = std::to_string(n);
std::rotate(s.begin(), s.begin() + 1, s.end());
n = std::stoull(s);
包含所有标题:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
unsigned long long n = 1934;
std::string s = std::to_string(n);
std::rotate(s.begin(), s.begin() + 1, s.end()); // left rotation
n = std::stoull(s);
std::cout << n; // 9341
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)