返回相同宽度数字型字符串

 /**返回相同闊度數字型字串. 例:
       function1(“000000”) => “000001”
       function1(”0023")   => “0024”
       function1(“0009”)   => “0010”
       function1(“000099”) => “000100”
       function1(“9”) => “0” //號碼循環再用
       程式接口:
       public static String function1(String num);
    */
     public static String function1(String num){
       int length = num.length();
       int number = 0;
       //得到字符串中的数值
       for(int i=0;i<length;i++){
           if(!(num.charAt(i)=='0')){
               number = Integer.parseInt(num.substring(i));
               break;
           }
       }
       number++;
       String returnStr = number+"";
       //得到原来宽度的字符串
       if(returnStr.length()==length){
          
       }else if(returnStr.length()>length){
           returnStr = returnStr.substring(returnStr.length()-length);
       }else{
           while(returnStr.length()<length){
               returnStr = "0" + returnStr;
           }
       }
       return returnStr;
   }

 

                                              //---------------------------纵横软件邮件所发笔试题1

转载于:https://www.cnblogs.com/chaohi/archive/2010/03/09/2330355.html

import pandas as pd from collections import defaultdict def parse_width(width_str): """解析宽度数据,返回可旋转的宽度列表""" if '*' in str(width_str): return sorted([int(x) for x in str(width_str).split('*')], reverse=True) return [int(width_str)] def calculate_utilization(widths, plate_width): """计算板材利用率""" return sum(widths) / plate_width def group_components(df): """按厚度和长度分组""" grouped = defaultdict(list) for _, row in df.iterrows(): key = (row['厚度'], row['长度']) grouped[key].append({ '编号': row['构件编号'], '宽度': parse_width(row['宽度']), '数量': row['数量'], '原始宽度': row['宽度'] }) return grouped def optimize_layout(components, plate_options=[1500, 1800]): """贪心算法进行排版优化""" results = [] for comp in components.copy(): while comp['数量'] > 0: best_plate = None max_utilization = 0 # 尝试所有板材规格 for plate_width in plate_options: current_width = 0 used = [] # 尝试组合当前构件 if any(w <= (plate_width - current_width) for w in comp['宽度']): selected_w = max([w for w in comp['宽度'] if w <= (plate_width - current_width)]) current_width += selected_w used.append((comp['编号'], selected_w)) comp['数量'] -= 1 # 尝试组合其他构件 for other in components: if other['数量'] <= 0 or other['编号'] == comp['编号']: continue if any(w <= (plate_width - current_width) for w in other['宽度']): selected_w = max([w for w in other['宽度'] if w <= (plate_width - current_width)]) current_width += selected_w used.append((other['编号'], selected_w)) other['数量'] -= 1 # 计算利用率 utilization = current_width / plate_width if utilization > max_utilization: max_utilization = utilization best_plate = (plate_width, used.copy()) if best_plate: results.append({ '板宽': best_plate[0], '组合': best_plate[1], '利用率': max_utilization }) return results def process_file(input_path, output_path): # 读取数据 df = pd.read_excel(input_path) # 数据预处理 grouped = group_components(df) final_results = [] # 处理每个分组 for (thickness, length), components in grouped.items(): plate_length = length + 20 # 优化排版 optimized = optimize_layout(components) # 生成结果 for plate in optimized: cutting_desc = " + ".join([f"{id}({w}mm)" for id, w in plate['组合']]) final_results.append({ '厚度': thickness, '板宽': plate['板宽'], '板长': plate_length, '板材数量': 1, # 实际需要根据数量计算 '切割方式': cutting_desc }) # 输出结果 result_df = pd.DataFrame(final_results) result_df.to_excel(output_path, index=False) # 使用示例 process_file('input.xlsx', 'output.xlsx')
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值