E - Find The Multiple (dfs)

本文介绍了一个算法挑战,目标是找到一个正整数n的非零倍数m,该m在十进制中仅由0和1组成。通过深度优先搜索(DFS)策略,递归地构造满足条件的m,确保其长度不超过19位。代码实现使用C++,展示了如何通过递归检查每个可能的数,直到找到符合条件的m。

E - Find The Multiple (dfs)

POJ1426 E - Find The Multiple 原题连接

题目大意

给定一个正整数n,请编写一个程序来寻找n的一个非零的倍数m,
这个m应当在十进制表示时每一位上只包含0或者1。
你可以假定n不大于200且m不多于100位。 
***提示:本题采用Special Judge,你无需输出所有符合条件的m,
你只需要输出任一符合条件的m即可。***

Input

输入包含多组数据,每组数据仅一行,只包含一个正整数n (1 <= n <= 200).

Output

对于输入的每组n,都输出任一符合条件的m。即使有多个符合条件的m,你也只需要输出一个即可。

Sample Input

2
6
19
0

Sample Output

  10
  100100100100100100
  111111111111111111

思路:

1:只含有0,1的数可以这样求得,利用1,给它乘以10,给它乘以10后加1.每次对一它进行这两种操作
2:DFS,用递归来做,终止递归的判别条件是,当这个数的长度超过19时,强制跳出当前的这条路,换一条路走。
**:1.long long 的范围是 -9223372036854775807 ~ 9223372036854775808
19位,所以超过19就可以跳出。
**:Special Judge,只要符合要求的任意一项即可,所以不同的计算机计算结果可能不同,不必太在意。

AC的代码:

AC的OJ连接

#include<cstdio>
using namespace std;

int n;
int flag;
void dfs(long long cur,int k)
{
	if(k==19 || flag)
	{
		return ;
	}
	if(cur%n==0)
	{
		flag=1;
		printf("%lld\n",cur);
		return ;
	}
	dfs(cur*10,k+1);
	dfs(cur*10+1,k+1);
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
		{
			continue;
		 }
		flag=0;
		dfs(1,0);
	}
	return 0;
}
pip install pandas openpyxl xlsxwriter tqdm import pandas as pd import re from tqdm import tqdm import os def filter_single_company_news(input_path, output_path, company_list): # === 1. 前置检查 === if not os.path.exists(input_path): raise FileNotFoundError(f"文件 {input_path} 不存在") # === 2. 加载公司列表 === with open(company_list, 'r', encoding='utf-8') as f: companies = [line.strip() for line in f if line.strip()] # === 3. 构建正则表达式 === escaped_companies = [re.escape(company) for company in companies] boundary_pattern = r'(?<![一-鿿a-zA-Z0-9])(?:{})(?![一-鿿a-zA-Z0-9])'.format('|'.join(escaped_companies)) cross_regex = re.compile( r'({}).*?({})'.format(boundary_pattern, boundary_pattern), flags=re.IGNORECASE ) # === 4. 分块处理(关键修复) === try: # 分块读取(添加 chunksize 参数) chunks = pd.read_excel(input_path, engine='openpyxl', chunksize=10000) except Exception as e: raise ValueError(f"Excel文件读取失败: {str(e)}") filtered_dfs = [] for chunk in tqdm(chunks, desc="处理进度"): # === 列名检查 === if 'NewsContent' not in chunk.columns: raise KeyError("Excel文件中必须包含 'NewsContent' 列") # === 类型转换 === chunk['NewsContent'] = chunk['NewsContent'].astype(str) # === 检测函数 === def check_multiple(text): matches = cross_regex.findall(text) if not matches: return False unique_companies = {m[0].lower() for m in matches} | {m[1].lower() for m in matches} return len(unique_companies) >= 2 # === 生成过滤掩码 === mask = chunk['NewsContent'].apply(lambda x: not check_multiple(x)) filtered_dfs.append(chunk[mask]) # === 5. 保存结果 === if filtered_dfs: final_df = pd.concat(filtered_dfs, ignore_index=True) final_df.to_excel(output_path, index=False, engine='xlsxwriter') print(f"处理完成!保留数据量:{len(final_df)}条") else: print("警告:未处理任何数据!") # === 使用示例 === if __name__ == "__main__": filter_single_company_news( input_path='sample.xlsx', output_path='filtered_news.xlsx', company_list='companies.txt' ) 以上为完整代码,运行到此处报错。 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_14844/3787377892.py in filter_single_company_news(input_path, output_path, company_list) 20 # 分块读取(添加 chunksize 参数) ---> 21 chunks = pd.read_excel(input_path, engine='openpyxl', chunksize=5000) 22 except Exception as e: D:\py\anaconda\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs) 310 ) --> 311 return func(*args, **kwargs) 312 TypeError: read_excel() got an unexpected keyword argument 'chunksize' During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_14844/3187432990.py in <module> 1 # === 使用示例 === 2 if __name__ == "__main__": ----> 3 filter_single_company_news( 4 input_path='sample.xlsx', 5 output_path='filtered_news.xlsx', ~\AppData\Local\Temp/ipykernel_14844/3787377892.py in filter_single_company_news(input_path, output_path, company_list) 21 chunks = pd.read_excel(input_path, engine='openpyxl', chunksize=5000) 22 except Exception as e: ---> 23 raise ValueError(f"Excel文件读取失败: {str(e)}") 24 25 filtered_dfs = [] ValueError: Excel文件读取失败: read_excel() got an unexpected keyword argument 'chunksize'
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值