content_views"
class="markdown_views prism-tomorrow-night">
回顾
- A+B III
- 问题 H: 三角数
- 问题 G: 3个数
- 等式 数组下标查询c;降低时间复杂度
- 1405 问题 E: 世界杯
- xtu 数码串
- xtu oj 神经网络
- xtu oj 1167 逆序数(大数据)
- xtu oj 原根
- xtu oj 不定方程的正整数解
- xtu oj 最多的可变换字符串
- xtu oj String I
- xtu oj 字母序列
- xtu oj 分段
- xtu oj 完全平方数II
- xtu oj 连接字符串
- xtu oj 2021
- xtu oj 数字
- xtu oj 删除
代码
<code class="prism language-c">class="token macro property">class="token directive-hash">#class="token directive keyword">includeclass="token string"><stdio.h>
class="token macro property">class="token directive-hash">#class="token directive keyword">define class="token macro-name">N class="token expression">class="token number">10010
class="token keyword">int aclass="token punctuation">[Nclass="token punctuation">]class="token punctuation">;
class="token keyword">void class="token function">sortclass="token punctuation">(class="token keyword">int qclass="token punctuation">[class="token punctuation">]class="token punctuation">,class="token keyword">int lclass="token punctuation">,class="token keyword">int rclass="token punctuation">)class="token punctuation">{class="token comment">//快速排序模板class="token keyword">ifclass="token punctuation">(lclass="token operator">>=rclass="token punctuation">)class="token punctuation">{class="token keyword">returnclass="token punctuation">;class="token punctuation">}class="token keyword">int iclass="token operator">=lclass="token operator">-class="token number">1class="token punctuation">,jclass="token operator">=rclass="token operator">+class="token number">1class="token punctuation">,xclass="token operator">=qclass="token punctuation">[class="token punctuation">(lclass="token operator">+rclass="token punctuation">)class="token operator">/class="token number">2class="token punctuation">]class="token punctuation">;class="token keyword">whileclass="token punctuation">(iclass="token operator"><jclass="token punctuation">)class="token punctuation">{class="token keyword">doclass="token punctuation">{iclass="token operator">++class="token punctuation">;class="token punctuation">}class="token keyword">whileclass="token punctuation">(qclass="token punctuation">[iclass="token punctuation">]class="token operator"><xclass="token punctuation">)class="token punctuation">;class="token keyword">doclass="token punctuation">{jclass="token operator">--class="token punctuation">;class="token punctuation">}class="token keyword">whileclass="token punctuation">(qclass="token punctuation">[jclass="token punctuation">]class="token operator">>xclass="token punctuation">)class="token punctuation">;class="token keyword">ifclass="token punctuation">(iclass="token operator"><jclass="token punctuation">)class="token punctuation">{class="token keyword">int tempclass="token operator">=qclass="token punctuation">[iclass="token punctuation">]class="token punctuation">;qclass="token punctuation">[iclass="token punctuation">]class="token operator">=qclass="token punctuation">[jclass="token punctuation">]class="token punctuation">;qclass="token punctuation">[jclass="token punctuation">]class="token operator">=tempclass="token punctuation">;class="token punctuation">}class="token punctuation">}class="token function">sortclass="token punctuation">(qclass="token punctuation">,lclass="token punctuation">,jclass="token punctuation">)class="token punctuation">;class="token function">sortclass="token punctuation">(qclass="token punctuation">,jclass="token operator">+class="token number">1class="token punctuation">,rclass="token punctuation">)class="token punctuation">;
class="token punctuation">}
class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">)class="token punctuation">{class="token keyword">int tclass="token punctuation">;class="token function">scanfclass="token punctuation">(class="token string">"%d"class="token punctuation">,class="token operator">&tclass="token punctuation">)class="token punctuation">;class="token keyword">whileclass="token punctuation">(tclass="token operator">--class="token punctuation">)class="token punctuation">{class="token keyword">int nclass="token punctuation">;class="token function">scanfclass="token punctuation">(class="token string">"%d"class="token punctuation">,class="token operator">&nclass="token punctuation">)class="token punctuation">;class="token comment">//所有朋友的个数class="token keyword">forclass="token punctuation">(class="token keyword">int iclass="token operator">=class="token number">0class="token punctuation">;iclass="token operator"><nclass="token punctuation">;iclass="token operator">++class="token punctuation">)class="token punctuation">{class="token function">scanfclass="token punctuation">(class="token string">"%d"class="token punctuation">,class="token operator">&aclass="token punctuation">[iclass="token punctuation">]class="token punctuation">)class="token punctuation">;class="token punctuation">}class="token function">sortclass="token punctuation">(aclass="token punctuation">,class="token number">0class="token punctuation">,nclass="token operator">-class="token number">1class="token punctuation">)class="token punctuation">;class="token keyword">int sumclass="token operator">=class="token number">1class="token punctuation">;class="token comment">//现在有多少人来聚会了class="token keyword">int ansclass="token operator">=class="token number">0class="token punctuation">;class="token comment">//会来多少朋友class="token keyword">forclass="token punctuation">(class="token keyword">int iclass="token operator">=class="token number">0class="token punctuation">;iclass="token operator"><nclass="token punctuation">;iclass="token operator">++class="token punctuation">)class="token punctuation">{class="token keyword">ifclass="token punctuation">(aclass="token punctuation">[iclass="token punctuation">]class="token operator"><=sumclass="token punctuation">)class="token punctuation">{class="token comment">//至少来 sum 个朋友c;i 这个朋友就会来ansclass="token operator">++class="token punctuation">;class="token comment">//多一个朋友来sumclass="token operator">++class="token punctuation">;class="token comment">//聚会的总人数多一个class="token punctuation">}class="token punctuation">}class="token function">printfclass="token punctuation">(class="token string">"%d\n"class="token punctuation">,ansclass="token punctuation">)class="token punctuation">;class="token punctuation">}class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>
思路
<code class="prism language-cpp">class="token macro property">class="token directive-hash">#class="token directive keyword">includeclass="token string"><stdio.h>
class="token macro property">class="token directive-hash">#class="token directive keyword">define class="token macro-name">N class="token expression">class="token number">10010
class="token keyword">int aclass="token punctuation">[Nclass="token punctuation">]class="token punctuation">;
class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">)class="token punctuation">{class="token keyword">int tclass="token punctuation">;class="token function">scanfclass="token punctuation">(class="token string">"%d"class="token punctuation">,class="token operator">&tclass="token punctuation">)class="token punctuation">;class="token keyword">whileclass="token punctuation">(tclass="token operator">--class="token punctuation">)class="token punctuation">{class="token keyword">int nclass="token punctuation">;class="token function">scanfclass="token punctuation">(class="token string">"%d"class="token punctuation">,class="token operator">&nclass="token punctuation">)class="token punctuation">;class="token comment">//所有朋友的个数class="token keyword">forclass="token punctuation">(class="token keyword">int iclass="token operator">=class="token number">0class="token punctuation">;iclass="token operator"><nclass="token punctuation">;iclass="token operator">++class="token punctuation">)class="token punctuation">{class="token function">scanfclass="token punctuation">(class="token string">"%d"class="token punctuation">,class="token operator">&aclass="token punctuation">[iclass="token punctuation">]class="token punctuation">)class="token punctuation">;class="token punctuation">}class="token keyword">int sumclass="token operator">=class="token number">1class="token punctuation">;class="token comment">//现在有多少人来聚会了class="token keyword">int ansclass="token operator">=class="token number">0class="token punctuation">;class="token comment">//会来多少朋友class="token keyword">forclass="token punctuation">(class="token keyword">int iclass="token operator">=class="token number">0class="token punctuation">;iclass="token operator"><nclass="token punctuation">;iclass="token operator">++class="token punctuation">)class="token punctuation">{class="token keyword">ifclass="token punctuation">(aclass="token punctuation">[iclass="token punctuation">]class="token operator"><=sumclass="token punctuation">)class="token punctuation">{class="token comment">//至少来 sum 个朋友c;i 这个朋友就会来ansclass="token operator">++class="token punctuation">;class="token comment">//多一个朋友来sumclass="token operator">++class="token punctuation">;class="token comment">//聚会的总人数多一个class="token punctuation">}class="token punctuation">}class="token function">printfclass="token punctuation">(class="token string">"%d\n"class="token punctuation">,ansclass="token punctuation">)class="token punctuation">;class="token punctuation">}class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>
为啥直接 <code>WAcode> 了c;样例也能过c;然后也不会出现需要用 <code>long longcode> 的情况。嗷嗷c;不是按照顺序排的。哈哈哈。贪心地做c;就是让那种要求低的朋友排在前面c;这样来聚会的人就会尽可能多。
排序一下就过了c;幸福。
但是我这里写了一个快速排序的模板c;有没有啥简单一点的办法来排序呢?要是找到了更好的办法会更新在评论区。