2021-07-19 AcWing 3773. 兔子跳
输入样例:
4 2 4 1 3 3 12 3 4 5 1 5 5 2 10 15 4
输出样例:
2 3 1 2
思路:①当 ai = x, min = 1;
②当存在 ai > x(且不存在 ai = x), min = 2;
③当所有ai均小于x时,取max(ai/x的上取整),即找到此时最大的ai
一开始是这样写的,超时了
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int t;
int m,n,x;
cin >> t;
while(t--)
{
bool ret=false;
int res=0;
cin >> n >> x;
for (int i = 0; i < n; i ++ ){
cin >> m;
if(m==x){
res=1;
ret=true;
}else if(m>x){
res=2;
}else{
if(!res) res=x/m+1;
else res=min(res,x/m+1);
}
if(ret) break;
}
cout << res <<endl;
}
return 0;
}
改进后
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int t;
int m,n,x;
cin >> t;
while(t--)
{
bool ret=false;
int res=0;
cin >> n >> x;
for (int i = 0; i < n; i ++ ){
cin >> m;
if(m==x){
ret=true;
}
res=max(res,m);
}
if(ret){
printf("1\n");
}else if(res>x){
printf("2\n");
}else{
printf("%d\n",(x+res-1)/res);//上取整
}
}
return 0;
}
本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15799163.html
分类:
刷题(数据结构和算法)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY