实在顶不住就别硬撑着了(我实在是憋不住了)
大家好,我是吴师兄,周末快乐,分享个有趣的编程问题。
这就是一个求阶乘的问题,大家刚刚开始学编程的时候应该都写过这样的程序。
一个求阶乘的问题,还能玩出什么样的花儿来?
我在回答区看到了一个非常有趣的回答,把各种版本的代码都举例了出来,一个比一个还离谱,后面的代码,甚至让怀疑是我学过的代码吗?
分享给大家图个乐,下面是回答正文,答主:小白白。
数学家版本:(简单利索,深藏功与名)
#include <iostream>
#include <cmath>
int main()
{
std::cout << std::tgamma(20 1) << std::endl;
}
(语言学家,你懂得,恨不得把所有语法特性都派上用场)
#include <iostream>
#include <utility>
template<std::size_t...I> constexpr auto foo(std::index_sequence<I...>) { return ((I 1) * ...); }
int main()
{
std::cout << foo(std::make_index_sequence<20>()) << std::endl;
}
(void main() 有没有嗅到浓厚的历史气息?)
#include <stdio.h>
void main(void) {
int i;
long long j;
for(i = 1, j = 1;i <= 20; j *= i );
printf("%lld", j);
}
(可以说是非常敏捷了)
#include <stdio.h>
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 <iostream>
#include <string>
#include <memory>
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<IDataProvider> m_dp;
std::shared_ptr<ICalculator> m_c;
std::shared_ptr<ISerializer> m_s;
std::shared_ptr<IPrinter> m_p;
public:
Foo(std::shared_ptr<IDataProvider> dp, std::shared_ptr<ICalculator> c, std::shared_ptr<ISerializer> s, std::shared_ptr<IPrinter> 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<MyDataProvider>(), std::make_shared<MultiplyAccumulateCalculator>(), std::make_shared<DefaultSerializer>(), std::make_shared<StreamPrinter>(std::cout));
foo.run();
}
(一看就是精通底层技术的大佬,把CPU拿捏得死死的)
#include <iostream>
#include <xmmintrin.h>
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 <iostream>
#include <numeric>
#include <vector>
#include <functional>
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.hpp>
// 由于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 <iostream>
#include <iomanip>
#include <type_traits>
using BaseType_t = long long;
constexpr BaseType_t lgBase = 9; // 注意10000*10000刚刚好小于int的取值范围
constexpr BaseType_t Base = 1000000000; // 注意10000*10000刚刚好小于int的取值范围
// 大整数的表示
template<BaseType_t...I> struct Biginteger {
using type = BigInteger;
};
// 连接
template<class T1, class T2> struct BI_Cat;
template<BaseType_t...I1, BaseType_t...I2> struct BI_Cat <BigInteger<I1...>, BigInteger<I2...>> : BigInteger<I1..., I2...> {};
// 左移一个单元(即*Base)
template<class T> struct BI_SHL;
template<BaseType_t...I> struct BI_SHL<BigInteger<I...>> : BigInteger<I..., 0> {};
// 去除开头的0
template<class T> struct BI_Remove_Zeros : T {};
template<BaseType_t...I> struct BI_Remove_Zeros<BigInteger<0, I...>> : BI_Remove_Zeros<BigInteger<I...>> {};
// 填充0到N个单元
template<int X, class IS> struct BI_Fill_Impl;
template<int X, class T, T...I> struct BI_Fill_Impl<X, std::integer_sequence<T, I...>> : BigInteger<(I, X)...> {};
template<int Size> struct BI_Fill_Zeros : BI_Fill_Impl<0, std::make_index_sequence<Size>> {};
template<class T, int N> struct BI_Resize;
template<BaseType_t...I, int N> struct BI_Resize<BigInteger<I...>, N> : BI_Cat<typename BI_Fill_Zeros<N - sizeof...(I)>::type, BigInteger<I...>> {};
// 返回较大的数值
template<int A, int B> struct int_min : std::integral_constant<int, (A<B?B:A)> {};
// 非进位加法:先把两个数的位数改成一样的然后依次相加
template<class A, class B, class ShouldResize> struct BI_AddNotCarry_Impl;
template<BaseType_t...I1, BaseType_t...I2> struct BI_AddNotCarry_Impl <BigInteger<I1...>, BigInteger<I2...>, std::true_type> : BigInteger<(I1 I2)...> {};
template<BaseType_t...I1, BaseType_t...I2> struct BI_AddNotCarry_Impl <BigInteger<I1...>, BigInteger<I2...>, std::false_type>
: BI_AddNotCarry_Impl<
typename BI_Resize<BigInteger<I1...>, int_min<sizeof...(I1), sizeof...(I2)>::value>::type,
typename BI_Resize<BigInteger<I2...>, int_min<sizeof...(I1), sizeof...(I2)>::value>::type,
std::true_type
>{};
template<class A, class B> struct BI_AddNotCarry;
template<BaseType_t...I1, BaseType_t...I2> struct BI_AddNotCarry <BigInteger<I1...>, BigInteger<I2...>>
: BI_AddNotCarry_Impl<BigInteger<I1...>, BigInteger<I2...>, std::bool_constant<sizeof...(I1) == sizeof...(I2)>> {};
// 判断是否为0
template<class Y> struct BI_IsZero;
template<BaseType_t...I> struct BI_IsZero<BigInteger<I...>> : std::bool_constant<((I == 0) && ...)> {};
// 自动进位
template<class A> struct BI_Carry;
template<class A, class B> struct BI_Add : BI_Carry<typename BI_AddNotCarry<A, B>::type> {};
template<class Mod, class Div, class ShouldCalc = typename BI_IsZero<Div>::type> struct BI_Carry_Impl;
template<class Mod, class Div> struct BI_Carry_Impl<Mod, Div, std::true_type> : Mod {};
template<class Mod, class Div> struct BI_Carry_Impl<Mod, Div, std::false_type>
: BI_Add<Mod, typename BI_SHL<Div>::type > {};
template<BaseType_t...I> struct BI_Carry<BigInteger<I...>>
: BI_Remove_Zeros<typename BI_Carry_Impl<BigInteger<(I % Base)...>, BigInteger<(I / Base)...>>::type> {};
// 乘以X并自动进位
template<class A, int X> struct BI_MulX;
template<BaseType_t...I1, int X> struct BI_MulX <BigInteger<I1...>, X>
: BI_Carry<BigInteger<(I1 * X)...>> {};
// 计算阶乘
template<int X> struct BI_Fact : BI_MulX<typename BI_Fact<X-1>::type, X> {};
template<> struct BI_Fact<0> : BigInteger<1> {};
template<BaseType_t...I>
std::ostream &operator<<(std::ostream &out, BigInteger<I...>) {
return ((out << std::setfill('0') << I << std::setw(lgBase)), ...);
}
int main()
{
std::cout << typename BI_Fact<20>::type() << std::endl;
}
原回答:
https://www.zhihu.com/question/365763395/answer/971009059
不得不服,这位老哥真是人才,把 C 玩出这么多花样,不愧是语言学家。当然,现实工作中不会有人这样写代码的,所以大家图个乐就好,不用较真。
你们在第几个版本倒下了?
我反正看到面向对象专家版本就已经忍不住了
···
END从零开始学算法,无悔做一名程序员!
原文链接:https://mp.weixin.qq.com/s/0qjOX8kxEspjBwjTv-aBpA
,
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com