解决数学逻辑,将数字放在第一个和第二个数字的第三个数字中

时间:2012-05-30 10:19:13

标签: c# c algorithm logic business-logic

我有两个号码。

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)
{

}

那么有人能建议我解决这个问题吗?

6 个答案:

答案 0 :(得分:2)

  

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

为什么?

不转换为字符串

  

使用模数和除法运算符。

转换为字符串

  

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

答案 1 :(得分: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!

答案 2 :(得分:1)

首先提一点建议:如果你在C#中使用int,那么你的例子中的值(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;
            }

答案 3 :(得分: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;
    }

答案 4 :(得分: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

答案 5 :(得分: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;
}