Java face question 1

Source: Internet
Author: User
Tags mul

1. ask for a 1calculation! +2! +......+60! The result?

If you want to calculate data, then you must first have a data type, this time can only use Double The data type.

Implement one: done directly through the code

Public class testdemo{
public static void Main (string[] args) {

Double sum = 0.0;// Save the final calculation result
         for (int x =1;x<=60;x++) {
             double temp = 1.0;//save results for each factorial
             for (int y = 1;y <= x;y++) {
                 temp *= y;//calculating factorial
             }
             sum += temp;
        
        }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("calculation result" +sum);
    
    }


}

Implementation two: Use recursion to complete, if you want to use recursion to complete the need two times recursion

Recursive operation one: responsible for the factorial operation of each data;

Recursive operation two: Add the results of each recursive operation

Public classtestdemo{
public static void Main (string[] args) {

System.out.println ("Calculation result "+sum (60));

}

public static double sum (int num) {
if (num==1) {
return 1;
}else{

return Mul (num) +sum (num-1);
}

}
public static double mul (int num) {
if (num = = 1) {
return 1;
}else{

Return Num*mul (num-1);
}

}

}

2. write a method that can turn an integer into a binary output.

Implement one: using common code to implement

public class testdemo{
public static void Main (string[] args) {
int num = 10;//10010

String str = "";// define string Save
while (num!=0) {// already has no value

str= (num%2) +str;

NUM=NUM/2;

}
System.out.println (str);
}


implementation two: by Recursive implementation

public class testdemo{
public static void Main (string[] args) {
Tobinary (18);

}
public static voidtobinary (int num) {

If (num==0) {

Return;

}

Tobinary (NUM/2);

System.out.println (num%2);

}
}

3.

5 people sat together and asked how old the 5th person was, and he said he was 2 years older than the 4th person. Asked how old the 4th person was, he said he was 2 years older than the 3rd man. Asked how old the 3rd person was, he said he was 2 years older than the 2nd man. Asked how old the 2nd person was, he said he was 2 years older than the 1th man. Asked how old the 1th person was, he said it was 8 years old. How old is the 5th one?

Implementation one: using plain code implementation

public class testdemo{
public static void Main (string[] args) {
int age = 8;

for (int x=0;x<4;x++) {

Age + = 2;

}

System.out.println (age);

}
}

Implementing two: Using recursive implementations

public class testdemo{
public static void Main (string[] args) {
Age (5);

}

public static int age (int num) {

if (num==1) {

RETURN8;

}

Return 2+age (num-1);

}
}


This article is from the "10281302" blog, please be sure to keep this source http://10291302.blog.51cto.com/10281302/1688314

Java face question 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.