<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          過于離譜,我實現(xiàn)憋不住了!

          共 330字,需瀏覽 1分鐘

           ·

          2022-01-20 07:15

          大家好,我是小林

          在知乎,看到了一個問題:

          這就是一個求階乘的問題,大家剛剛開始學(xué)編程的時候應(yīng)該都寫過這樣的程序。

          一個求階乘的問題,還能玩出什么樣的花兒來?

          我在回答區(qū)看到了一個非常有趣的回答,把各種版本的代碼都舉例了出來,一個比一個還離譜,后面的代碼,甚至讓懷疑是我學(xué)過的代碼嗎?

          分享給大家圖個樂,下面是回答正文,答主:小白白。

          數(shù)學(xué)家版本:

          (簡單利索,深藏功與名)

          #include?
          #include?
          int?main()
          {
          ????std::cout?<std::tgamma(20?+?1)?<std::endl;
          }

          語言學(xué)家版本:

          (語言學(xué)家,你懂得,恨不得把所有語法特性都派上用場)

          #include?
          #include?

          template<std::size_t...I>?constexpr?auto?foo(std::index_sequence)?{?return?((I+1)?*?...);?}

          int?main()?
          {
          ????std::cout?<std::make_index_sequence<20>())?<std::endl;
          }

          歷史學(xué)家版本:

          (void main() 有沒有嗅到濃厚的歷史氣息?)

          #include?
          void?main(void)?{
          ????int?i;
          ????long?long?j;
          ????for(i?=?1,?j?=?1;i?<=?20;?j?*=?i++);
          ????????printf("%lld",?j);?
          }

          敏捷開發(fā)上線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);
          }

          面向?qū)ο髮<野姹荆?/span>

          (好家伙,一個簡單的問題,愣是祭出了接口、繼承、虛函數(shù)、虛繼承、智能指針等一大堆東西出來,這很面向?qū)ο螅?/p>

          #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?<};

          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();
          }

          提前優(yōu)化的并行版本:

          (一看就是精通底層技術(shù)的大佬,把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?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?<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;
          }

          “宏孩兒”元編程版:

          (當(dāng)年看各種C++框架中,排山倒海一樣的宏定義,簡直令人發(fā)指)

          #include?

          //?由于boost.preprocessor僅提供255以下的整數(shù)運(yùn)算
          //?所以使用sequence來?(十位個位)(千位百位)(十萬位萬位)?的方式來表示大整數(shù)。

          //?不進(jìn)位加法:(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)

          //?進(jìn)位加法:(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的取值范圍

          //?大整數(shù)的表示
          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>?{};

          //?返回較大的數(shù)值
          template<int?A,?int?B>?struct?int_min?:?std::integral_constant<int,?(A?{};

          //?非進(jìn)位加法:先把兩個數(shù)的位數(shù)改成一樣的然后依次相加
          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)?&&?...)>?{};

          //?自動進(jìn)位
          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并自動進(jìn)位
          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')?<std::setw(lgBase)),?...);
          }

          int?main()
          {
          ????std::cout?<typename?BI_Fact<20>::type()?<std::endl;
          }

          原回答:

          https://www.zhihu.com/question/365763395/answer/971009059

          不得不服,這位老哥真是人才,把 C++ 玩出這么多花樣,不愧是語言學(xué)家。當(dāng)然,現(xiàn)實工作中不會有人這樣寫代碼的,所以大家圖個樂就好,不用較真。

          你們在第幾個版本倒下了?

          我反正看到面向?qū)ο髮<野姹揪鸵呀?jīng)忍不住了···


          圖解系列文章:
          小林的2021年終總結(jié)
          圖解文章匯總
          計算機(jī)基礎(chǔ)學(xué)習(xí)路線
          小林的圖解系統(tǒng),大曝光!
          不鴿了,小林的「圖解網(wǎng)絡(luò) 3.0 」發(fā)布!
          瀏覽 26
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大奶一区二区三区 | 激情成人开心网 | 亚洲精品一区二区三小电影 | 国产一区二区三区四 | 国产精品69毛片高清亚洲 |