当前位置:网站首页>CSDN daily practice - find the closest element and output the subscript
CSDN daily practice - find the closest element and output the subscript
2022-06-11 00:00:00 【Blue stars】
Examples are as follows : An array {915,941,960,976,992,1015,1034,1050,1073,1089,1115,1131,1150,1166,1182,1208,1227}; The target value is assumed to be 1000, The closest element is 992, Subscript to be 4
#include <stdio.h>
int main()
{
int min = (1 << 31) - 1;// initialization int Type of , It becomes 32 Maximum number of bits 2^32-1
int idx = 0;// Use subscript idx Express , And initialize to 1
int arr[] = { 915, 941, 960, 976, 992, 1015, 1034, 1050, 1073, 1089, 1115, 1131, 1150, 1166, 1182, 1208, 1227 };
int n = 1000;
for (int i = 0; i < sizeof(arr) / sizeof(int); i++)//sizeof(arr) / sizeof(int) Indicates that in the array int Number of type elements
{
int diff = arr[i] - n;// Index in turn , Calculate the element and n The difference between the
if (diff < 0)
diff = -diff;// The difference is less than 0 Go to absolute value
if (diff < min)
{
min = diff;// Let the number with the smallest absolute value be the new min
idx = i;// Record subscripts ( from 0 Indexes )
}
}
printf(" The closest thing is %d The subscript is %d", arr[idx], idx);
return 0;
}stay c In language 1<<n Express :
Such as 1<<3 Express the 1 Binary system 0000 0001 Shift three bits to the left :0000 1000
That is to say :
n=1 1*2
n=2 1*2*2
n=3 1*2*2*2
int Type account 4 Bytes ,32 position , The maximum is 2^31-1
边栏推荐
- 【漫天烟花】绚烂烟花点亮夜空也太美了叭、某程序员携带烟花秀给大家拜年啦~
- 【Pygame合集】回忆杀-“童年游戏”,看看你中几枪?(附五款源码自取)
- Usage of C tryparse
- flutter 如何去掉listview顶部空白的问题
- IGBT and third generation semiconductor SiC double pulse test scheme
- Insert sort
- LabVIEW错误“内存已满 - 应用程序停止在节点”
- LabVIEW使用MathScript Node或MATLAB脚本时出现错误1046
- 【Pygame小游戏】别找了,休闲游戏专题来了丨泡泡龙小程序——休闲游戏研发推荐
- Difference between oscilloscope and spectrum analyzer
猜你喜欢

【Pygame合集】滴~穿越童年游戏指南 请查收:这里面有你玩过的游戏嘛?(附五款源码自取)

Error report of curl import postman

LabVIEW获取Clamp函数找到的所有点的信息
![[fireworks in the sky] it's beautiful to light up the night sky with gorgeous fireworks. A programmer brought a fireworks show to pay New Year's greetings to everyone~](/img/3b/2fcd5ff2ea08c4c63428899babd522.png)
[fireworks in the sky] it's beautiful to light up the night sky with gorgeous fireworks. A programmer brought a fireworks show to pay New Year's greetings to everyone~

Opencv实战之图像的基本操作:这效果出来惊艳了众人(附代码解析)

LabVIEW get IMAQ get last event coordinates

Apple CMS collection station source code - building tutorial - attached source code - new source code - development documents

IGBT and third generation semiconductor SiC double pulse test scheme

VS 番茄助手添加头注释 以及使用方式

【Pygame小游戏】来了来了它来了——这款五子棋小游戏超A的,分享给你的小伙伴儿一起pk吧~
随机推荐
LabVIEW pictures look bright or dark after being cast from 16 bits to 8 bits
【Pygame小游戏】不怕你走不过系列:极致AI走迷宫,学习完带你打开新世界大门~(附游戏源码)
Apple CMS collection station source code - building tutorial - attached source code - new source code - development documents
选择排序
VS 番茄助手添加头注释 以及使用方式
Top ten information security principles
SystemVerilog(十)-用户自定义类型
Merge sort
苹果CMS采集站源码-搭建教程-附带源码-全新源码-开发文档
【Pygame小游戏】激荡大脑思维,一起来玩转奇思妙想“24点”叭~(超赞滴)
Vs tomato assistant add header comments and usage
LabVIEW获取IMAQ Get Last Event坐标
The serial port in the visa test panel under LabVIEW or max does not work
【Pygame小游戏】这款经典的炸弹人超能游戏上线,你爱了嘛?(附源码)
csdn每日一练——找出最接近元素并输出下标
LabVIEW获取Clamp函数找到的所有点的信息
curl导入postman报错小记
LabVIEW programming specification
Flowable process deployment
HyperLeger Fabric安装