C++模板求n!与1+2+...n

//求n!与1+2+...n
#include <iostream>
using namespace std;
template<int _N=0>
class A
{
    public:
    //static int const result = 3*A<_N-1>::result;
    enum{result=_N*A<_N-1>::result};//求n!
    //enum{result=_N+A<_N-1>::result};//求1+...n。
};

template<>
class A<1>
{
    public:
    //static int const result = 1;
    enum{result=1};
};
int main()
{
    cout<<A<3>::result<<endl;
    return 0;
}
时间: 2024-08-29 15:23:02

C++模板求n!与1+2+...n的相关文章

第十六周oj刷题——Problem K: 填空题:类模板---求数组的最大值

Description 类模板---求数组的最大值 找出一个数组中的元素的最大值,数组大小为10.(用类模板来实现) 数组元素类型作为类模板的参数. Input 10个int型数据 10个double型数据 10个char型数据 10gestring型数据 Output 10个int型数据的最大值 10个double型数据的最大值 10个char型数据的最大值 10个string型数据的最大值 Sample Input 1 3 5 7 9 8 6 4 2 0 1.2 3.4 5.66 7.8 9

OJ刷题之《函数模板--求n个数之和》

题目描述 利用函数模板求4个数的和. 部分代码已给定如下,只需要提交缺失的代码. #include <iostream> using namespace std; /* 补充缺少代码 */ int main() { double result; unsigned char c1,c2,c3,c4; cin>>c1>>c2>>c3>>c4; result = sum<unsigned char>(c1,c2,c3,c4); cout&l

AtCoder Beginner Contest 142【D题】【判断素数的模板+求一个数的因子的模板】

D - Disjoint Set of Common Divisors Problem Statement Given are positive integers AA and BB. Let us choose some number of positive common divisors of AA and BB. Here, any two of the chosen divisors must be coprime. At most, how many divisors can we c

模板 求GCD&amp;LCM

求最大公倍数 1 int GCD(int a,int b) 2 { 3 if(a % b == 0) return b; 4 else 5 return GCD(b,a%b); 6 } 求最小公倍数 1 int LCM(int a,int b) 2 { 3 return a*a/GCD(a,b); //最小公倍数等于两数乘积除以最大公约数 4 }

KMP算法模板 求子串和模板串首先匹配的位置

1 #include <cstdio> 2 using namespace std; 3 4 const int MAXN = 1e6 + 10; 5 int nex[MAXN]; 6 int s[MAXN], t[MAXN]; 7 8 void get_nex(int lm) { 9 int i = 0, j = -1; nex[0] = -1; 10 while (i < lm) { 11 if (j == -1 || t[j] == t[i]) { 12 i++; j++; nex

主席树|求区间第k小模板

主席树 学了主席树,用来求区间上的第k小 写一下自己整理后的模板 求区间第k小 #include<bits/stdc++.h> using namespace std; //求区间第k小 const int maxn = 500010; struct node{ int v,lc,rc; }T[maxn * 21]; int n,m; int root[maxn]; int e; void insert(int pre,int cur,int pos,int l,int r){ if(l ==

sdut 面向对象程序设计上机练习二(函数模板)

面向对象程序设计上机练习二(函数模板) Time Limit: 1000MS Memory limit: 65536K 题目描述 利用数组和函数模板求5个数最大值(分别考虑整数.单精度.长整数的情况). 输入 分别输入5个int型整数.5个float 型实数.5个long型正整数. 输出 分别输出5个int型整数的最大值.5个float 型实数的最大值.5个long型正整数的最大值. 示例输入 11 22 666 44 55 11.11 22.22 33.33 888.88 55.55 1234

面向对象程序设计上机练习二(函数模板)

面向对象程序设计上机练习二(函数模板) Time Limit: 1000MS Memory limit: 65536K 题目描述 利用数组和函数模板求5个数最大值(分别考虑整数.单精度.长整数的情况). 输入 分别输入5个int型整数.5个float 型实数.5个long型正整数. 输出 分别输出5个int型整数的最大值.5个float 型实数的最大值.5个long型正整数的最大值. 示例输入 11 22 666 44 55 11.11 22.22 33.33 888.88 55.55 1234

UVA - 10298 Power Strings (KMP求字符串循环节)

Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiati