#include<iostream> #include<vector> #include<ctime> #include<algorithm> #include"Queen.h" using namespace std; std::vector<int> CreatRandomPrice(int min, int max, int num) { std::vector<int> price; if (max - min + 1 < num) { return price; } srand(time(0)); for (auto i{ 0 }; i < num; i++) { while (true) { auto temp{ rand() % (max + 1 - min) + min }; auto iter{ find(price.begin(),price.end(),temp) }; if (price.end() == iter) { price.push_back(temp); break; } } } return price; } int main() { Queue<double,24> queue; auto rand = CreatRandomPrice(100, 999, 168); double sum = 0, average; for (int i = 0; i < 168; i++) { if (!queue.isFull()) { queue.insert(rand[i]); sum += rand[i]; average = sum / (i + 1); } else { sum = sum - queue.getFront() + rand[i]; average = sum / 24; queue.remove(); queue.insert(rand[i]); } cout << "当前价格: " << rand[i] << ", 最近24小时商品平均价格: " << average << endl; } return 0; }
时间: 2024-04-27 19:22:38 浏览: 21
这是一段 C++ 代码,使用了 STL 中的 vector 和 algorithm 库。它定义了一个 CreatRandomPrice 函数,用于生成一组在 min 和 max 之间的不重复的 num 个随机数。然后,它定义了一个 Queue 类,用于实现一个循环队列,队列的最大大小为 24。接着,在 main 函数中,它使用 CreatRandomPrice 函数生成 168 个随机数,并将它们插入到 Queue 对象中。当队列还未满时,直接插入并计算平均值;当队列已满时,先移除队首元素,再插入新元素并计算平均值,并输出当前价格以及最近 24 小时的平均价格。
相关问题
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctime> #include<iostream> #include<algorithm> #include<map> #include<stack> #include<queue> #include<vector> #include<set> #include<string> #define dd double #define ll long long dd PI = acos(-1); using namespace std; const ll MAXN = 1e5 + 5; const ll INF = 1e9 + 5; ll n; struct node { ll x, y; }s[5000]; ll dp[MAXN] = { 0 }; int main() { //ios::sync_with_stdio(false); ll n, m; cin >> n >> m; for (ll i = 1; i <= n; i++) { cin >> s[i].x >> s[i].y; } for (ll i = 1; i <= n; i++) { for (ll j = m; j >= s[i].x; j--) { dp[j] = max(dp[j], dp[j - s[i].x] + s[i].y); } } cout << dp[m] << endl; }
这是一段动态规划求解背包问题的代码,用来求解一个容量为m的背包能够装下的最大价值。具体实现过程为:
1. 定义一个结构体node,用来存储物品的重量和价值。
2. 读入物品数量n和背包容量m,然后读入每个物品的重量和价值。
3. 使用二重循环,遍历每一个物品,然后从大到小枚举背包容量j,更新dp[j]的值,表示容量为j的背包能够装下的最大价值。
4. 最后输出dp[m]的值,即容量为m的背包能够装下的最大价值。
值得注意的是,这段代码中还定义了一些常量和宏定义,如MAXN、INF等,以及一些头文件和命名空间的使用。
#include <Windows.h> #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <cstdlib> #include <ctime> #include <conio.h> #include <winsock.h> #include <ws2bth.h> #include <bluetoothapis.h> #pragma comment(lib, "ws2_32.lib") using namespace std; const string CONFIG_FILE = "config.ini"; const int MAX_BLUETOOTH_DEVICES = 10; int main() { // 读取配置文件 ifstream config(CONFIG_FILE); if (!config.is_open()) { cout << "无法打开配置文件!" << endl; return -1; } string line; int search_count = 0; while (getline(config, line)) { if (line.find("search_count") != string::npos) { search_count = stoi(line.substr(line.find("=") + 1)); break; } } config.close(); // 初始化蓝牙 WSAData wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != NO_ERROR) { cout << "WSAStartup 失败!" << endl; return -1; } // 枚举蓝牙设备 BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS) }; searchParams.fReturnAuthenticated = TRUE; searchParams.fReturnRemembered = TRUE; searchParams.fReturnUnknown = TRUE; searchParams.hRadio = NULL; BLUETOOTH_DEVICE_INFO deviceInfo = { sizeof(BLUETOOTH_DEVICE_INFO) }; HBLUETOOTH_DEVICE_FIND deviceFindHandle; vector<BLUETOOTH_DEVICE_INFO> devices; deviceFindHandle = BluetoothFindFirstDevice(&searchParams, &deviceInfo); if (deviceFindHandle != NULL) { do { devices.push_back(deviceInfo); } while (BluetoothFindNextDevice(deviceFindHandle, &deviceInfo)); BluetoothFindDeviceClose(deviceFindHandle); } // 检查蓝牙设备数量 if (devices.size() >= MAX_BLUETOOTH_DEVICES) { ofstream file("CheckBT.log"); file << "PASS"; // 向文档中写入"PASS"字符串 } else { ofstream file("CheckBT.log"); file << "FAIL"; // 向文档中写入"FAIL"字符串 } // 清理蓝牙 WSACleanup(); return 0; }改进这段代码,把每个蓝牙设备名称输出到文档中
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <winsock.h>
#include <ws2bth.h>
#include <bluetoothapis.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
const string CONFIG_FILE = "config.ini";
const int MAX_BLUETOOTH_DEVICES = 10;
int main() {
// 读取配置文件
ifstream config(CONFIG_FILE);
if (!config.is_open()) {
cout << "无法打开配置文件!" << endl;
return -1;
}
string line;
int search_count = 0;
while (getline(config, line)) {
if (line.find("search_count") != string::npos) {
search_count = stoi(line.substr(line.find("=") + 1));
break;
}
}
config.close();
// 初始化蓝牙
WSAData wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
cout << "WSAStartup 失败!" << endl;
return -1;
}
// 枚举蓝牙设备
BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS) };
searchParams.fReturnAuthenticated = TRUE;
searchParams.fReturnRemembered = TRUE;
searchParams.fReturnUnknown = TRUE;
searchParams.hRadio = NULL;
BLUETOOTH_DEVICE_INFO deviceInfo = { sizeof(BLUETOOTH_DEVICE_INFO) };
HBLUETOOTH_DEVICE_FIND deviceFindHandle;
vector<BLUETOOTH_DEVICE_INFO> devices;
deviceFindHandle = BluetoothFindFirstDevice(&searchParams, &deviceInfo);
if (deviceFindHandle != NULL) {
do {
devices.push_back(deviceInfo);
} while (BluetoothFindNextDevice(deviceFindHandle, &deviceInfo));
BluetoothFindDeviceClose(deviceFindHandle);
}
// 输出蓝牙设备名称到文档
ofstream file("CheckBT.log");
if (devices.size() >= MAX_BLUETOOTH_DEVICES) {
file << "PASS" << endl;
} else {
file << "FAIL" << endl;
}
for (auto device : devices) {
file << device.szName << endl;
}
// 清理蓝牙
WSACleanup();
return 0;
}
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)