1

Let's say that I have the following number: 1286 now I want to remove the last digit 6 and end up with the first 3 digits 128 only. I also would like to do this using the number 6 and the number 1286 only as inputs.

Also if there is a c# solution would be great. Thanks

Edit: I want to do this using a math equation between the 6 and the 1286, I already know how to parse strings, which is not what I'm after.

4

3 回答 3

1

请尝试以下代码:(这仅使用数学函数完成,我也不懂 C#)

    java.util.Scanner s=new java.util.Scanner(System.in);
    int last=s.nextInt();
    int firstNumber=s.nextInt();
    int ans=0;
    loop:
    for(int temp=firstNumber,i=0;temp>0;temp/=10)
    {
        if(temp%10==last){ans=temp/10;while(i>0){ans=ans*10+(i%10);i/=10;} break loop;}
        i=i*10;
        i=i+(temp%10);
    }
    if(ans>0)System.out.println(ans);

    }
}
于 2016-10-26T19:19:06.707 回答
0
string input = "OneTwoThree";

// Get first three characters
string sub = input.Substring(0, 3);

sub现在将有字符串中的前 3 个字符,0 是开始位置,然后你想要多少个字符(即:3) - 这是 (0, 3) 进入它的地方 - 如果你有 ( 3, 3), sub 等于 "Two"

我想这可能是你正在寻找的:)

于 2012-06-07T16:54:16.730 回答
0

在 JavaScript 中:这是您的电话号码:

var num = 1286;

此行删除最后一位数字:

num % 10; //returns 6

这两行删除了 6:

num /= 10 // turns num to 128.6
num = Math.trunc(num) // num now equals 128

更好的是,您可以将它放在一个函数中,如下所示:

function sumOfDigits(num) {
  const sumArr = [];
  while (num > 0) {
    sumArr.push(num % 10);
    num /= 10;
    num = Math.trunc(num);
  }
  return sumArr.reduce((a, b) => a + b, 0);
}

sumOfDigits(1234);
// returns 10

希望这可以帮助。

于 2017-10-12T17:24:51.103 回答