Skip to content

Commit eb482b9

Browse files
committed
add
1 parent d97eb21 commit eb482b9

File tree

21 files changed

+384
-0
lines changed

21 files changed

+384
-0
lines changed

19.10.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int Rand5(){
5+
int x = ~(1<<31); // max int
6+
while(x > 5)
7+
x = Rand7();
8+
return x;
9+
}
10+
11+
12+
13+
int main(){
14+
return 0;
15+
}

19.11

33.1 KB
Binary file not shown.

19.11.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
using namespace std;
4+
5+
void PrintPairSum(int a[], int n, int sum){
6+
if(a==NULL || n<2) return;
7+
8+
sort(a, a+n);
9+
int low = 0, high = n-1;
10+
while(low < high){
11+
if(a[low]+a[high] > sum)
12+
--high;
13+
else if(a[low]+a[high] < sum)
14+
++low;
15+
else{
16+
cout<<a[low]<<" "<<a[high]<<endl;
17+
++low; --high;
18+
}
19+
20+
}
21+
}
22+
23+
int main(){
24+
int n = 6, sum = 6;
25+
int a[] = {
26+
1, 2, 3, 4, 5, 6
27+
};
28+
PrintPairSum(a, n, sum);
29+
return 0;
30+
}

19.2

17.9 KB
Binary file not shown.

19.2.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
enum Check{
5+
ROW, COLUMN, DIAGONAL, REDIAGONAL
6+
};
7+
8+
int CheckRowColumn(int board[], int n, Check check){
9+
int type = 0;
10+
for(int i=0; i<n; ++i){
11+
bool found = true;
12+
for(int j=0; j<n; ++j){
13+
int k = 0;
14+
if(check == ROW)
15+
k = i * n + j;
16+
else
17+
k = i + j * n;
18+
if(j == 0){
19+
type = board[k];
20+
}
21+
else if(board[k] != type){
22+
found = false;
23+
break; // 有一个不满足,检查下一行
24+
}
25+
}
26+
if(found) return type;
27+
}
28+
return 0;
29+
}
30+
int CheckDiagonal(int board[], int n, Check check){
31+
int type = 0;
32+
bool found = true;
33+
// 主对角
34+
for(int i=0; i<n; ++i){
35+
int k = 0;
36+
if(check == DIAGONAL)
37+
k = i + i * n;
38+
else
39+
k = i + (n-1-i) * n;
40+
if(i == 0){
41+
type = board[k];
42+
}
43+
else if(board[k] != type){
44+
found = false;
45+
break;
46+
}
47+
}
48+
if(found) return type;
49+
50+
return 0;
51+
}
52+
int HasWon(int board[], int n){
53+
int type = 0;
54+
type = CheckRowColumn(board, n, ROW);
55+
if(type != 0) return type;
56+
type = CheckRowColumn(board, n, COLUMN);
57+
if(type != 0) return type;
58+
type = CheckDiagonal(board, n, DIAGONAL);
59+
if(type != 0) return type;
60+
type = CheckDiagonal(board, n, REDIAGONAL);
61+
if(type != 0) return type;
62+
63+
return 0;
64+
}
65+
int main(){
66+
int n = 3; // 3*3
67+
int board[] = {
68+
2, 2, 1,
69+
2, 1, 1,
70+
1, 2, 0,
71+
};
72+
int type = HasWon(board, n);
73+
if(type != 0)
74+
cout<<type<<" won!"<<endl;
75+
else
76+
cout<<"nobody won!"<<endl;
77+
return 0;
78+
}

19.3

17.1 KB
Binary file not shown.

19.3.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int NumZeros(int n){
5+
if(n < 0) return -1;
6+
int num = 0;
7+
while((n /= 5) > 0){
8+
num += n;
9+
}
10+
return num;
11+
}
12+
int NumZeros1(int n){
13+
if(n < 0) return -1;
14+
int num = 0;
15+
for(int i = 5; n/i > 0; i *= 5)
16+
num += n/i;
17+
return num;
18+
}
19+
int main(){
20+
for(int i=1; i<100; ++i){
21+
if(NumZeros(i) == NumZeros1(i))
22+
cout<<i<<endl;
23+
}
24+
return 0;
25+
}

19.4

17.1 KB
Binary file not shown.

19.4.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int Max1(int a, int b){
5+
int c[2] = {
6+
a, b
7+
};
8+
int z = a - b;
9+
z = (z>>31) & 1;
10+
return c[z];
11+
}
12+
int Max2(int a, int b){
13+
int z = a - b;
14+
int k = (z>>31) & 1;
15+
return a - k * z;
16+
}
17+
int main(){
18+
int a = 5, b = 10;
19+
cout<<Max1(a, b)<<endl;
20+
cout<<Max2(a, b)<<endl;
21+
return 0;
22+
}

19.5

18.5 KB
Binary file not shown.

19.5.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
struct Result{
6+
int hits;
7+
int pseudo_hits;
8+
};
9+
10+
Result Estimate(const char* solution, const char* guess){
11+
Result res;
12+
res.hits = 0;
13+
res.pseudo_hits = 0;
14+
int solution_mask = 0;
15+
for(int i=0; i<4; ++i){
16+
solution_mask |= 1 << (solution[i] - 'A');
17+
}
18+
for(int i=0; i<4; ++i){
19+
if(guess[i] == solution[i])
20+
++res.hits;
21+
else if(solution_mask & ( 1<<(guess[i] - 'A')))
22+
++res.pseudo_hits;
23+
}
24+
return res;
25+
}
26+
27+
int Min(int a, int b){
28+
return a < b ? a : b;
29+
}
30+
Result Estimate1(const char* solution, const char* guess){
31+
Result res;
32+
res.hits = 0;
33+
res.pseudo_hits = 0;
34+
int num = 26 + 5;
35+
int guess_count[num], solution_count[num];
36+
memset(guess_count, 0, sizeof(guess_count));
37+
memset(solution_count, 0, sizeof(solution_count));
38+
for(int i=0; i<4; ++i){
39+
if(guess[i] == solution[i])
40+
++res.hits;
41+
++guess_count[(int)(guess[i]-'A')];
42+
++solution_count[(int)(solution[i]-'A')];
43+
}
44+
char color[] = "RGBY";
45+
for(int i=0; i<4; ++i){
46+
int idx = (int)(color[i] - 'A');
47+
res.pseudo_hits += Min(guess_count[idx], solution_count[idx]);
48+
}
49+
res.pseudo_hits -= res.hits;
50+
return res;
51+
}
52+
int main(){
53+
char solution[] = "RYGB";
54+
char guess[] = "YRRR";
55+
Result res = Estimate(solution, guess);
56+
cout<<res.hits<<" "<<res.pseudo_hits<<endl;
57+
Result res1 = Estimate1(solution, guess);
58+
cout<<res1.hits<<" "<<res1.pseudo_hits<<endl;
59+
return 0;
60+
}

19.7

17.2 KB
Binary file not shown.

19.7.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
bool g_Invalid = false;
5+
6+
int GetMaxSum(int a[], int n){
7+
if(a==NULL || n<=0){
8+
g_Invalid = true;
9+
return 0;
10+
}
11+
g_Invalid = false;
12+
13+
int max_sum = 1<<31; // Min Int
14+
int cur_sum = 0;
15+
for(int i=0; i<n; ++i){
16+
if(cur_sum <= 0)
17+
cur_sum = a[i];
18+
else
19+
cur_sum += a[i];
20+
21+
if(cur_sum > max_sum)
22+
max_sum = cur_sum;
23+
}
24+
25+
return max_sum;
26+
}
27+
int main(){
28+
int a[] = {
29+
-2, -8, 3, -2, 4, -10
30+
};
31+
int max_sum = GetMaxSum(a, 6);
32+
if(g_Invalid)
33+
cout<<"Invalid Input!"<<endl;
34+
else
35+
cout<<max_sum<<endl;
36+
return 0;
37+
}

20.1

17.3 KB
Binary file not shown.

20.1.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int Add1(int a, int b){
5+
char *c = (char*)a;
6+
return (int)&c[b]; // c+sizeof(char)*b=a + b
7+
}
8+
int Add2(int a, int b){
9+
if(b == 0) return a;
10+
int sum = a ^ b; // 各位相加,不计进位
11+
int carry = (a & b) << 1; // 记下进位
12+
return Add2(sum, carry); // 求sum和carry的和
13+
}
14+
int Add3(int a, int b){
15+
while(b != 0){
16+
int sum = a ^ b;
17+
int carry = (a & b) << 1;
18+
a = sum;
19+
b = carry;
20+
}
21+
return a;
22+
}
23+
int main(){
24+
int a = 1123927350, b = -134310;
25+
cout<<Add1(a, b)<<endl;
26+
cout<<Add2(a, b)<<endl;
27+
cout<<Add3(a, b)<<endl;
28+
return 0;
29+
}

20.2

18.9 KB
Binary file not shown.

20.2.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
using namespace std;
4+
5+
void Swap(int &a, int &b){// 有可能swap同一变量,不能用异或版本
6+
int t = a;
7+
a = b;
8+
b = t;
9+
}
10+
void RandomShuffle(int a[], int n){
11+
for(int i=0; i<n; ++i){
12+
int j = rand() % (n-i) + i;// 产生i到n-1间的随机数
13+
Swap(a[i], a[j]);
14+
}
15+
}
16+
int main(){
17+
srand((unsigned)time(0));
18+
int n = 9;
19+
int a[] = {
20+
1, 2, 3, 4, 5, 6, 7, 8, 9
21+
};
22+
RandomShuffle(a, n);
23+
for(int i=0; i<n; ++i)
24+
cout<<a[i]<<endl;
25+
return 0;
26+
}

20.3

18.9 KB
Binary file not shown.

20.3.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
using namespace std;
4+
5+
void Swap(int &a, int &b){// 有可能swap同一变量,不能用异或版本
6+
int t = a;
7+
a = b;
8+
b = t;
9+
}
10+
void PickMRandomly(int a[], int n, int m){
11+
for(int i=0; i<m; ++i){
12+
int j = rand() % (n-i) + i;// 产生i到n-1间的随机数
13+
Swap(a[i], a[j]);
14+
}
15+
}
16+
int main(){
17+
srand((unsigned)time(0));
18+
int n = 9, m = 5;
19+
int a[] = {
20+
1, 2, 3, 4, 5, 6, 7, 8, 9
21+
};
22+
PickMRandomly(a, n, m);
23+
for(int i=0; i<m; ++i)
24+
cout<<a[i]<<endl;
25+
return 0;
26+
}

20.4

17.1 KB
Binary file not shown.

20.4.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int Count2s(int n){
5+
int count = 0;
6+
int factor = 1;
7+
int low = 0, cur = 0, high = 0;
8+
9+
while(n/factor != 0){
10+
low = n - (n/factor) * factor;
11+
cur = (n/factor) % 10;
12+
high = n / (factor*10);
13+
14+
switch(cur){
15+
case 0:
16+
case 1:
17+
count += high * factor;
18+
break;
19+
case 2:
20+
count += high * factor + low + 1;
21+
break;
22+
default:
23+
count += (high + 1) * factor;
24+
break;
25+
}
26+
27+
factor *= 10;
28+
}
29+
30+
return count;
31+
}
32+
int main(){
33+
for(int i=1; i<1000; ++i)
34+
cout<<i<<" "<<Count2s(i)<<endl;
35+
return 0;
36+
}

0 commit comments

Comments
 (0)