如何在C中连接两个整数

时间:2012-10-03 00:47:47

标签: c math optimization mathematical-optimization

Stack Overflow已经用许多其他语言回答了这个问题,但没有回答C.所以我想我会问,因为我有同样的问题。

如何在C中连接两个整数?

示例:

x = 11;
y = 11;

我想z如下:

z = 1111;

其他示例尝试使用字符串执行此操作。没有字符串的方法是什么?

我正在寻找一种在C中执行此操作的有效方法,因为在我的特定用法中,这将成为代码的时间关键部分。

提前致谢!

9 个答案:

答案 0 :(得分:35)

unsigned concatenate(unsigned x, unsigned y) {
    unsigned pow = 10;
    while(y >= pow)
        pow *= 10;
    return x * pow + y;        
}

compilation/correctness/speed

的证明

我避免使用log10pow函数,因为我很确定它们使用浮点并且速度很慢,因此可能在您的计算机上更快。也许。轮廓。

答案 1 :(得分:9)

z = x * pow(10, log10(y)+1) + y;

<强>解释

首先,您获得应该变为第二的变量的位数:

int digits = log10(y)+1;  // will be 2 in your example

然后通过将其与10 ^位相乘来“移动”另一个变量。

int shifted = x * pow(10, digits);   // will be 1100 in your example

最后添加第二个变量:

z = shifted + y;   // 1111

或者在一行中:

z = x * pow(10, (int)log10(y)+1) + y;

答案 2 :(得分:2)

int myPow(int x, int p)
{
     if (p == 0) return 1;
     if (p == 1) return x;

     int tmp = myPow(x, p/2);
     if (p%2 == 0) return tmp * tmp;
     else return x * tmp * tmp;
}
int power = log10(y);
z = x*myPow(10,power+1)+y;

在这里,我无耻地从https://stackoverflow.com/a/1505791/1194873

复制了myPow

答案 3 :(得分:2)

这可能不是一个最佳或快速的解决方案,但没有人提及它,这是一个简单的解决方案,可能会有用。

您可以使用sprintf()strtol()

char str[100];
int i=32, j=45;
sprintf(str, "%d%d", i, j);
int result=strtol(str, NULL, 10);

首先使用sprintf()向字符串写入数字1后跟另一个字符串(就像您将使用printf()打印到stdout一样),然后将结果字符串转换为带{{ 1}}。

strtol()会返回strtol(),其值可能大于long中可存储的值,因此您可能需要先检查结果值。

int

如果int result; long rv=strtol(str, NULL, 10); if(rv>INT_MAX || rv<INT_MIN || errno==ERANGE) { perror("Something went wrong."); } else { result=rv; } 返回的值不在strtol()范围内(即,不在(包括)intINT_MIN之间),则会发生错误。 INT_MAXINT_MIN来自INT_MAX

如果字符串中的值太大而无法在limits.h中表示,long将被设置为errno(来自ERANGE),因为溢出

了解errno.h here

编辑:

正如chqrlie提到的启发性评论所指出的那样,负数会导致这种方法出现问题。

你可以使用这个或修改它来解决这个问题

strtol()

首先将第二个数字及其符号打印到字符数组char str[100], temp[50]; int i=-32, j=45, result; sprintf(temp, "%+d", j); sprintf(str, "%d%s", i, temp+1); long rv=strtol(str, NULL, 10);

temp中的+会导致打印号码的标志。

现在将第一个数字和第二个数字打印到%+d但没有第二个数字的符号部分。我们忽略str中的第一个字符,跳过第二个数字的符号部分。

最后temp完成了。

答案 4 :(得分:1)

这是另一种方法:

int concat(int x, int y) {
    int temp = y;
    while (y != 0) {
        x *= 10;
        y /= 10;
    }
    return x + temp;
}

谁知道你会得到什么样的表现。试着看看..

答案 5 :(得分:1)

您还可以使用宏来合并字符串(简便方法)

#include<stdio.h>
#define change(a,b) a##b
int main()
 {
    int y;
    y=change(12,34);
    printf("%d",y);
    return 0;
 }

它有一个缺点。我们无法在此方法中传递参数

答案 6 :(得分:0)

也许这会奏效:

int x=11,y=11,temp=0;
int z=x;
while(y>0)
{
    // take reciprocal of y into temp
    temp=(temp*10)+(y%10);       
    y=y/10;
}
while(temp>0)
{
    // take each number from last of temp and add to last of z
    z=(z*10)+(temp%10);      
    temp=temp/10;
}

代码很冗长,但很简单。 如果有任何错误,请纠正我。

答案 7 :(得分:0)

这是@Mooing Duck的答案的变体,它使用查找表为10的倍数(对于缓慢整数乘法的平台)它还返回无符号long long以允许更大的值并在查找表中使用unsigned long long考虑@ chqrlie关于无限循环的评论。如果组合输入可以保证不超过无符号,那么可以改变它们。

static const unsigned long long pow10s[] = {
   10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000
};
unsigned long long concat(unsigned x, unsigned y) {
    const unsigned long long *p = pow10s;
    while (y >= *p) ++p;
    return *p * x +y;        
}

答案 8 :(得分:-3)

#include<iostream>
using namespace std;

int main()
{
    int a=0,b=0,count=0,c=0,t=0;
    cout<<"enter 2 no"<<endl;
    cin>>a>>b;
    t=b;

    while(b!=0)
    {
        b=b/10;
        count++;
    }

    while(count!=0)
    {
        a=a*10;
        count--;
        c=a+t;
    }

    cout<<"concate no is:"<<c;
}
相关问题