2

我有两个号码。

First Number is 2875 &
Second Number is 852145

现在我需要一个创建第三个数字的程序。

Third Number will be 2885725145

逻辑是

First digit of third number is first digit of first number.  
Second digit of third number is first digit of second number.  
Third digit of third number is second digit of first number.  
Fourth digit of third number is second digit of second number;

很快。

如果任何数字有剩余的数字,那么最后应该附加。

我不想将 int 转换为字符串。

int CreateThirdNumber(int firstNumber, int secondNumber)
{

}

那么任何人都可以建议我解决这个问题吗?

4

6 回答 6

2

我不想将 int 转换为字符串。

为什么?

不转换为字符串

使用模数和除法运算符。

随着转换为字符串

将它们转换为字符串。使用 .Substring() 在字符串中提取和附加值。将附加的字符串转换为整数。

于 2012-05-30T10:20:58.647 回答
1

首先一点建议:如果您int在 C# 中使用,那么您的示例中的值 (2885725145) 大于int.MaxValue;(因此在这种情况下您应该使用long而不是int)。无论如何,这里是您的示例的代码,没有字符串。

        int i1 = 2875;
        int i2 = 852145;
        int i3 = 0;

        int i1len = (int)Math.Log10(i1) + 1;
        int i2len = (int)Math.Log10(i2) + 1;

        i3 = Math.Max(i1, i2) % (int)Math.Pow(10, Math.Max(i1len, i2len) - Math.Min(i1len, i2len));

        int difference = (i1len - i2len); 
        if (difference > 0)
            i1 /= (int)Math.Pow(10, difference);
        else
            i2 /= (int)Math.Pow(10, -difference);

        for (int i = 0; i < Math.Min(i1len, i2len); i++)
            {
            i3 += (i2 % 10) * (int)Math.Pow(10, Math.Max(i1len, i2len) - Math.Min(i1len, i2len) + i * 2);
            i3 += (i1 % 10) * (int)Math.Pow(10, Math.Max(i1len, i2len) - Math.Min(i1len, i2len) + i * 2 + 1);
            i1 /= 10;
            i2 /= 10;
            }
于 2012-05-30T12:19:36.743 回答
1

这里有一点可以为您提供指导:

说你有号码2875。首先,你需要确定它的长度,然后,提取第一个数字

这可以很容易地计算出来:

int iNumber = 2875;
int i = 10;
int iLength = 0;

while (iNumber % i <= iNumber){
    iLength++;
    i *= 10;
}

// iNumber is of length iLength, now get the first digit,
// using the fact that the division operator floors the result
int iDigit = iNumber / pow(10, iLength-1);
// Thats it!
于 2012-05-30T10:32:14.273 回答
0

我不明白你为什么不想使用字符串(是作业吗?)。无论如何,这是另一种可能的解决方案:

    long CreateThirdNumber(long firstNumber, long secondNumber)
    {
        long firstN = firstNumber;
        long secondN = secondNumber;
        long len1 = (long)Math.Truncate(Math.Log10(firstNumber));
        long len2 = (long)Math.Truncate(Math.Log10(secondNumber));
        long maxLen = Math.Max(len1, len2);
        long result = 0;
        long curPow = len1 + len2 + 1;
        for (int i = 0; i <= maxLen; i++)
        {
            if (len1 >= i)
            {
                long tenPwf = (long)Math.Pow(10, len1 - i);
                long firstD = firstN / tenPwf;
                firstN = firstN % tenPwf;
                result = result + firstD * (long)Math.Pow(10, curPow--);
            }
            if (len2 >= i)
            {

                long tenPws = (long)Math.Pow(10, len2 - i);
                long secondD = secondN / tenPws;
                result = result + secondD * (long)Math.Pow(10, curPow--);
                secondN = secondN % tenPws;
            }
        }
        return  result;
    }
于 2012-05-30T12:59:33.750 回答
0

这解决了它:

#include <stdio.h>

int main(void)
{

  int first = 2875,second = 852145;
  unsigned int  third =0;
  int deci,evenodd ,tmp ,f_dec,s_dec;

  f_dec = s_dec =1;
  while(first/f_dec != 0 || second/s_dec != 0) {
    if(first/f_dec != 0) {
       f_dec *=10;

    }
    if( second/s_dec != 0) {
      s_dec *= 10;
    }

  }
  s_dec /=10; f_dec/=10;


  deci = s_dec*f_dec*10;


  evenodd =0;tmp =0;
  while(f_dec != 0 || s_dec !=0 )  {
    if(evenodd%2 == 0 && f_dec !=0 ) {
      tmp = (first/f_dec);
      first -=(tmp*f_dec);

      tmp*=deci;

      third+=tmp;
      f_dec/=10;
      deci/=10;
    }

    if(evenodd%2 != 0 && s_dec != 0) {
      tmp= (second/s_dec);
      second -=(tmp*s_dec);
      //printf("tmp:%d\n",tmp);

      tmp*=deci;

      third += tmp;
      s_dec/=10;
      deci/=10;
    }

    evenodd++;
  }
  printf("third:%u\ncorrct2885725145\n",third);
return 0;

}

输出:

third:2885725145
corrct2885725145
于 2012-05-30T14:20:46.567 回答
0
#include <stdio.h>

long long int CreateThirdNumber(int firstNumber, int secondNumber){
    char first[11],second[11],third[21];
    char *p1=first, *p2=second, *p3=third;
    long long int ret;

    sprintf(first,  "%d", firstNumber);
    sprintf(second, "%d", secondNumber);
    while(1){
        if(*p1)
            *p3++=*p1++;
        if(*p2)
            *p3++=*p2++;
        if(*p1 == '\0' && *p2 == '\0')
            break;
    }
    *p3='\0';
    sscanf(third, "%lld", &ret);
    return ret;
}
int main(){
    int first = 2875;
    int second = 852145;
    long long int third;
    third = CreateThirdNumber(first, second);
    printf("%lld\n", third);

    return 0;
}
于 2012-05-30T16:06:17.343 回答