Skip to content

Commit

Permalink
RotateLeft and drop eggs programs
Browse files Browse the repository at this point in the history
  • Loading branch information
dqiushuang committed Jul 12, 2013
1 parent 5f20be7 commit c9c086f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
57 changes: 57 additions & 0 deletions 1-RotateLeft.cpp
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;
}

36 changes: 36 additions & 0 deletions 2-DropEggs.cpp
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;
}

0 comments on commit c9c086f

Please sign in to comment.