Sum is very important!

本文探讨了如何使用智能算法解决求解两个数之和为给定值的组合数量问题,通过实例展示了输入数据处理和输出结果生成的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

聪明的你们一定会求两个数的和啦,是时候考验考验大家啦。

Input

输入包含多组测试数据,对于每组测试用例:

输入一个正整数n( 0 < n < 105 )。

接着输入n个整数Ai( |Ai| < 108 )。

接着输入一个整数k( |Ai| < 108 )。

问:从n个整数中取出两个数a, b(a != b),使得a + b = k的组合种数是多少?

Output

输出组合种数。每组数据占一行。

Sample Input

4
1 2 2 3
5
5
1 2 3 4 5
6

Sample Output

1
2


#include <stdio.h>  
#include <string.h> 
#include <map>
#include<algorithm> 
#define K 11111111 
using namespace std;
 
  
int num[K],k;
int BinSearch(int Array[],int SizeOfArray,int key){  
    int low=0,high=SizeOfArray-1;  
    int mid;  
    while(low<=high){  
        mid=(low+high)/2;  
        if(key==Array[mid]){
            return mid;  
        }  
        if(key<Array[mid])  
            high=mid-1;  
        if(key>Array[mid])  
            low=mid+1;  
    }  
    return -1;  
}  
  
int main(){
    int n,i,b,sum;  
    while(scanf("%d",&n)!=EOF){
        sum=0;
        for(i=0;i<n;i++){
            scanf("%d",&num[i]);
        }
        sort(num,num+n);
        scanf("%d",&k);
        map<int,int> m;
        for(i=0;i<n;i++){
            if(m[num[i]]){
                continue;
            }
            b=k-num[i];
            if((BinSearch(num,n,b)>-1)&&b!=num[i]){
                sum++;
                m[b]=1;
                m[num[i]]=1;
            }   
        }
        printf("%d\n",sum);
    }
    return 0;
}


第一部分: # -*- coding: gbk -*- import os import glob import cv2, math import numpy as np from PIL import Image import random import numpy as np import h5py import os from PIL import Image import scipy.io IMG_EXTENSIONS = [ '.jpg', '.JPG', '.jpeg', '.JPEG', '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', ] #图片转矩阵 def pil_to_np(img_PIL): '''Converts image in PIL format to np.array. From W x H x C [0...255] to C x W x H [0..1] ''' ar = np.array(img_PIL) if len(ar.shape) == 3: ar = ar.transpose(2, 0, 1) else: ar = ar[None, ...] return ar.astype(np.float32) / 255. #矩阵转图片 def np_to_pil(img_np): '''Converts image in np.array format to PIL image. From C x W x H [0..1] to W x H x C [0...255] ''' ar = np.clip(img_np * 255, 0, 255).astype(np.uint8) if img_np.shape[0] == 1: ar = ar[0] else: ar = ar.transpose(1, 2, 0) return Image.fromarray(ar) # 判断文件夹中是否有以上类型图片,没有则返回0 def is_image_file(filename): #如果不都为空、0、false,则any()返回true return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) #返回文件夹内文件绝对路径组成的列表 def make_dataset(dir): images = [] assert os.path.isdir(dir), '%s is not a valid directory' % dir # os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) 通过在目录树中游走输出在目录中的文件名,top返回三项(root,dirs,files),分别代表: # 当前正在遍历的这个文件夹的本身的地址; list类型,内容是该文件夹中所有的目录的名字(不包括子目录); list类型,内容是该文件夹中所有的文件(不包括子目录) for root, _, fnames in sorted(os.walk(dir)): for fname in fnames: #print(fname) #拼接出图片的地址,并加入到images列表 path = os.path.join(root, fname) images.append(path) return images def hazy_simu(img_name,depth_or_trans_name,save_dir,pert_perlin = 0,airlight=0.76,is_imdepth=1,visual_range = [0.05, 0.1, 0.2, 0.5, 1]): """ This is the function for haze simulation with the parameters given by the paper: HAZERD: an outdoor scene dataset and benchmark for single image dehazing IEEE Internation Conference on Image Processing, Sep 2017 The paper and additional information on the project are available at: https://labsites.rochester.edu/gsharma/research/computer-vision/hazerd/ If you use this code, please cite our paper. IMPORTANT NOTE: The code uses the convention that pixel locations with a depth value of 0 correspond to objects that are very far and for the simulation of haze these are placed a distance of 2 times the visual range. Authors: Yanfu Zhang: yzh185@ur.rochester.edu Li Ding: l.ding@rochester.edu Gaurav Sharma: gaurav.sharma@rochester.edu Last update: May 2017 python version update : Aug 2021 Authors : Haoying Sun : 1913434222@qq.com parse inputs and set default values Set default parameter values. Some of these are used only if they are not passed in :param img_name: the directory and name of a haze-free RGB image, the name should be in the format of ..._RGB.jpg :param depth_name: the corresponding directory and name of the depth map, in .mat file, the name should be in the format of ..._depth.mat :param save_dir: the directory to save the simulated images :param pert_perlin: 1 for adding perlin noise, default 0 :param airlight: 3*1 matrix in the range [0,1] :param visual_range: a vector of any size :return: image name of hazy image """
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值