The partial sum problem

算法:搜索

描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K.
输入There are multiple test cases.
Each test case contains three lines.The first line is an integer N(1≤N≤20),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-10^8≤A[i]≤10^8).The third line contains an integer K(-10^8≤K≤10^8).输出If Tom can choose some integers from the array and their them is K,printf ”Of course,I can!”; other printf ”Sorry,I can’t!”.样例输入4
1 2 4 7
13
4
1 2 4 7
15
样例输出

Of course,I can!
Sorry,I can't!

代码:

#include <iostream>
  #include <string>
  #include <cstring>
  #include <algorithm>
  #include <iomanip>
  using namespace std;
  int n,m,a[25],flag;
  int dfs(int i,int sum)
  {
    if(i==n)
        return sum==m;
    if(dfs(i+1,sum))
        return 1;
    if(dfs(i+1,sum+a[i]))
        return 1;
    return 0;

  }
   int main()
   {
   	  int i,j,k;
   	  while(cin>>n)
   	  {
   	  	  for(i=0;i<n;i++)
   	  	  cin>>a[i];
   	  	  cin>>m;
   	  	  if(dfs(0,0)) cout<<"Of course,I can!"<<endl;
   	  	  else cout<<"Sorry,I can't!"<<endl;
	  }
	  return 0;
   }



转载于:https://www.cnblogs.com/wangyumin/p/5323418.html

优化这段代码,窗口1、3、5、9、11、17、33,输入3200个点,输出也要3200个点,边缘输出原值:#include "SmoothProfileOnXAxisMean.h" #define MAX_WINDOW_SIZE 33 #define HALF_MAX_WINDOW 16 #define II 1 // 每周期处理一个数据 // 使用18位定点数(16位整数 + 8位小数)替代浮点数 typedef ap_fixed<24, 12> fixed_t; typedef ap_fixed<32, 16> fixed_cache; void SmoothProfileOnXAxisMeanOptimize(hls::stream<float>& points_in_z, hls::stream<float>& smoothed_z, ap_uint<6> mean_win_size, float invalid_z ) { #pragma HLS PIPELINE II=1 #pragma HLS INTERFACE ap_ctrl_none port=return #pragma HLS INTERFACE axis port=points_in_z #pragma HLS INTERFACE axis port=smoothed_z #pragma HLS INTERFACE ap_none port=mean_win_size #pragma HLS INTERFACE ap_none port=invalid_z // 循环缓冲区 - 使用定点数提高时序性能 static fixed_t buffer[MAX_WINDOW_SIZE]; #pragma HLS ARRAY_PARTITION variable=buffer complete dim=1 static ap_uint<1> win[MAX_WINDOW_SIZE]; #pragma HLS ARRAY_PARTITION variable=win complete dim=1 static ap_uint<1> invalid_flag0; static ap_uint<1> invalid_flag1; static ap_uint<1> invalid_flag2; // 读取输入并转换为定点数 float in_val_float = points_in_z.read(); fixed_t in_val = in_val_float; fixed_t invalid_z_fixed = invalid_z; // std::cout << "out1_data:" << in_val << " " << invalid_z_fixed << " " << invalid_z <<std::endl; for (int i = MAX_WINDOW_SIZE-1; i > 0; i--){ #pragma HLS UNROLL buffer[i] = buffer[i - 1]; win[i] = win[i-1]; } buffer[0] = (in_val==invalid_z_fixed)?(fixed_t)0:in_val; win[0] = (in_val==invalid_z_fixed)?0:1; static fixed_cache sum_buffer0[11]; static fixed_cache sum_win0[11]; for (int i = 0; i < 11; i=i+1) { #pragma HLS UNROLL sum_buffer0[i] = buffer[i * 3] + buffer[i * 3+1] + buffer[i * 3+2]; sum_win0[i] = win[i * 3] + win[i * 3+1] + win[i * 3+2]; } invalid_flag0 = win[16]; static fixed_cache sum_buffer1[3]; sum_buffer1[0] = sum_buffer0[0] + sum_buffer0[1] + sum_buffer0[2] + sum_buffer0[3]; sum_buffer1[1] = sum_buffer0[4] + sum_buffer0[5] + sum_buffer0[6] + sum_buffer0[7]; sum_buffer1[2] = sum_buffer0[8] + sum_buffer0[9] + sum_buffer0[10]; static fixed_cache sum_win1[3]; sum_win1[0] = sum_win0[0] + sum_win0[1] + sum_win0[2] + sum_win0[3]; sum_win1[1] = sum_win0[4] + sum_win0[5] + sum_win0[6] + sum_win0[7]; sum_win1[2] = sum_win0[8] + sum_win0[9] + sum_win0[10]; invalid_flag1 = invalid_flag0; static fixed_cache sum_buffer2; sum_buffer2 = sum_buffer1[0] + sum_buffer1[1] + sum_buffer1[2]; static fixed_cache sum_win2; sum_win2 = sum_win1[0] + sum_win1[1] + sum_win1[2]; invalid_flag2 = invalid_flag1; // std::cout << "out2_data:" << in_val << " " << sum_buffer2 << " " << sum_win2 <<std::endl; // 除法优化 - 使用移位和乘法避免硬件除法器 fixed_t result; if (invalid_flag2 == 0) { result = invalid_z_fixed; } else { // 使用倒数乘法代替除法 ap_ufixed<24, 8> reciprocal = 1.0 / sum_win2.to_int(); result = fixed_t(sum_buffer2 * reciprocal); } // std::cout << "out2_data:" << in_val << " " << result << " " << sum_buffer2 << " " << sum_win2 << " " << std::endl; smoothed_z.write((float)result); }
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值