Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions 第7章贪心策略/7.1简单贪心/7-1鸡兔同笼.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include<iostream>

using namespace std;

int main() {
int a;
while(scanf("%d",&a)!=EOF) {
int minimun=0,maximun=0;
if(a%2==0) {
minimun=a/4+(a%4)/2;
maximun=a/2;
int maxinum=0,mininum=0;
if(a%2==0){
maxinum=a/2;
mininum=a/4+(a%4)/2;
}
printf("%d %d\n",minimun,maximun);
printf("%d %d\n",mininum,maxinum);
}
return 0;
}
12 changes: 5 additions & 7 deletions 第7章贪心策略/7.1简单贪心/7-2FatMouse Trade.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include<iostream>
#include<algorithm>

using namespace std;
/**
�����Լ۱��������
Expand All @@ -22,21 +21,20 @@ int main() {
while(scanf("%d%d",&m,&n)!=EOF) {
if(m==-1&&n==-1)break;
for(int i=0; i<n; i++) {
scanf("%lf%lf",&bean[i].weight,&bean[i].cost);
scanf("%lf%lf",&bean[i].weight,&bean[i].weight);
}
sort(bean,bean+n,Compare);
double answer=0;
for(int i=0; i<n; i++) {
if(m>bean[i].cost) {
answer+=bean[i].weight;
if(m>=bean[i].cost) {
m-=bean[i].cost;
answer+=bean[i].weight;
} else {
answer+=(bean[i].weight/bean[i].cost)*m;
answer+=(m/bean[i].cost)*bean[i].weight;
break;
}
}
printf("%0.3f\n",answer);
printf("%.3f\n",answer);
}

return 0;
}
12 changes: 5 additions & 7 deletions 第7章贪心策略/7.2区间贪心/7-4今年暑假不AC.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include<iostream>
#include<algorithm>

using namespace std;
/**
̰�IJ��ԣ����Ƚ���������
Expand All @@ -12,25 +11,24 @@ struct Program {
int endTime;
};

Program programs[MAXN];

bool Compare(Program x,Program y) {
return x.endTime<y.endTime;
}

int main() {
int n;
Program program[MAXN];
while(scanf("%d",&n)!=EOF) {
if(n==0)break;
for(int i=0; i<n; i++) {
scanf("%d%d",&programs[i].startTime,&programs[i].endTime);
scanf("%d%d",&program[i].startTime,&program[i].endTime);
}
sort(programs,programs+n,Compare);
sort(program,program+n,Compare);
int currentTime=0,answer=0;
for(int i=0; i<n; i++) {
if(programs[i].startTime>=currentTime) {
if(currentTime<=program[i].startTime) {
answer++;
currentTime=programs[i].endTime;
currentTime=program[i].endTime;
}
}
printf("%d\n",answer);
Expand Down
2 changes: 0 additions & 2 deletions 第8章递归与分治/8.1递归策略/8-2汉诺塔Ⅲ.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include<iostream>

using namespace std;

long long fac(int n) {
if(n==1) {
return 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include<iostream>

using namespace std;

int fun(int x,int y) {
if(y==1||x==y) {
return 1;
Expand Down
23 changes: 23 additions & 0 deletions 第8章递归与分治/8.1递归策略/test8-2全排列.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<iostream>
#include<algorithm>
using namespace std;
void pailie(string &str,int s,int t) {
if(s==t) {
cout<<str<<endl;
return;
} else {
for(int i=s; i<=t; i++) {
swap(str[s],str[i]);
pailie(str,s+1,t);
swap(str[s],str[i]);
}
}
}
int main() {
string str;
while(getline(cin,str)) {
sort(str.begin(),str.end());
pailie(str,0,str.size()-1);
}
return 0;
}
2 changes: 0 additions & 2 deletions 第8章递归与分治/8.2分治法/8-3Fibonacci.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include<iostream>

using namespace std;

int Fibonacci(int n) {
if(n==0||n==1) {
return n;
Expand Down
1 change: 0 additions & 1 deletion 第8章递归与分治/8.2分治法/8-4二叉树.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include<iostream>

using namespace std;
/**
二叉树
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ using namespace std;

void print(int num) {
int i=0;
while(pow(2,i)<=num) {
i++;
}
while(pow(2,i)<=num)i++;
i--;
if(i==0) {
printf("2(0)");
Expand Down