如何优雅地利用C++编程从1乘到20?
ACM比赛整理
2022-03-06 01:44
来自:知乎,作者:小白白
链接:https://www.zhihu.com/question/365763395/answer/971009059
知乎的一个问题:
答主:小白白。
数学家版本:
(简单利索,深藏功与名)
#include
#include
int main()
{
std::cout << std::tgamma(20 + 1) << std::endl;
}
语言学家版本:
(语言学家,你懂得,恨不得把所有语法特性都派上用场)
#include
#include
template<std::size_t...I> constexpr auto foo(std::index_sequence) { return ((I+1) * ...); }
int main()
{
std::cout << foo(std::make_index_sequence<20>()) << std::endl;
}
历史学家版本:
(void main() 有没有嗅到浓厚的历史气息?)
#include
void main(void) {
int i;
long long j;
for(i = 1, j = 1;i <= 20; j *= i++);
printf("%lld", j);
}
敏捷开发上线1.0版本:
(可以说是非常敏捷了)
#include
int main() {
//printf("%d", 1*2*3*4*5*6*7*8*9*10);
printf("%lld", (long long)1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
}
面向对象专家版本:
(好家伙,一个简单的问题,愣是祭出了接口、继承、虚函数、虚继承、智能指针等一大堆东西出来,这很面向对象)
#include
#include
#include
struct IBaseInterface {
virtual ~IBaseInterface() = 0;
};
inline IBaseInterface::~IBaseInterface() = default;
struct IDataProvider : virtual public IBaseInterface {
virtual int first() = 0;
virtual int last() = 0;
virtual int next(int v) = 0;
};
struct ICalculator : virtual public IBaseInterface {
virtual long long calc(IDataProvider *) = 0;
};
struct IPrinter : virtual public IBaseInterface {
virtual void print(const std::string &) = 0;
};
struct ISerializer : virtual public IBaseInterface {
virtual std::string serialize(long long value) = 0;
};
struct IRunnable : virtual public IBaseInterface {
virtual void run() = 0;
};
class Foo : virtual public IRunnable {
std::shared_ptr m_dp;
std::shared_ptr m_c;
std::shared_ptr m_s;
std::shared_ptr m_p;
public:
Foo(std::shared_ptr dp, std::shared_ptr c, std::shared_ptr s, std::shared_ptr p) : m_dp(std::move(dp)), m_c(std::move(c)), m_s(std::move(s)),m_p(std::move(p)) {}
void run() override { return m_p->print(m_s->serialize(m_c->calc(m_dp.get()))); }
};
class DefaultSerializer : virtual public ISerializer {
public:
std::string serialize(long long value) override { return std::to_string(value); }
};
class StreamPrinter : virtual public IPrinter {
std::ostream &m_os;
public:
explicit StreamPrinter (std::ostream &os) : m_os(os) {}
void print(const std::string &s) override { m_os << s << std::endl; }
};
class MultiplyAccumulateCalculator : virtual public ICalculator {
public:
long long calc(IDataProvider *dp) override {
int i = dp->first();
long long j = i;
do
j *= (i = dp->next(i));
while(i != dp->last());
return j;
}
};
int main() {
struct MyDataProvider : virtual public IDataProvider {
int first() override { return 1; }
int last() override { return 20; }
int next(int v) override { return v+1; }
};
Foo foo(std::make_shared(), std::make_shared(), std::make_shared(), std::make_shared(std::cout)) ;
foo.run();
}
提前优化的并行版本:
(一看就是精通底层技术的大佬,把CPU拿捏得死死的)
#include
#include
double foo(int x) {
__m128 a = {1.0f, 2.0f, 3.0f, 4.0f};
__m128 b = {4.0f, 4.0f, 4.0f, 4.0f};
__m128 c = {1.0f, 1.0f, 1.0f, 1.0f};
for(int i = 0; i < x / 4; ++i, a = _mm_add_ps(a, b))
c = _mm_mul_ps(c, a);
for(int i = x % 4; i < 4; ++i)
a[i] = 1.0f;
c = _mm_mul_ps(c, a);
return (double)c[0] * (double)c[1] * (double)c[2] * (double)c[3];
}
int main() {
std::cout << foo(20) << std::endl;
}
黑魔法版本:
(能看懂这段代码的,都不是普通人!)
#include
#include
#include
#include
int main() {
std::vector<int> v(std::atoi(std::end(__DATE__) - (__LINE__) / 2) - 1); // 2021年,第六行
std::iota(v.begin(), v.end(), 1);
std::cout << std::accumulate(v.begin(), v.end(), 1ull, std::multiplies<>()) << std::endl;
}
“宏孩儿”元编程版:
(当年看各种C++框架中,排山倒海一样的宏定义,简直令人发指)
#include
// 由于boost.preprocessor仅提供255以下的整数运算
// 所以使用sequence来 (十位个位)(千位百位)(十万位万位) 的方式来表示大整数。
// 不进位加法:(77)(66)(55) + (44)(33)(22) = (121)(99)(77)
#define PP_ADD_N_N_CARRY_OP(R, DATA, I, ELEM) (BOOST_PP_ADD(BOOST_PP_SEQ_ELEM(I, DATA), ELEM))
#define PP_ADD_N_N_CARRY(SEQ_A, SEQ_B) BOOST_PP_SEQ_FOR_EACH_I(PP_ADD_N_N_CARRY_OP, SEQ_A, SEQ_B)
// 进位加法:(121)(99)(77) = (21)(0)(78)
// 注意SEQ_A的长度要比SEQ_B长
#define PP_ADD_N_N_OP(S, STATE, ELEM_CARRY) \
BOOST_PP_SEQ_PUSH_FRONT( \
BOOST_PP_SEQ_REPLACE(STATE, 0, BOOST_PP_MOD(BOOST_PP_ADD(BOOST_PP_SEQ_HEAD(STATE), ELEM_CARRY), 100)), \
BOOST_PP_DIV(BOOST_PP_ADD(BOOST_PP_SEQ_HEAD(STATE), ELEM_CARRY), 100) \
)
#define PP_ADD_N_N(SEQ_A, SEQ_B) BOOST_PP_SEQ_REVERSE(BOOST_PP_SEQ_FOLD_LEFT(PP_ADD_N_N_OP, BOOST_PP_SEQ_NIL(0), PP_ADD_N_N_CARRY(SEQ_A, SEQ_B)))
// 没什么好说的,X*N = X+X+X+X+X+...+X
#define PP_MUL_N_1_EXP_OP(Z, I, DATA) (DATA)
#define PP_MUL_N_1_EXP(SEQ_N, N) BOOST_PP_REPEAT(N, PP_MUL_N_1_EXP_OP, SEQ_N)
#define PP_MUL_N_1_MYOP(S, STATE, ITEM) PP_ADD_N_N(STATE, ITEM)
#define PP_MUL_N_1_FWD(EXP) BOOST_PP_SEQ_FOLD_LEFT(PP_MUL_N_1_MYOP, BOOST_PP_SEQ_HEAD(EXP), BOOST_PP_SEQ_TAIL(EXP))
#define PP_MUL_N_1(SEQ_N, N) PP_MUL_N_1_FWD(PP_MUL_N_1_EXP(SEQ_N, N))
#define FACT5 PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1((1), 2), 3), 4), 5)
#define FACT10 PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(FACT5, 6), 7), 8), 9), 10)
#define FACT15 PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(FACT10, 11), 12), 13), 14), 15)
#define FACT20 PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(FACT15, 16), 17), 18), 19), 20)
#define FACT25 PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(PP_MUL_N_1(FACT20, 21), 22), 23), 24), 25)
static_assert(false, BOOST_PP_STRINGIZE(FACT10));
真·模板元编程版本
(泛型编程,码不惊人死不休)
#include
#include
#include
using BaseType_t = long long;
constexpr BaseType_t lgBase = 9; // 注意10000*10000刚刚好小于int的取值范围
constexpr BaseType_t Base = 1000000000; // 注意10000*10000刚刚好小于int的取值范围
// 大整数的表示
template struct BigInteger {
using type = BigInteger;
};
// 连接
template<class T1, class T2> struct BI_Cat;
template struct BI_Cat , BigInteger> : BigInteger {};
// 左移一个单元(即*Base)
template<class T> struct BI_SHL;
template struct BI_SHL> : BigInteger0> {};
// 去除开头的0
template<class T> struct BI_Remove_Zeros : T {};
template struct BI_Remove_Zeros> : BI_Remove_Zeros> {};
// 填充0到N个单元
template<int X, class IS> struct BI_Fill_Impl;
template<int X, class T, T...I> struct BI_Fill_Impl> : BigInteger<(I, X)...> {};
template<int Size> struct BI_Fill_Zeros : BI_Fill_Impl<0, std::make_index_sequence> {};
template<class T, int N> struct BI_Resize;
templateint N> struct BI_Resize, N> : BI_Cat<typename BI_Fill_Zerossizeof...(I)>::type, BigInteger> {};
// 返回较大的数值
template<int A, int B> struct int_min : std::integral_constant<int, (A {};
// 非进位加法:先把两个数的位数改成一样的然后依次相加
template<class A, class B, class ShouldResize> struct BI_AddNotCarry_Impl;
template struct BI_AddNotCarry_Impl , BigInteger, std::true_type> : BigInteger<(I1 + I2)...> {};
template struct BI_AddNotCarry_Impl , BigInteger, std::false_type>
: BI_AddNotCarry_Impl<
typename BI_Resize, int_min<sizeof...(I1), sizeof...(I2)>::value>::type,
typename BI_Resize, int_min<sizeof...(I1), sizeof...(I2)>::value>::type,
std::true_type
>{};
template<class A, class B> struct BI_AddNotCarry;
template struct BI_AddNotCarry , BigInteger>
: BI_AddNotCarry_Impl, BigInteger, std::bool_constant<sizeof...(I1) == sizeof...(I2)>> {};
// 判断是否为0
template<class Y> struct BI_IsZero;
template struct BI_IsZero> : std::bool_constant<((I == 0) && ...)> {};
// 自动进位
template<class A> struct BI_Carry;
template<class A, class B> struct BI_Add : BI_Carry<typename BI_AddNotCarry::type> {};
template<class Mod, class Div, class ShouldCalc = typename BI_IsZero::type> struct BI_Carry_Impl;
template<class Mod, class Div> struct BI_Carry_Impl : Mod {};
template<class Mod, class Div> struct BI_Carry_Impl
: BI_Addtypename BI_SHL::type > {};
template struct BI_Carry>
: BI_Remove_Zeros<typename BI_Carry_Impl, BigInteger<(I / Base)...>>::type> {};
// 乘以X并自动进位
template<class A, int X> struct BI_MulX;
templateint X> struct BI_MulX , X>
: BI_Carry> {};
// 计算阶乘
template<int X> struct BI_Fact : BI_MulX<typename BI_Fact-1>::type, X> {};
template<> struct BI_Fact<0> : BigInteger<1> {};
template
std::ostream &operator<<(std::ostream &out, BigInteger) {
return ((out << std::setfill('0') << I << std::setw(lgBase)), ...);
}
int main()
{
std::cout << typename BI_Fact<20>::type() << std::endl;
}
版权申明:内容来源网络,版权归原创者所有。除非无法确认,都会标明作者及出处,如有侵权,烦请告知,我们会立即删除并致歉!
评论