Binary Watch_太阁竞赛A

本文探讨了一道经典的编程题——二进制手表问题。题目要求根据给定的点亮二进制位数,输出所有可能的时间显示。文章通过具体的代码实现,展示了如何计算并输出这些时间。
题目1 : Binary Watch
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Consider a binary watch with 5 binary digits to display hours (00 - 23) and 6 binary digits to display minutes (00 - 59).

For example 11:26 is displayed as 01011:011010.  

Given a number x, output all times in human-readable format "hh:mm" when exactly x digits are 1.  


输入
An integer x. (0 ≤ x ≤ 9)  

输出
All times in increasing order.  

样例输入
1
样例输出
00:01  
00:02  
00:04  
00:08  
00:16  
00:32  
01:00  
02:00  
04:00  
08:00  

16:00


【我的程序】

#include <iostream>
using namespace std;

int compt(int x)
{
    int ans=0;
    while (x>0){ if (x%2==1) ans++; x/=2; }
    return ans;
}

int main()
{
    int n,a[60];
    cin>>n;
    for (int i=0;i<=59;i++) a[i]=compt(i);
    for (int i=0;i<=23;i++)
        for (int j=0;j<=59;j++)
            if (a[i]+a[j]==n) cout<<i/10 <<i%10 <<':' <<j/10 <<j%10 <<endl;
    return 0;
}


import pandas as pd from sklearn.model_selection import train_test_split # 加载数据 off_train = pd.read_csv('ccf_offline_stage1_train.csv') off_test = pd.read_csv('ccf_offline_stage1_test.csv') # 简单的数据清洗 def preprocess(df): # 处理缺失值 df['Distance'].fillna(-1, inplace=True) # 转换日期字段 df['Date_received'] = pd.to_datetime(df['Date_received'], format='%Y%m%d') df['Date'] = pd.to_datetime(df['Date'], format='%Y%m%d') # 提取时间差 df['days_diff'] = (df['Date'] - df['Date_received']).dt.days return df off_train = preprocess(off_train) off_test = preprocess(off_test) # 构建训练集和标签 train_data = off_train.drop(['Coupon_id', 'Date'], axis=1) labels = off_train['Date'].notnull().astype(int) # 核销为1,未核销为0from sklearn.ensemble import GradientBoostingClassifier from sklearn.metrics import roc_auc_score # 划分训练集和验证集 X_train, X_val, y_train, y_val = train_test_split(train_data, labels, test_size=0.2, random_state=42) # 训练 GBDT 模型 gbdt_model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=5, random_state=42) gbdt_model.fit(X_train, y_train) # 验证模型 y_pred = gbdt_model.predict_proba(X_val)[:, 1] print("GBDT AUC Score:", roc_auc_score(y_val, y_pred))import xgboost as xgb from sklearn.metrics import roc_auc_score # 转换为 DMatrix 格式 dtrain = xgb.DMatrix(X_train, label=y_train) dval = xgb.DMatrix(X_val, label=y_val) # 设置参数 params = { 'booster': 'gbtree', 'objective': 'binary:logistic', 'eval_metric': 'auc', 'max_depth': 6, 'eta': 0.1, 'subsample': 0.8, 'colsample_bytree': 0.7, 'silent': 1 } # 训练模型 watchlist = [(dtrain, 'train'), (dval, 'val')] xgb_model = xgb.train(params, dtrain, num_boost_round=200, evals=watchlist) # 验证模型 y_pred_xgb = xgb_model.predict(dval) print("XGBoost AUC Score:", roc_auc_score(y_val, y_pred_xgb))
最新发布
07-03
### C++中 `#ifndef`, `#define` 的作用及用法 在C/C++中,`#ifndef`, `#define` 是预处理指令,用于控制头文件的包含行为,防止头文件被多次包含。这种机制通常被称为 **头文件保护** 或 **多重包含防护**。 #### 1. 防止头文件重复包含 当一个头文件被多个源文件包含时,可能会导致编译器重复定义符号的问题。例如,如果头文件中定义了某个结构体或函数声明,并且该头文件被多次包含,编译器会报错,提示重复定义[^1]。 ```c #ifndef BINARYTREE_H #define BINARYTREE_H typedef int ElementType; typedef struct BI_TREE_T { ElementType value; struct BI_TREE_T *lChild; struct BI_TREE_T *rChild; } BI_TREE; extern void binary_tree_main_test(void); #endif ``` - `#ifndef BINARYTREE_H`: 检查是否已经定义了宏 `BINARYTREE_H`。如果没有定义,则继续执行后续代码。 - `#define BINARYTREE_H`: 定义宏 `BINARYTREE_H`,以标记该头文件已被包含。 - `#endif`: 结束条件编译块。 通过这种方式,确保头文件的内容仅被编译一次,避免重复定义问题。 #### 2. 宏名的选择 宏名通常是头文件名的大写形式,带有 `_H` 后缀(如 `BINARYTREE_H`)。这是一种常见的命名约定,有助于快速识别头文件对应的宏。 #### 3. 示例:头文件保护的作用 假设有以下两个文件: **binary_tree.h** ```c #ifndef BINARYTREE_H #define BINARYTREE_H typedef int ElementType; typedef struct BI_TREE_T { ElementType value; struct BI_TREE_T *lChild; struct BI_TREE_T *rChild; } BI_TREE; extern void binary_tree_main_test(void); #endif ``` **main.c** ```c #include "binary_tree.h" #include "binary_tree.h" // 再次包含头文件 int main() { binary_tree_main_test(); return 0; } ``` 即使 `binary_tree.h` 被包含两次,由于头文件保护的存在,其内容只会被编译一次[^1]。 #### 4. 单调队列中的头文件保护 在引用[2]中,虽然没有直接展示头文件保护,但在实际开发中,单调队列相关的代码也可以封装到头文件中,并使用类似的保护机制。例如: ```c #ifndef MONOTONIC_QUEUE_H #define MONOTONIC_QUEUE_H void Max(int n, int k, int a[]); void Min(int n, int k, int a[]); #endif ``` 这可以确保单调队列的功能声明不会被重复包含[^2]。 ### 总结 `#ifndef` 和 `#define` 的组合是C/C++中常用的头文件保护机制,能够有效避免头文件被多次包含导致的编译错误。通过合理选择宏名并遵循命名规范,可以使代码更加清晰和易于维护。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值