1048. Find Coins 解析

本文介绍了一种通过排序和二分查找的方法来判断是否能够使用两枚硬币恰好支付特定金额的算法。该算法首先对硬币面额进行排序,然后针对每一种硬币尝试寻找另一个能够与之组合成目标金额的硬币。

找两个硬币看能不能付钱,两个硬币必须正好等于要付的价格。

那就直接从小到大排序,然后从第一个元素开始,找价格减去当前硬币的价值,看能不能找到。

注意二分查找的最后位置。这个后面再找个时间研究下。里面其实有很多细节需要注意的。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstring>

#define MAX 100010

using namespace std;

int n,m;
int coin[MAX];

int BinSearch(int num){
	int l = 0 ,r = n - 1;
	int pos = -1;
	while(l < r){
		int mid = (l + r + 1) /2 ;
		if(coin[mid] < num)
			l = mid + 1;
		else if(coin[mid] > num)
			r = mid - 1 ;
		else{
			pos = mid;
			break;
		}
	}
	return pos;
}

int main(){

	scanf("%d%d",&n,&m);
	for(int i = 0 ; i < n ; i++){
		scanf("%d",&coin[i]);
	}
	sort(&coin[0],&coin[n]);

	bool tag = false;
	for(int i = 0; i < n ;i++){
		int pos = BinSearch(m-coin[i]);

		if(pos>0 && pos != i){
			printf("%d %d\n",coin[i],coin[pos]);
			tag = true;
			break;
		}	
	}

	if(!tag)
		printf("No Solution\n");


	return 0;
}


from bs4 import BeautifulSoup import requests import lxml import openpyxl import os def get_html(url,headers,data): try: response = requests.get(url, headers=headers, params=data) if response.status_code == 200: return response.text else: print('请求失败') except Exception as e: print(e) def save_data(html): soup=BeautifulSoup(html,'lxml') node=soup.find_all('table') for sub_node in node: title=sub_node.find_all('h1')[2].get('title') likes=sub_node.find_all('span',{'class':'video-like-info video-toolbar-item-text'})[0].string coins=sub_node.find_all('span',{'class':'video-coin-info video-toolbar-item-text'})[0].string favorites=sub_node.find_all('span',{'class':'video-fav-info video-toolbar-item-text'})[0].string shares=sub_node.find_all('span',{'class':'video-share-info video-toolbar-item-text'})[0].string description=sub_node.find_all('span',{'class':'desc-info-text'})[0].string print(title, likes, coins, favorites, shares, description) sheet.append([title, likes, coins, favorites, shares, description]) if __name__ == "__main__": path='d:\\video' if not os.path.exists(path): os.mkdir(path) os.chdir(path) wb = openpyxl.Workbook() sheet = wb.active sheet.append(['标题', '点赞人数', '投币人数', '收藏人数', '转发人数','视频介绍']) url ='https://search.bilibili.com/all?vt=68706038&keyword=python&page=''' headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36' } data={ 'page':0 } for page in range(0,11): data['page'] = page html = get_html(url,headers,data) save_data(html) wb.save('video.xlsx') 请将我的代码完善,我想要实现前十页视频爬取的相关内容
最新发布
06-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值