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 template class solver, input template parameters, recursive calls, each recursive value minus 1 to the template parameter of 1 o'clock, display call End template class.
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>
template <size_t n> struct Sum { C14/>enum Value {N = Sum<n-1>::n + n};
Template <> struct sum<1> {
enum Value {n=1};
}
; int main (void)
{
size_t result = sum<10>::n;
printf ("result =%d\n", result);
return 0;
}
Output:
result = 55
Author: csdn Blog Caroline-wendy