-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm_.cpp
128 lines (113 loc) · 2.18 KB
/
algorithm_.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "algorithm_.h"
#include <algorithm>
#include <iostream>
/*利用浮点数的存储规则,1 8 23单精度 1 11 52双精度的符号位 指数阶码 小数位表示法求得绝对值*/
float myfabs(float x)
{
*(int*)&x = 0x7fffffff;
return x;
} /*三个非规格数
NAN 阶码全1 小数位不全为0
无穷大 阶码全1 小数位全0
无穷小 解码全0 小数位不全为0*/
/*找出一组数中,和为给定值的数对*/
/*先排序,后从前后往中间找
*/
void printPair(int* arr, size_t N, int sum)
{
int count = 0;
int tempSum = 0;
std::sort(arr, arr + N);
int * left = arr;
int * right = arr + N - 1;
int rightTemp = 0;
int leftTemp = 0;
while (left < right)
{
tempSum = (*left) + (*right);
if (tempSum == sum)
{
std::cout << ++count << ":" << *left << " " << *right << "\n";
rightTemp = *right;
leftTemp = *left;
while ((left < right) && (*right == rightTemp))
{
--right;
}
while ((left < right) && (*left == leftTemp)) /*跳过相同的元素*/
{
++left;
}
}
else if (tempSum < sum)
{
++left;
}
else
{
--right;
}
}
std::cout << count;
}
/*最后一个元素被挤出去*/
void insert_(int *p, int index, int size, int value)
{
int i = size - 1;
while (i >= index)
{
*(p + i) = *(p + i - 1);
--i;
}
p[index] = value;
}
void del_(int *p, int index, int size)
{
while (index < size - 1)
{
*(p + index) = *(p + index + 1);
++index;
}
p[index] = 0;
}
/*异或0 保持 异或1 取反 异或自己 为0*/
int myabs(int x)
{
int signBit = (x >> 31) & 0x1; /*得到符号位*/
int allBit = 0 - signBit; /*得到全0 或者 全1的特殊数*/
int ret = x ^ allBit; /*全部取反或者不变*/
ret += signBit; /*取反加1 或者 加0*/
return ret;
}
/*借助另外一个栈和中间变量来对栈进行排序*/
void stackSort(std::stack<int>& s)
{
std::stack<int> t;
int temp;
t.push(s.top());
s.pop();
while (!s.empty()) /*s顶部的小 则弹出保存在中间变量里*/
{
if (s.top() < t.top())
{
temp = s.top();
s.pop();
while (!t.empty())
{
s.push(t.top());
t.pop();
}
t.push(temp);
}
else
{
t.push(s.top());
s.pop();
}
}
while (!t.empty())
{
s.push(t.top());
t.pop();
}
}