中国开发网: 论坛: 程序员情感CBD: 贴子 46061
Yxd
俺写的程序。。。。嘿嘿
// PlayLast.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace ::std;

template < long nHead, long nInc, unsigned long nSize > struct PlayLast;

template < long nHead, long nInc >
struct PlayLast< nHead, nInc, 1 >
{
enum { result = nHead };
};

template < long nHead, long nInc >
struct PlayLast< nHead, nInc, 2 >
{
enum { result = nHead + nInc };
};

template < long nHead, long nInc, unsigned long nSize >
struct PlayLast< nHead, nInc, nSize >
{
enum { result = typename PlayLast< nHead + nInc, nInc + nInc, nSize / 2 >::result };
};

int _tmain(int argc, _TCHAR* argv[])
{
cout << _T("{ 1, 2, 3, ..., 100 } last is : ");
cout << PlayLast< 1, 1, 100 >::result << endl;

cout << _T("{ 1, 6, 11, ..., 496 } last is : ");
cout << PlayLast< 1, 5, 100 >::result << endl;

cout << _T("{ 9, 6, 3, ..., -288 } last is : ");
cout << PlayLast< 9, -3, 100 >::result << endl;

return 0;
}

screen output as the following....
{ 1, 2, 3, ..., 100 } last is : 64
{ 1, 6, 11, ..., 496 } last is : 316
{ 9, 6, 3, ..., -288 } last is : -180

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录