网站首页 > 基础教程 正文
单项选择
判断题
3
编程实战
1. 因数分解
题目分析
依次将 2、3、4、5、6 ... 除尽。
参考程序
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
LL n;
int main() {
cin >> n;
bool is_first = true;
for(int i = 2; i <= n / i; i++){
if(n % i == 0){
int x = 0;
while(n % i == 0){
x++;
n /= i;
}
if(!is_first)
cout << " * ";
if(x == 1) cout << i;
else cout << i << "^" << x;
is_first = false;
}
}
if(n > 1){
if(!is_first)
cout << " * ";
cout << n << endl;
}
return 0;
}
2. 巧夺大奖
题目分析
贪心算法:先按照奖励从大到小排序,再按照结束时间从后先前枚举,如果有空闲时间,就累加该奖励。
参考程序
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
struct Node{
int t; // 截止时间
int r; // 收益
bool operator < (const Node& x) const{
return r > x.r;
}
}node[N];
int n;
int T[N];
int res = 0;
int main() {
cin >> n;
for(int i = 1; i <= n; i++) cin >> node[i].t;
for(int i = 1; i <= n; i++) cin >> node[i].r;
sort(node + 1, node + n + 1);
for(int i = 1; i <= n; i++){
for(int j = node[i].t; j >= 1; j--){
if(T[j] == 0){
T[j] = node[i].r;
res += node[i].r;
break;
}
}
}
cout << res << endl;
return 0;
}
- 上一篇: 「C语言重点难点精讲」关键字精讲1
- 下一篇: 网络编程——C++实现socket通信(TCP)
猜你喜欢
- 2024-11-11 C++经典算法问题:背包问题(迭代+递归算法)!含源码示例
- 2024-11-11 C++进阶教程:C#嵌套循环 c++嵌套循环break
- 2024-11-11 C++经典算法 穷举法 穷举算法的优点
- 2024-11-11 C++数据结构-- 递归 排序 c++使用递归函数实现全排列
- 2024-11-11 如何使用c++发送window消息通知 c++怎么发给别人
- 2024-11-11 C++ replace函数-C++字符串替换函数
- 2024-11-11 C++学习:循环练习题(一) c++循环结构例题解析
- 2024-11-11 C/C++最细循环解析 c++循环结构23道题
- 2024-11-11 网络编程——C++实现socket通信(TCP)
- 2024-11-11 「C语言重点难点精讲」关键字精讲1
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)