题目:将20条鱼放入10个桶中,每个桶可以放0~10条,总共有多少种方法。
int Function(int NumOfBox,int NumOfFish,int max) { if(NumOfBox==0 || NumOfFish==0 || (NumOfBox==1&&NumOfFish>10)) return 0; if(NumOfBox==1&&NumOfFish<=10) return 1; int ret = 0; for(int i=0;i<=max && i<=NumOfFish;++i) { ret+=Function(NumOfBox-1,NumOfFish-i,max); } //cout<<ret<<endl; return ret; } int main() { cout<<Function(10,20,10)<<endl; return 0; }
int LongestPalindrome(char* str,int length) { if(str==NULL || length<0) return -1; if(length==0 || length==1) return length; int maxNum = 1; char* left = NULL; char* right = NULL; for(int i = 0;i!=length-1;++i) { //子串奇数的情况 left = str+i; right = str+i; while (left>=str && right<=str+length-1) { if(*left==*right) { --left; ++right; } else break; } if(right-left-1>maxNum) maxNum = right-left-1; //子串偶数的情况 left = str+i; right = str+i+1; if(*left == *right) { while (left>=str && right<=str+length-1) { if(*left==*right) { --left; ++right; } else break; } } if(right-left-1>maxNum) maxNum = right-left-1; } return maxNum; }
char myArray[100]={0}; void Helper(int n,int left,int right); void PrintAllTheBrackets(int n) { if(n<1) return; Helper(n,0,0); } void Print(char* a,int length) { if(a==NULL && length<1) return; for (int i = 0;i!=length;++i) cout<<*(a+i); cout<<endl; } void Helper(int n,int left,int right) { if(left==n && right==n) { Print(myArray,n*2); return; } int index = left + right; if(left<n) { myArray[index] = '('; Helper(n,left+1,right); } if(right<left) { myArray[index] = ')'; Helper(n,left,right+1); } } int main() { PrintAllTheBrackets(3); return 0; }
int CountDots(int* a,int arrayLength,int ropeLength) { if(a==NULL || arrayLength<1 || ropeLength<0) return -1; int* first = a; int* second = a; int ret = 1; while (second<a+arrayLength) { if(ropeLength>=*second-*first) { if(second-first+1>ret) ret = second-first+1; ++second; } else ++first; } return ret; } int main() { int a[] = {1,3,7,8,9,11}; cout<<CountDots(a,6,4)<<endl; return 0; };
int maxnumstr(char *inputstr, char *outputstr) { if(inputstr==NULL || outputstr==NULL) return -1; int ret = 0; int count = 0; char* maxstart=NULL; char* temp = NULL; for (char* iter = inputstr;*iter!='\0';++iter) { if(*iter>='0'&&*iter<='9') { ++count; if(temp==NULL) temp = iter; } else { if(count>ret) { ret = count; count=0; maxstart = temp; temp = NULL; } } } if(maxstart!=NULL) for (int i = 0;i!=ret;++i) *outputstr++=*maxstart++; *outputstr='\0'; //这句代码不能遗漏! return ret; } int main() { //测试用例 char *inputstr = "hello123world456789over1245664mm"; char *outputstr = (char*)malloc(50); memset(outputstr,0,50); int count = maxnumstr(inputstr,outputstr); printf("The longest digital string is: "); puts(outputstr); printf("The length is: %d\n",count); free(outputstr); system("pause"); return 0; }
每个不小于6的偶数都可以表示为两个素数之和
编写一个函数,输出6-100000内所有偶数可以表示为哪两个素数之和,如果一个偶素有多种表示方式,输出任意一种即可。
bool IsPrime(int num) { for(int i=2;i<=sqrt(static_cast<double>(num));++i) if(!(num%i)) return false; return true; } void Judge(int even) { for(int i=1;i!=even/2;++i) if(IsPrime(i)&&IsPrime(even-i)) cout<<even<<" "<<i<<" + "<<even-i<<endl; } void Print() { for (int i =6;i!=1000;i+=2) Judge(i); }
void Print(int n) { if(n<1) return; int* arr = new int[n*n]; arr[0] = 1; int i = 0; //行 int j = 1; //列 int value = 2; //数值 for(int limit = 2;limit<=n;++limit) { if(!(limit&1))//偶数行,先向下,再向左。 { while (i!=limit) { arr[n*i+j]=value++; cout<<n*i+j<<endl; ++i; } --i; --j; while (j>=0) { arr[n*i+j]=value++; --j; } ++j; ++i; } else //奇数行,先向右,再向上。 { while (j!=limit) { arr[n*i+j]=value++; ++j; } --j; --i; while (i>=0) { arr[n*i+j]=value++; --i; } ++i; ++j; } } for(int i=0;i!=n;++i) { for(int j=0;j!=n;++j) cout<<arr[n*i+j]<<" "; cout<<endl; } }
一个字符串只由'+','-',和‘0’-‘9’组成,并且'+','-'只作为二元运算符。
bool calculate(const char* exp, int &result);
bool calculate(const char* exp, int &result) { if(exp==NULL) return false; char opera; int temp; result = *exp++-'0'; while(*exp!='\0') { opera = *exp++; temp = *exp++-'0'; if(opera=='+') result+=temp; else result-=temp; } return true; }
int Print(int x,int y) { if(x==0 && y==0) return 1; bool isPlus=true; //递增或递减标识符 int count = 1; //每次递增或递减次数 int num = 1; //返回值 int i=0; //行 int j=0; //列 while(true) { if(isPlus) { for(int m =0;m!=count;++m) { ++i; ++num; if(i==x && j==y) return num; } for(int m =0;m!=count;++m) { ++j; ++num; if(i==x && j==y) return num; } ++count; isPlus = false; } else { for(int m =0;m!=count;++m) { --i; ++num; if(i==x && j==y) return num; } for(int m =0;m!=count;++m) { --j; ++num; if(i==x && j==y) return num; } ++count; isPlus = true; } } }
题目:输出程序自身源代码
//输出程序自身 ifstream ifile("1.cpp"); string s; if(ifile) { while (getline(ifile,s)) cout<<s<<endl; } else cout<<"不能打开文件!";