[英]Recursive method that prints all of the digits in a number that or greater than the first digit and lower than the last digit
The problem: 问题:
I'm trying to write a recursive method that prints all of the digits in a number that or greater than the first digit and lower than the last digit. 我正在尝试编写一个递归方法,该方法打印的数字大于或等于第一个数字并且低于最后一个数字。 I accomplished to write a recursive method that prints all of the digits that or lower then the last digit.
我完成了写一个递归方法,打印所有数字或低于最后一位数字。 I can't figure out how to check if the digit is grater then the first digit.
我无法弄清楚如何检查数字是否比第一个数字更重要。
Examples: 例子:
For print(325648) it will print 5, 6, 4. 对于打印(325648),它将打印5,6,4。
For print(237) it will print 3. 对于打印(237),它将打印3。
For print(925648) it will not print any digit. 对于打印(925648),它不会打印任何数字。
This is my code: 这是我的代码:
public static void print(int n) {
print(n,n%10);
}
private static void print(int n,int lastDigit) {
if(n==0)
return;
if(n%10<lastDigit)
System.out.println(n%10);
print(n/10,lastDigit);
}
The requirement of this method: 这种方法的要求:
Please note: This is not a homework assignment! 请注意:这不是家庭作业! I'm writing this method as a practice to a exam that i am talking tomorrow.
我正在写这种方法作为我明天在说的考试的练习。
The idea here is to recurse by dividing by 10 until the number is reduced to its first digit. 这里的想法是通过除以10递归,直到数字减少到第一个数字。 On the return through all recursions you have the first digit as a return value and compare quite easily.
在返回所有递归时,您将第一个数字作为返回值并进行比较。
private static void print( int n ){
print( n/10, n%10 );
}
private static int print( int n, int ld ){
if( n < 10 ) return n;
int digit = n % 10;
int first = print( n/10, ld );
if( digit < ld && digit > first )
System.out.println( digit );
return first;
}
public static int firstDigit(int n) {
if(n == 0) {
return 0;
}
if(n != 0 && n / 10 == 0) {
return n;
}
return firstDigit(n/10);
}
public static void print(int n) {
print(n, firstDigit(n), n%10);
}
private static void print(int n, int firstDigit, int lastDigit) {
if(n == 0)
return;
if(n % 10 < lastDigit && n % 10 > firstDigit)
System.out.println(n%10);
print(n/10, firstDigit, lastDigit);
}
Pass the first digit as a parameter. 将第一个数字作为参数传递。
public static int firstDigit(int n) {
return (int) (n / Math.pow(10, Math.floor(Math.log10(n))));
}
public static void print(int n) {
print(n, firstDigit(n), n%10);
}
private static void print(int n, int firstDigit, int lastDigit) {
if(n == 0)
return;
if(n % 10 < lastDigit && n % 10 > firstDigit)
System.out.println(n%10);
print(n/10, lastDigit);
}
EDIT: You can calculate the first digit through recursion too: 编辑:您也可以通过递归计算第一个数字:
public static int firstDigit(int n) {
if (n / 10 == 0) {
return n;
} else {
return firstDigit(n / 10);
}
}
Very simple, almost your code 非常简单,几乎是你的代码
public static void print(int n) {
int first = Integer.parseInt(Integer.toString(n).substring(0, 1));
print(n, first, n%10);
}
private static void print(int n, int first, int lastDigit) {
if(n==0)
return;
if(n%10<lastDigit && n%10 > first)
System.out.println(n%10);
print(n/10, first, lastDigit);
}
An alternative solution is to represent the number to be checked as string: 另一种解决方案是将要检查的数字表示为字符串:
public static void main(String[] args) throws IOException {
int n = 325648;
print(n);
}
private static void print(int n) {
String intAsString = String.valueOf(n);
int numberLength = intAsString.length();
if( numberLength < 3) {
System.out.println("Can't do with ints shorted than 3 digits");
}
int firstDigit = Integer.valueOf(intAsString.substring(0,1));
int lastDigit = Integer.valueOf(intAsString.substring(numberLength-1,numberLength));
print(intAsString, 0, firstDigit, lastDigit );
}
private static void print(String intAsString,int index, int firstDigit, int lastDigit) {
int digit = Integer.valueOf(intAsString.substring(index,index+1));
if((digit > firstDigit) && (digit < lastDigit)) {
System.out.print(digit);
}
if((index +1) < intAsString.length()) {
print(intAsString,++index, firstDigit, lastDigit);
}
}
I just tried with string and array compare, I using string because I want code to be bit flexible. 我只是尝试使用字符串和数组比较,我使用字符串,因为我希望代码有点灵活。
public class Data {
public static void main(String[] args) {
// TODO Auto-generated method stub
print(3245);
}
static void print(int n){
String s = n+"";
String[] s1 = s.split("");
printF(Integer.valueOf(s1[0]),Integer.valueOf(s1[s1.length-1]),s1,0);
}
static void printF(int lst,int end, String[] ar,int index){
if(Integer.valueOf(ar[index])>lst && Integer.valueOf(ar[index])<end){
System.out.println(ar[index]);
}
if(index < ar.length-1){
index = index+1;
Data.printF(lst,end, ar,index);
}
}
}
Following the test output of above written code: 按照上面编写的代码的测试输出:
Input: 3245
output: 4
Input:1234567
output:23456
Input:58697
output:8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.