C++ 如何将c+中的第一个数字移动到数字的末尾+;?
代码如下:C++ 如何将c+中的第一个数字移动到数字的末尾+;?,c++,C++,代码如下: #include <iostream> using namespace std; int main () { int n; cin >> n; int first = n; while (first>=10) { first/=10; } cout << first << endl; } #包括 使用名称空间std; int main() { int n;
#include <iostream>
using namespace std;
int main ()
{
int n;
cin >> n;
int first = n;
while (first>=10)
{
first/=10;
}
cout << first << endl;
}
#包括
使用名称空间std;
int main()
{
int n;
cin>>n;
int first=n;
而(第一个>=10)
{
第一/=10;
}
cout您可以修改循环,不仅获得第一位数字,还可以同时计算位数(这是循环的迭代次数)。然后,根据需要使用%
和*10
修改n
使用将数字转换为字符串,使用执行左旋转并使用函数转换回数字:
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
}
#包括
#包括
#包括
int main(){
无符号长n=1934;
std::string s=std::to_string(n);
旋转(s.begin(),s.begin()+1,s.end());//左旋转
n=标准::斯托尔(s);
标准::cout
这:
将数字转换为字符串
将第一个字符添加到结尾
删除第一个字符
并将结果转换回无符号long
由于已经有很多使用std::string
的解决方案,我尝试在没有它的情况下使用它,下面是我的结果。
我希望这有帮助
#include <iostream>
#include <cmath>
using namespace std;
int removeFirst(int n)
{
int tmp(0);
for (int i(0);; ++i)
{
int m = n % 10;
n /= 10;
if (n != 0)
{
tmp += pow(10, i) * m;
}
else
{
break;
}
}
return tmp;
}
int main()
{
int input, first, withoutFirst;
cin >> input;
withoutFirst = removeFirst(input);
while (input >= 10)
{
input /= 10;
}
first = input;
cout << withoutFirst << first << endl;
}
#包括
#包括
使用名称空间std;
int removeFirst(int n)
{
int-tmp(0);
对于(int i(0);++i)
{
int m=n%10;
n/=10;
如果(n!=0)
{
tmp+=pow(10,i)*m;
}
其他的
{
打破
}
}
返回tmp;
}
int main()
{
int输入,第一个,不带第一个;
cin>>输入;
withoutFirst=移除第一个(输入);
同时(输入>=10)
{
输入/=10;
}
第一个=输入;
cout这是一个不使用字符串的版本
//There's also a builtin log10 function but you can write your own if you want to:
int log10(int nbr) {
return log(n) / log(10);
}
//....
int first_digit = n / (int)pow(10, log10(n));
int everything_else = n % (int)pow(10, log10(n));
确保包括math.h
它使用了一个事实:在C++中,代码>浮标> int >代码>转换总是向着零旋转,所以<代码>(int)Log10(101)< /C>将返回2,而<代码> POW(10,(Log10(n)))因此,
会将每个数字四舍五入到10/100/1000/etc
。剩下的只是简单的除法和模运算。已经有一些答案是使用string
的,还有一个答案到目前为止没有string
。使用string
是最有效的方法。但是如果OP想通过计算来解决这个问题方法和使用integer,这是我尝试过的。如果不使用string
,我想这种解决方案会更有效
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int n, digits, firstDigit, firstDigitToLast;
cin >> n;
digits = (int)log10(n);
firstDigit = (int)(n / pow(10, digits));
firstDigitToLast = n % ((int) pow(10, digits));
firstDigitToLast *= 10;
firstDigitToLast += firstDigit;
cout << firstDigitToLast << endl;
}
#包括
#包括
使用名称空间std;
int main()
{
int n,数字,firstDigit,firstDigitToLast;
cin>>n;
数字=(int)log10(n);
第一位数字=(整数)(n/pow(10位数字));
firstDigitToLast=n%((整数)功率(10位数字));
firstDigitToLast*=10;
firstDigitToLast+=第一位数字;
cout我会使用log10
和%
int num = 8907;
int num_digits = log10(num); // Value of 3 for 8907
int pwr = pow(10, num_digits); // Value of 1000 for 8907
int ones_place = num % 10; // 7
int bigs_place = num / pwr; // 8
int cur_msd = bigs_place * pwr; // 8000
int new_msd = ones_place * pwr; // 7000
num -= cur_msd;
num += new_msd;
num -= ones_place;
num += bigs_place;
cout << num << endl;
这里有一个简单的解决方案,它不使用字符串或浮点函数/算术。使用诸如pow()
之类的函数可能会遇到如上所述的问题
注意:我查看了生成的汇编语言,惊讶于编译器能够理解代码的意图,并在编译时计算了9341。我怀疑pow
解决方案或浮点方法会产生这些结果。这是编程和数学+编程竞赛的典型元素,因此我认为我不会给出一个完整的答案,但我会说:
- 使用字符串几乎肯定不是最节省cpu时间的解决方案
- 您已经有了第一个(最高有效)数字。修改问题中的代码,您可以计算适当的10次幂,从原始数字中减去第一个数字
- 请注意,
n*10
将数字向左移动,并留下一个“洞”,您可以在以后根据需要填充。(如果这对您来说是显而易见的,请道歉。)
- 您可以只使用整数运算、无浮点、无函数(
log
、exp
)完成所有这些操作。其他解决方案已经显示了完整的算法;我强调您可以不使用浮点和日志
- 注意0和负数
假设我们有正整数作为输入
获取最有效的数字
MSD=floor(X/pow(10,floor(log10(X))));
Y=Rest+MSD.
把剩下的号码都记下来
Rest=X%pow(10,floor(log10(X)));
将其余部分提升到适当的值
Rest=Rest*10;
将前一个最高有效数字相加
MSD=floor(X/pow(10,floor(log10(X))));
Y=Rest+MSD.
分解示例:
X=54321; // 54321
log10(X) // 4.734...
floor(... ) // 4
pow(10,... ) // 10000
X/... // 5.4321
MSD=floor(... );// 5
pow(10,floor(log10(X)) // 10000
X%... // 4321
10*... // 43210
Y=MSD+... ; // 43215
一个不使用字符串的解决方案。它使用的是std::stack
,可能不会赢得任何效率奖励,但它应该非常简单易懂
#include <stack>
#include <iostream>
int main()
{
int num = 1934;
std::stack<int> digits;
// Break the number into digits, pushing them onto a stack
while (num)
{
auto digit = num % 10;
digits.push(digit);
num /= 10;
}
// Get the first digit from the top of the stack and save it
auto first = 0;
if (!digits.empty())
{
first = digits.top();
digits.pop();
}
// Pop the remaining digits off the stack and print them
while (!digits.empty())
{
std::cout << digits.top();
digits.pop();
}
// Print the first digit on the end
std::cout << first << '\n';
}
#包括
#包括
int main()
{
int num=1934;
std::堆栈数字;
//将数字拆分为数字,并将其推到堆栈上
while(num)
{
自动数字=num%10;
数字。按(数字);
num/=10;
}
//从堆栈顶部获取第一个数字并保存它
自动优先=0;
如果(!digits.empty())
{
first=数字。top();
digits.pop();
}
//将剩余的数字从堆栈中弹出并打印出来
而(!digits.empty())
{
STD::CUT< P>我首先要说我不是C++程序员,所以我不是说这个代码是好的,只是它工作,它遵循你的方法!< /P>
在我向您展示代码的最小编辑以获得所需内容之前,我将向您介绍如何操作,并给出一个示例:我们假设您希望将2345
转换为3452
- 您首先查找输入(
n
)的最高有效位(first
)
- 您现在需要从前面删除该数字。这很简单:
- 我们已经有了一个循环,首先将
除以10,所以让我们重用它
- 创建一个从1开始的数字(我们称之为
bigness
),每个循环将其乘以10
- 你现在有
X=54321; // 54321
log10(X) // 4.734...
floor(... ) // 4
pow(10,... ) // 10000
X/... // 5.4321
MSD=floor(... );// 5
pow(10,floor(log10(X)) // 10000
X%... // 4321
10*... // 43210
Y=MSD+... ; // 43215
#include <stack>
#include <iostream>
int main()
{
int num = 1934;
std::stack<int> digits;
// Break the number into digits, pushing them onto a stack
while (num)
{
auto digit = num % 10;
digits.push(digit);
num /= 10;
}
// Get the first digit from the top of the stack and save it
auto first = 0;
if (!digits.empty())
{
first = digits.top();
digits.pop();
}
// Pop the remaining digits off the stack and print them
while (!digits.empty())
{
std::cout << digits.top();
digits.pop();
}
// Print the first digit on the end
std::cout << first << '\n';
}
#include <iostream>
using namespace std;
int main ()
{
int n;
cin >> n;
int first = n;
int bigness = 1;
while (first >= 10)
{
first /= 10;
bigness *= 10;
}
n = n - (first * bigness);
n = (n * 10) + first;
cout << n;
}
#include <math.h> // log10, pow, floor
#include <stdio.h> // printf
int main ()
{
int n = 6945;
int p = pow(10,floor(log10(n)));
int output = (n - ((n/p)%10)*p)*10+((n/p)%10);
printf("%i",output); // outputs 9456
}
unsigned reverse_helper(unsigned x) {
if (x < 10) {
return x;
}
unsigned last = reverse_helper(x/10);
cout << x%10;
return last;
}
void reverse(unsigned x) {
cout << reverse_helper(x)) << endl;
}
int main(void) {
reverse(0);
reverse(9);
reverse(10);
reverse(1934);
reverse(1234567890);
}
0
9
01
9341
2345678901
#include<stdio.h>
#include<math.h>
int main()
{
int a,g=10,m=1,c,num;
printf("enter the number : ");
scanf("%d",&a);
c=log10(a);
num=num+((a%10)*pow(10,c));
a=a/10;
c=log10(a);
while(m<=c){
if(m==c){
num=num+((a%10)*g)+a/10;
break;
}
else{
num=num+((a%10)*g);
g=g*10;
a=a/10;
m=m+1;
}
}
printf("%d",num);
}