爬虫问题

在这里插入图片描述

#include <iostream>
爬虫问题 ,虫子每次爬u米,休息一分钟下落d米,井深n米 ,爬总高度为h
using namespace std;
int _time (int n,int u,int d);
int main() {
	int n1,u1,d1;          //输入输出 
	int n2,u2,d2;
	int n3,u3,d3;
	cin>>n1>>u1>>d1;
	cin>>n2>>u2>>d2;
	cin>>n3>>u3>>d3;
	if(n1!=0) {
		cout<<_time(n1,u1,d1)<<endl;
	}
	if(n2!=0) {
		cout<<_time(n2,u2,d2)<<endl;
	}
	if(n3!=0) {
		cout<<_time(n3,u3,d3)<<endl;
	}
	return 0;
}
int _time (int n,int u,int d) {
	int h =0;
	int time=0;
	int win =0;
	while(win!=1) {        //win用来控制循环 
		h=h+u;        //向上爬 
		time++;
		if(h>=n) {     //判断是否爬到井口 
			win=1;
		} else {
			h=h-d;   //向下滑 
			time++;
		}
	}
	return time;
}

爬虫就是要来用代码模拟虫子向上向下爬的过程,就是两个变量的加减法,就是多了一个哨兵变量来判断是否爬到井口

要多把文字翻译成代码就会了,再多会一些算法,多见一些代码,多做码农

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值