exercise3
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第三次作业
1、两名参与者交替从一堆石子中取出若干数目,其个数由参与者自已决定.但是要求参与者每次至少取出一个,至多取出一半,然后另一名参与者继续.拿到最后一个石子的参与者将输掉该游戏.
输入:输入开始时石子的个数
输出:如果先取者输,则输出“lose”,否则输出“win”
#include
#include
using namespace std;
int main(){
int number;
cin>>number;
double i=1;
double k=pow(2.0,i)-1;
while(k ++i; k=pow(2.0,i)-1; } if(k==number) cout<<"lose"; if(k>number) cout<<"win"; return 0; } 2、Given some Chinese Coins (three kinds-- 1, 2, 5), and their number is num_1, num_2 and num_5 respectively, please output the minimum value that you cannot pay with given coins. Input Input contains multiple test cases. Each test case contains 3 positive integers num_1, num_2 and num_5 (0<=num_i<=1000). A test case containing 0 0 0 terminates the input and this test case is not to be processed. Output Output the minimum positive value that one cannot pay with given coins, one line for one case. #include #include #include using namespace std; const int MAX = 1000; const int type[3] = {1,2,5}; bool ans[MAX]={0},tmp[MAX]={0}; int cnt[3]; void Search(){ int i,j,k; for(i=0;i<=cnt[0];++i){ ans[i]=true; } for(i=1;i<3;++i){ for(j=0;j for(k=0;k<=cnt[i] && j+k*type[i]<=MAX;++k){ if(tmp[j+k*type[i]] || ans[j]) tmp[j+k*type[i]]=true; } } for(j=1;j<=MAX;++j){ ans[j]=tmp[j]; tmp[j]=false; } } } int main(){ int res=0; scanf("%d %d %d",&cnt[0],&cnt[1],&cnt[2]); if(cnt[0]==0 && cnt[1]==0 && cnt[2]==0){ printf("0"); return 0; } Search(); for(int i=0;i<=MAX;++i){ if(!ans[i]){ res=i; break; } } printf("%d",res); return 0; } 3、写出完整的二分查找程序,要求: (1)先将给定的数据利用快速排序进行排序(采用复杂度为nlg(n)的算法)。 (2)利用二分查找法判断给定的数据是否在数据集中。 #include #include using namespace std; void quicksort(int a[],int left,int right){ if(left>=right) return; else{ int first=left; int last=right; int key=a[first]; while(first { while(first --last; a[first]=a[last]; while(first ++first; a[last]=a[first]; } a[first]=key; quicksort(a,left,first-1); quicksort(a,first+1,right); } } int search(int num,int a[],int size){ int left=0; int right=size-1; int mid=(left+right)/2; while(left<=right){ if(a[mid]==num) return mid; else if(a[mid]>num) right=mid-1; else left=mid+1; mid=(left+right)/2; } return 0; } int main(){ int s[]={2,1,5,87,43,23,9}; quicksort(s,0,6); int num=23; if(search(num,s,7)) printf("给定的数据在数据集中!"); else printf("给定的数据不在数据集中!"); return 0; } 4、 Problem Description You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings. Input The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string. Output There should be one line per test case containing the length of the largest string found.