简体   繁体   English

在Java中查找整数的中间数字

[英]Find middle digit in an integer in Java

I have an integer in java "1234567" and my program finds middle digit in a set of integer, is there more optimized way than below code?. 我在java“1234567”中有一个整数,我的程序在一组整数中找到中间数字,是否有比下面的代码更优化的方法? Recently asked in java interview. 最近在java采访中问道。

What I have done is first find no of digits, first, last and middle indexes. 我所做的是首先找不到数字,第一,最后和中间索引。 Then find middle digit again iterating on same integer. 然后再次找到相同整数的中间数字。 Please advice some optimization. 请建议一些优化。

int a1 = 1234567;
int a = a1;

// calculate length
int noOfDigits = 0;
while(a!=0)
{
   a = a/10;
   noOfDigits++;
}

int first = 0;
int last = noOfDigits-1;
int middle = (first+last)/2;

boolean midExists = ((a1%2)==1);
System.out.println(" digits: "+a1);
System.out.println(" no of digits "+noOfDigits);
System.out.println(" first "+first);
System.out.println(" last " + last);

if(midExists)
{
   System.out.println(" middle " + middle);
   int i = last;
   int middleDigit = 0;
   a = a1;
   while(i != middle)
   {
     a = (a / 10);
     middleDigit = (a%10);
     i--;
   }
   System.out.println("middle digit: " + middleDigit);
 }
 else
   System.out.println(" Mid not Exists.. ");

Program Output: 节目输出:

digits: 1234567
no of digits 7
first 0
last 6
middle 3
middle digit: 4

You can also do this in one pass. 您也可以一次完成此操作。 Idea is that first store the integer in the another variable. 想法是首先将integer存储在另一个变量中。 Then move two digits to the left in one integer while only one digit in the another one. 然后在一个integer向左移动两位数,而在另一个integer只移动一位数。

int a1 = 1234567;  
int a2 = a1;
int flag=0;

while(a2>0)
{
    a2/=10;               //Moves to the left by one digit
    if(a2==0)             //If there are odd no. of digits
    {
        flag=1;
        break;
    }
    a2/=10;               //Moves to the left by one digit
    a1/=10;               //Moves to the left by one digit
}
System.out.print(flag!=1?"No Mid Exists":a1%10);

Your "math" is working correctly. 你的“数学”工作正常。 The one thing you can: compute the length (number of digits) within your number upfront , to avoid "iterating" the number twice - so you can determine if that number of digits is even or odd without "iterating" the number: 您可以做的一件事: 预先计算您的号码中的长度 (位数),以避免“迭代”两次数字 - 这样您就可以确定该位数是偶数还是奇数而不 “迭代”数字:

int n = 1234;
int length = (int)(Math.log10(n)+1);

should give you 4 for 1234, and 5 for 12345. 应该给你4个1234,5个12345。

But beyond that: you can express information in different ways. 但除此之外:您可以用不同的方式表达信息。 For example: you can turn an int value into a string. 例如:您可以将int值转换为字符串。

String asStr = Integer.toString(123456);

And now: you can easily check the length of that string; 现在:您可以轻松检查该字符串的长度 ; and you can directly access the corresponding character! 并且您可以直接访问相应的角色!

The only thing to keep in mind: characters representing numbers like '1', '2', ... have different numerical values as int 1, 2, ... (see an ASCII table; as '1' is 49 when regarding its numerical value)! 唯一要记住的是:表示数字的字符如“1”,“2”,......具有不同的数值,如int 1,2,...(参见ASCII表;当关于'1'时,为49它的数值)!

this answer has less code, but wouldn't take much in performance i think: 这个答案代码较少,但我认为性能不会太大:

int a1 = 12334;
int a = a1;
int middle = 0;
int noOfDigits = 0;

while (a1 != 0) {
    a1 = a1 / 10;
    noOfDigits++;
}
if (noOfDigits % 2 == 1) {
    for (int i = 0; i < (noOfDigits / 2) + 1; i++) {
        middle = a % 10;
        a = a / 10;
    }
    System.out.println(middle);
} else {
    System.out.println("No mid existing");
}

Using only math 仅使用数学

int num = 123406789;
int countDigits = (int)Math.ceil(Math.log10(num));
int midIndex = (int)Math.ceil(countDigits/2);
int x = num / (int)Math.pow(10, midIndex);
int middleDigit = x % 10;
System.out.println(middleDigit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM