/*
求1+2+…+n
要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A ? B : C)。
//*/
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
class nplus{
public:
static int cnt;
static int sum;
static unsigned long plus;
nplus(){++cnt; sum += cnt;plus *= cnt;}
~nplus(){}
};
int nplus::cnt = 0;
int nplus::sum = 0;
unsigned long nplus::plus = 1;
int main()
{
nplus np[10];
cout << "sum: " << nplus::sum << " plus: " << nplus::plus << endl;
return 0;
}求1+2+…+n 要求不能使用乘除法
最新推荐文章于 2025-01-09 22:39:10 发布
本文介绍了一种不使用常见控制结构(如循环和条件判断)来实现1到n求和的方法。通过定义一个类并在构造函数中累加计数器的方式巧妙地完成了任务。

1949

被折叠的 条评论
为什么被折叠?



