The algorithm of finding 1+2+...+n

Source: Internet
Author: User

Topic Description:

For 1+2+3+...+n, it is not possible to use the key words such as multiplication and division, for, while, if, else, switch and case, etc. B:C).

Input:

The input may contain more than one test sample. For each test case, enter an integer n (1<= n<=100000).

Output:

For each test case, output the 1+2+3+...+n value.

Sample input:

3
5
  

Sample output:

6
15

This topic is really boring, try to write in the Java language, the construction method, finally, there is no way, or to use for, the construction of an object array is not false, but that people call is to create the array reference object constructor, but also to instantiate the object n times in the array, and forced me to use for loop, this is just, The test results timed out, and the key is that the time for each test case does not timeout, but the total time has expired.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Do not want to waste time on this boring topic, just like this, there is a time no Java, hands are born, in addition, the use of Java constructor ideas to do this topic, there is no for the loop can calculate the answer, welcome hint, really do not want to think!

Anyway, put the code on it!

Import java.util.*;  
public class main{public  
    static void Main (String args[]) {  
        Scanner cin = new Scanner (system.in);  
        int n;  
        while (Cin.hasnext ()) {  
            n = cin.nextint ();   
            Temp.reset ();  
            Temp arr[] = new Temp[n];  
            for (int i=0;i<n;i++)  
                arr[i] = new Temp ();  
            System.out.println (Temp.getsum ());  
      
}} Class temp{  
    private static int n = 0;  
    private static int sum = 0;   
    public static void Reset () {  
        n = 0;  
        sum = 0;  
    }  
    Public Temp () {  
        ++n;  
        sum = n;  
    }  
    public static int Getsum () {return  
        sum;  
    }  
}

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.