-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f20be7
commit c9c086f
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 1-循环左移.cpp : 定义控制台应用程序的入口点。 | ||
// | ||
|
||
#include "stdafx.h" | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int N; //the length of array | ||
|
||
void rotateleft(int *a, int n, int left) { | ||
int i, j, k, change; | ||
|
||
if(left >= n) | ||
left = left % n; | ||
if(left == 0) | ||
return ; | ||
|
||
for(i = 0; i + left < n; i += change) { | ||
if(i + 2 * left < n) | ||
change = left; | ||
else | ||
change = n -i - left; | ||
|
||
for(j = i; j < i + change; ++j) { | ||
a[j] = a[j] ^ a[j + left]; | ||
a[j + left] = a[j] ^ a[j + left]; | ||
a[j] = a[j] ^ a[j + left]; | ||
} | ||
|
||
if(n - i == left + change) { | ||
k = (left - change) % left; | ||
if(k == 0) | ||
return ; | ||
left = k; | ||
} | ||
} | ||
} | ||
|
||
int _tmain(int argc, _TCHAR* argv[]) | ||
{ | ||
int left; //rotate left | ||
int a[] = {1, 2, 3, 4, 5, 6, 7}; | ||
|
||
cin >>left; | ||
|
||
N = sizeof(a)/sizeof(int); | ||
|
||
rotateleft(a, N, left); | ||
|
||
for(int i = 0; i < N; ++i) | ||
cout <<a[i] <<" "; | ||
cout <<endl; | ||
|
||
system("pause"); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 2-扔鸡蛋.cpp : M个鸡蛋,N层楼 | ||
//确定在哪一层鸡蛋会破。 | ||
//dp[m][n] = min(dp[m][n], 1 + max(dp[m - 1][i - 1], dp[m][n - i])) i = 1...n | ||
//典型的DP问题 | ||
|
||
#include "stdafx.h" | ||
#include <iostream> | ||
using namespace std; | ||
|
||
#define N 100 | ||
#define M 100 | ||
|
||
int _tmain(int argc, _TCHAR* argv[]) | ||
{ | ||
int n; | ||
int dp[M + 1][N + 1]; //记录M个鸡蛋,N层楼的解 | ||
memset(dp, 0, sizeof(dp)); | ||
for(int i = 1;i <= N; ++i) //1个鸡蛋 | ||
dp[1][i] = i; | ||
|
||
for(int i = 2; i <= M; ++i){ | ||
for(int j = 1; j <= N; ++j) { | ||
int min = 0x0FFFFFFF; | ||
for(int k = 1; k <= j; ++k){ | ||
if(min > 1 + max(dp[i - 1][ k - 1], dp[i][ j - k])) | ||
min = 1 + max(dp[i - 1][ k - 1], dp[i][ j - k]); | ||
} | ||
dp[i][j] = min; | ||
} | ||
} | ||
|
||
cout <<dp[2][100]; | ||
system("pause"); | ||
return 0; | ||
} | ||
|