China MOOC_ 0 Basic Java language _ 7th week function _2 End number

Source: Internet
Author: User

2 Finish (5 points)

Topic content:

The factor of a positive integer is all positive integers that can be divisible by it. And if a number is exactly equal to the sum of factors other than itself, this number is called the end number. For example 6=1+2+3 (6 of the factor is the one-to-be).

Now you are going to write a program that reads two positive integers n and M (1<=n<m<1000) and outputs all the n,m in the range.

Tip: You can write a function to determine whether a number is complete.

Input format:

Two positive integers, separated by a space.

Output format:

In the meantime, all the numbers are separated by a space, and the last number has no space behind it. If not, a blank line is output.

Input Sample:

1 10

Sample output:

6

time limit: 500ms memory limit: 32000kb
Import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner sc = new Scanner (system.in);  T count = 0;//counter int n = 0;//reads in two positive integers n and m (1<=n<m<1000) int m = 0;n = Sc.nextint (); m = Sc.nextint (); for (int i = n; I <= m; i++) {if (Iswanshu (i)) {count++;if ((count! = 0) && (count! = 1)) {System.out.print ("");} System.out.print (i);}}} public static Boolean Iswanshu (int number) {//hint: You can write a function to determine whether a number is the end of a Boolean Wanshu = false;//is the sum of int sums = 0;//factor and for (int i = 1; i < number; i++) {if (number% i = = 0) {sum + = i;}} if (sum = = number) The factor of {//A positive integer is all positive integers that can be divisible by it. And if a number is exactly equal to the sum of factors other than itself, this number is called the end number. For example 6=1+2+3 (6 of the factor is the one-to-be). Wanshu = true;} else {Wanshu = false;} return Wanshu;}}

China MOOC_ 0 Basic Java language _ 7th week function _2 End number

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.