Programming algorithm: Find 1+2+...+n (constructor) code (c + +)

Source: Internet
Author: User
Tags constructor include reset

Title: Seek 1+2+...+n, the request cannot use multiplication and division method \for\while\if\else\switch\case and so on key word and Condition judgment statement (A? B:C).

You can use constructors, loop solver, use arrays to construct multiple classes, and use static variables of classes to store data.

Code:

* * * * * main.cpp * * *  Created on:2014.7.12 * 
 This column more wonderful content: http://www.bianceng.cnhttp:// www.bianceng.cn/Programming/sjjg/
 *      author:spike 
 /
      
#include <stdio.h>  
#include < stdlib.h>  
#include <string.h>  
#include <math.h>  
      
using namespace std;  
      
Class Temp {public  
:  
    temp () {++n; Sum+=n;}  
    static void Reset () {n=0; sum=0;}  
    Static size_t Getnum () {return Sum;}  
Private:  
    static size_t N;  
    static size_t Sum;  
      
size_t temp::n = 0;  
size_t temp::sum = 0;  
      
size_t Sum (size_t N) {  
    temp::reset ();  
    temp* a = new temp[n];  
    Delete[] A;  
    A = NULL;  
    return Temp::getnum ();  
}  
      
int main (void)  
{  
    size_t result = Sum;  
    printf ("result =%d\n", result);  
    return 0;  
}

Output:

result = 55

Author: csdn Blog Caroline-wendy

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.