C++ 将两个数字合二为一。示例:123 和 456 变成 123456
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2611081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combine two numbers into one. Example: 123 and 456 become 123456
提问by jiake
In C++, how do I combine (note: not add) two integers into one big integer?
在 C++ 中,如何将两个整数组合(注意:不是相加)成一个大整数?
For example:
例如:
int1 = 123;
int2 = 456;
Is there a function to take the two numbers and turn intCombined into 123456?
有没有函数可以将两个数字转换为 intCombined 为 123456?
EDIT:
编辑:
My bad for not explaining clearly. If int2 is 0, then the answer should be 123, not 1230. In actuality though, int1 (the number on the left side) would only have a value if int2 goes over the 32 bit limit. So when int2 is 0, then int1 is 0 (or garbage, i'm not sure).
我不好解释清楚。如果 int2 为 0,那么答案应该是 123,而不是 1230。但实际上,int1(左侧的数字)只有在 int2 超过 32 位限制时才会有值。因此,当 int2 为 0 时,则 int1 为 0(或垃圾,我不确定)。
回答by sth
The power of ten, that you need to multiply the first number with, is the smallest one, that is bigger than the second number:
需要与第一个数字相乘的十的幂是最小的,即大于第二个数字:
int combine(int a, int b) {
int times = 1;
while (times <= b)
times *= 10;
return a*times + b;
}
回答by Jan Johansen
You could convert them into strings, combine them and then convert them back to an int?
您可以将它们转换为字符串,组合它们,然后将它们转换回 int?
回答by R Samuel Klatchko
For each digit in int2
, you can multiple int1
by 10 and then add int2
:
对于 中的每个数字int2
,您可以int1
乘以 10,然后添加int2
:
// merge(123, 0) => 1230
int merge(int int1, int int2)
{
int int2_copy = int2;
do
{
int1 *= 10;
int2_copy /= 10;
} while (int2_copy);
return int1 + int2;
}
You could get rid of the loop using log10
and ceil
.
您可以使用log10
and摆脱循环ceil
。
回答by Noel
If the numbers you are trying to combine are positive integers you can use pairing Functions.
如果您尝试组合的数字是正整数,您可以使用配对 Functions。
Pairing function creates a unique number from two. It is also a reversible function.
配对功能从两个创建一个唯一的数字。它也是一个可逆函数。
x,y -> z
z -> x,y
z = (x+y)(x+y+1)/2 + y
Then the reverse is:
那么反过来就是:
w = floor((sqrt(8z+1)-1)/2)
t = (w*w + w)/2
y = z - t
x = w - y
Note. The above is not in any specific language. Just some math...
笔记。以上不是任何特定语言。只是一些数学...
回答by csj
Assuming both ints are non-negative, and int1 goes on the left and int2 goes on the right, you need to figure out how many digits long int2 is, multiply int1 by 10 a bunch of times, and then add them.
假设两个int都是非负的,int1在左边,int2在右边,你需要算出int2的长度是多少,将int1乘以10一堆,然后相加。
unsigned int int1 = blah;
unsigned int int2 = blah;
unsigned int temp = int2;
do
{
temp /= 10;
int1 *= 10;
} while (temp >0)
unsigned int newInt = int1 + int2;
回答by wheaties
You could use stringstream:
您可以使用字符串流:
string Append(int _1, int _2){
stringstream converter;
converter << _1 << _2;
return converter.str();
}
then call atoi
on the returned string.
然后调用atoi
返回的字符串。
回答by Clifford
The following is essentially sth's accepted solution but with the b==0 fix, and the loop replaced with an expression to calculate the scale directly:
以下本质上是 sth 接受的解决方案,但使用 b==0 修复,并且循环替换为表达式以直接计算比例:
#include <math.h>
int combine(int a, int b)
{
int times = 1;
if( b != 0 )
{
times = (int)pow(10.0, (double)((int)log10((double)b)) + 1.0);
}
return a * times + b ;
}
In some circumstances (such as a target with an FPU and a good maths library) the expression might be faster than the loop, but I have not tested that hypothesis.
在某些情况下(例如具有 FPU 和良好数学库的目标),表达式可能比循环更快,但我尚未测试该假设。
回答by Juliano
Another option that works for C too:
另一个也适用于 C 的选项:
#include <stdio.h>
int CombineInt(int int1, int int2)
{
char cResult[32];
sprintf(cResult, "%d%d", int1, int2);
return atoi(cResult);
}
回答by Mehtab Khan
#include <iostream>
using namespace std;
int main()
{
int num1,num2,comb,a,c;
cout << "Enter the 1st numbers" << endl;
cin>>num1;
cout << "Enter the 2st numbers" << endl;
cin>>num2;
a=num2/10;
if(a<=9){
c=num1*100;
comb=c+num2;
cout<<"The combination of the two numbers is "<<comb;
}
else if(a>9&&a<=19){
c=num1*1000;
comb=c+num2;
cout<<"The combination of the two numbers is "<<comb<<endl;
}
else if(a>19&&a<=29){
c=num1*10000;
comb=c+num2;
cout<<"The combination of the two numbers is "<<comb<<endl;
}
return 0;
}