博客
关于我
19年 湘潭邀请赛
阅读量:303 次
发布时间:2019-03-03

本文共 1409 字,大约阅读时间需要 4 分钟。

代码优化后的内容

模拟n叉树结构的代码优化

#include 
#include
#include
#include
#include
using namespace std;int main() { int k, m, n, N; long long p; while (scanf("%d%d%d%lld", &k, &m, &n, &p)) { vector
a(k + 1, 0); for (int i = 1; i <= k; ++i) { scanf("%lld", &a[i]); } sort(a.begin() + 1, a.end()); if (n == 1) { N = m; } else { N = (pow(n, m) - 1) / (n - 1); } long long ans = 0; for (int i = 1; i < N; ++i) { int parent = i / n; if (parent == 0) { parent = i; } a[i] = (a[parent] % p + a[i] % p) % p; ans = (ans + a[i]) % p; } cout << ans << endl; } return 0;}

字符串处理问题的代码优化

#include 
#include
#include
#include
#include
using namespace std;int main() { int n, a[10]; string s; while (cin >> n >> s) { memset(a, 0, sizeof(a)); for (size_t i = 0; i < n; ++i) { if (s[i] == 'x') { a[1]++; } else if (s[i] == 't' && a[1]) { a[2]++; a[1]--; } else if (s[i] == 'C' && a[2]) { a[3]++; a[2]--; } else if (s[i] == 'p' && a[3]) { a[4]++; a[3]--; } else if (s[i] == 'c' && a[4]) { a[5]++; a[4]--; } } cout << a[5] << endl; } return 0;}

转载地址:http://hwfm.baihongyu.com/

你可能感兴趣的文章
Objective-C实现Lempel-Ziv压缩算法(附完整源码)
查看>>
Objective-C实现logistic regression逻辑回归算法(附完整源码)
查看>>
Objective-C实现LongestIncreasingSubsequence最长递增子序列算法(附完整源码)
查看>>
Objective-C实现Lower-Upper Decomposition上下分解算法(附完整源码)
查看>>
Objective-C实现lowest common ancestor最低共同祖先算法(附完整源码)
查看>>
Objective-C实现LRU 缓存算法(附完整源码)
查看>>
Objective-C实现LRU缓存(附完整源码)
查看>>
Objective-C实现lstm prediction预测算法(附完整源码)
查看>>
Objective-C实现lucas数列算法(附完整源码)
查看>>
Objective-C实现Luhn (Mod 10)Algorithm算法(附完整源码)
查看>>
Objective-C实现LZW编码(附完整源码)
查看>>
Objective-C实现MAC桌面暗水印(附完整源码)
查看>>
Objective-C实现markov chain马尔可夫链算法(附完整源码)
查看>>
Objective-C实现MATLAB中Filter函数功能(附完整源码)
查看>>
Objective-C实现matrix exponentiation矩阵求幂算法(附完整源码)
查看>>
Objective-C实现MatrixMultiplication矩阵乘法算法 (附完整源码)
查看>>
Objective-C实现max non adjacent sum最大非相邻和算法(附完整源码)
查看>>
Objective-C实现max subarray sum最大子数组和算法(附完整源码)
查看>>
Objective-C实现MaximumSubarray最大子阵列(Brute Force蛮力解决方案)算法(附完整源码)
查看>>
Objective-C实现MaximumSubarray最大子阵列(动态规划解决方案)算法(附完整源码)
查看>>