当前位置:网站首页>刷题-洛谷-P1161 开灯
刷题-洛谷-P1161 开灯
2022-07-25 13:32:00 【宋向上_UP】
P1161 开灯-C语言
1、题目

2、解题过程
(1)第一次 超时 #define LIGHT 20000//灯的最大值
结果:
代码:
//洛谷 P1161 开灯
#include <stdio.h>
#define NUM 5000//n<=5000
#define LIGHT 20000//灯的最大值
int main() {
int n;//进行n次操作
int light[LIGHT] = {
0 };//初始值为关0
double a[NUM] = {
0.0 };
int t[NUM] = {
1 };
int k;//需要按开关的编号
int i,j;
scanf_s("%d", &n);
for (i = 0; i < n; i++) {
scanf_s("%lf %d", &a[i], &t[i]);
for (j = 1; j <= t[i]; j++) {
//灯的编号从1开始
k = (int)(a[i]*j);//向下求整
light[k] = (light[k] == 0) ? 1 : 0;//切换灯的状态
}
}
i = 1;//从第一盏灯开始遍历
while (light[i] == 0) {
//灯的状态为关
i++;
}
printf("%d",i);//灯的状态为开的编号
return 0;
}
(2)第二次
结果:
代码:
//洛谷 P1161 开灯
#include <stdio.h>
#define NUM 5000//n<=5000
#define LIGHT 20000//灯的最大值
int main() {
int n;//进行n次操作
int light[LIGHT] = {
0 };//初始值为关0
double a[NUM] = {
0.0 };
int t[NUM] = {
1 };
int k;//需要按开关的编号
int i,j;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf %d", &a[i], &t[i]);
for (j = 1; j <= t[i]; j++) {
//灯的编号从1开始
k = (int)(a[i]*j);//向下求整
light[k] = (light[k] == 0) ? 1 : 0;//切换灯的状态
}
}
i = 1;//从第一盏灯开始遍历
while (light[i] == 0) {
//灯的状态为关
i++;
}
printf("%d",i);//灯的状态为开的编号
return 0;
}
边栏推荐
- 好友让我看这段代码
- ThreadLocal&Fork/Join
- R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题
- Connotation and application of industrial Internet
- 并发编程之并发工具集
- Leetcode 113. 路径总和 II
- How to solve the problem of taking up too much space when recording and editing videos?
- Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
- 2022全球开发者中,你的收入排多少?
- Canvas judgment content is empty
猜你喜欢

Numpy简介和特点(一)

二叉树基本知识

QGIS loading online map: Gaode, Tiandi map, etc
[email protected](using password:YES)"/>全网最简单解决方式1045-Access denied for user [email protected](using password:YES)

Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1

uniapp处理后台传输图片

领域驱动模型设计与微服务架构落地-模型设计

0710RHCSA

Discussion on principle and application technology of MLIR

Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
随机推荐
hcip第八天实验
Leetcode 113. path sum II
0716RHCSA
电脑里一辈子都不想删的神仙软件
Hcip day 8 notes
The whole process of 6w+ word recording experiment | explore the economical data storage strategy of alluxio
ThreadLocal&Fork/Join
Generate SQL script file by initializing the latest warehousing time of vehicle attributes
QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
stable_ Baselines quick start
0719RHCSA
VIM basic operation summary
Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board
Hcip day 9 notes
hcip第六天笔记
Discussion on principle and application technology of MLIR
【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
Excel import and export source code analysis
基于百问网IMX6ULL_PRO开发板驱动AP3216实验
Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li