自己写的,水平很挫,仅供参考
目录
1.语言识别问题
2.销售网络问题(未完成) 3.股票投资问题
4.判断手机号码合法性 5.元音字母复制 6.验证身份证号 7.选秀节目打分
8.数组最大值放中间,其他依次放其左右(规律未找着,未完成)
9.任务调度(解题关键,需要一个容器来承载下标跟值的一一对应关系,最好就是定义一个结构体) 10.将某字符变成小写后的某个字符 11.链表的逆序 12.单词统计
13.字符串进行转换,转换成相应的数字已知:yi er san si wu liu qi ba jiu 分别对应,对一段只含有这几种字符的字符串进行转换,转换成相应的数字 14.一个数组中比平均数大的个数 15.求一个数组中第一大和第二大数 16.字符变成整数 17.整数变字符 18.判断素数问题
19(1).约瑟夫环(循环列表) 19(2).约瑟夫环(数学方法只能求出最后的胜利者的序号) 19(3).约瑟夫环(容器实现) 20.判断某个整数是回文。即这样的,反过来还是 21.判断一个字符串是不是回文
22.求一个字符串中的最大回文子串,就是从n个字符开始检查是不是回文,知道m个字符符合回文,那么这个就是最大回文 23.找出^n的数
24.统计一个数二进制表达中的个数
25.镜像反转二进制表达式,并输出十进制值 26.连续字符统计
27.判断一个字符串中()是否配对 28.查找子字符串个数
29(1).找出一个字符串中是否包含相同(包括连续的)的子字符串(要求子串长度大于等于)并输出出现频率最高的子字符串
29(2)找出一个字符串中是否包含连续相同的子字符串,并输出出现频率最高的子字符串 30.删除字符窜中字符数最少的字符
31.关于数组的循环移位,左移为负,右移为正 32.求一个二维数组每列的最小值 33.两个字符串,求最长公共子串
34.超大整数加法运算,大整数会用字符串或者数组来存,不过注意低位存字符前面几位,高位存后面,存到字符中应该存“”。这边我用的是数组 35.排序总结
36.将一个字符串空格分隔,并倒序输出 37.删除一个字符串中的某个字符串
38.取出一个字符串中所有的数字,并取出所有字母 39,简单的字符统计
40.查找字符串中空格分隔的单词的最大长度 41.二叉树的操作 42.分块查找
1.语言识别问题
#include
using namespace std; void main() {
int n,S_num=0,T_num=0,m=0;
cin>>n;
char ch;
getchar();
for(int i=0;i
// m=0;
while(1)
{
ch=getchar();
/* m++;
if((m>3)&& (ch==\'\\n\'))
{
m=0;
break;
}*/
if(ch==\'\\n\') break;
if(ch==\'s\'||ch==\'S\') S_num++;
if(ch==\'t\'||ch==\'T\') T_num++;
}
}
if(S_num
else cout
}
2.销售网络问题(未完成)
#include
using namespace std; void main() {
int n,S_num=0;
cin>>n;
int a[n]; for(int i=0;i
cin>>a[i]; if(a[]) for(int i=0;i
// m=0;
while(1)
{
ch=getchar();
/* m++;
if((m>3)&& (ch==\'\\n\'))
{
m=0;
break;
}*/
if(ch==\'\\n\') break;
if(ch==\'s\'||ch==\'S\') S_num++;
if(ch==\'t\'||ch==\'T\') T_num++;
}
}
if(S_num
else cout
}
3.股票投资问题
#include
using namespace std; void main() {
int B,C=0,D=0,E=0,i,j,k,l,n,m;
int A;
int a[12];//未来天得股价
int b[12][12];
cin>>B;//测试数
memset(b,0,sizeof(b));
//for(i=0;i
cin>>A; for(j=0;j
cin>>a[j];
int temp=0; for(k=0;k
for(l=k+1;l
{
temp=A/a[k]; b[k][l]=temp*(a[l]-a[k]); if(b[k][l]
b[k][l]=0;
}
int max=b[0][1]; m=0; n=1;
for(k=0;k
for(l=k+1;l
{
if(b[k][l]>max)
{ max=b[k][l];
m=k;
n=l;
}
if(b[k][l]==max)//相等的取购价低的
{ if(a[k]
{ max=b[k][l];
m=k;
n=l;
}
}
}
if (max==0)
{cout
}
else{
C=m+1;
D=n+1;
E=max;
cout
}
}
4.判断手机号码合法性
#include
using namespace std;
int verifyMsisdn(char* inMsisdn) { int n=0; int i=0; int j=0; char *p; p=inMsisdn;
while(p[i]!=\'\\0\')
{
i++;
n++;
}
if(n!=13)
return 1;
else
{
while(p[j]!=\'\\0\')
{
if(!((p[j]>=\'0\' && p[j]
{return 2;
break;
}
j++;
} if(!(p[0]==\'8\'&& p[1]==\'6\')) return 3; else return 0;
}
}
void main() {
char a[20]; cin>>a; int m=verifyMsisdn(a);
cout
}
5.元音字母复制
#include
using namespace std;
void sortVowel (char* input) { int j=0; char output[50]={0};
for(int i=0;input[i]!=\'\\0\';i++)
{ if(input[i]==\'a\' || input[i]==\'e\'|| input[i]==\'i\'|| input[i]==\'o\'|| input[i]==\'u\')
{
output[j]=input[i];
j++;
}
}
int w=j; char temp; for(int k=0;k
if(output[l]>output[l+1])
{
temp=output[l];
output[l]=output[l+1];
output[l+1]=temp;
} }
for(int i=0;input[i]!=0;i++)
{ if( input[i]==\'A\'|| input[i]==\'E\'|| input[i]==\'I\'|| input[i]==\'O\'|| input[i]==\'U\')
{
output[j]=input[i];
j++;
}
}
char temp2; for(int m=w;m
if(output[n]>output[n+1])
{
temp2=output[n];
output[n]=output[n+1];
output[n+1]=temp2;
} }
cout
char a[50]; cin.get(a,50);
sortVowel(a);
}
6.验证身份证号
#include
using namespace std;
int verifyIDCard(char* input) { int n=0; int i=0; int j=0; char *p; p=input;
while(p[i]!=\'\\0\')
{
i++;
n++;
}
if(n!=18)
return 1;
else
{
while(j
{
if(!((p[j]>=\'0\' && p[j]
{return 2;
break;
}
j++;
} if(!(p[17]==\'x\'|| (p[17]>=\'0\' && p[17]
{
int year=(p[6]-\'0\')*1000+(p[7]-\'0\')*100+(p[8]-\'0\')*10+(p[9]-\'0\');
int month=(p[10]-\'0\')*10+(p[11]-\'0\');
int day=(p[12]-\'0\')*10+(p[13]-\'0\');
if(!(year>=1900 && year
return 4; else{
if(!(month>=1 && month
return 5; else{
//能被整除且不能被整除或能被整除的年份
bool ryear=(year%4==0 && year%100!=0) || (year%400==0);
if(!((!ryear && day>0 && day0 && day
return 6;
else{
return 0;
}
}
}
}
}
}
void main() {
for(int c=0;c>a; int m=verifyIDCard(a);
cout
7.选秀节目打分
#include
using namespace std; #define N 5 int total(int score[],int judge_type[],int cn)
{ int sum1=0,sum2=0,m=0,n=0,aver=0,totalscore=0;
for(int i=0;i
{sum1+=score[i];
m++;
} else {
sum2+=score[i];
n++; }
}
if(n==0)
totalscore=sum1/m;
else
{ totalscore=(int)(sum1/m * 0.6+sum2/n * 0.4);}
return totalscore;
}
void main() {
int score[N]; int judge_type[N]; for(int i=0;i
cin>>judge_type[i];
cout
cin>>score[i]; } int totalscore= total(score,judge_type,N); cout
8.数组最大值放中间,其他依次放其左右(规律未找着,未完成)
#include using namespace std;
void sort(int input[], int n, int output[]) {
int i,j;
int temp =0;
for(i =0; i
for(j =0; j
{
if(input[j]>input[j+1])
{
temp = input[j];
input[j] = input[j+1];
input[j+1] = temp;
}
}
if(n%2 ==0) {
for(i =0 ; i
{
output[i] = input[2*i];
}
for(i =0; i
{
output[n/2+i] = input[n-1-2*i];
} } else {
for(i=0; i
{
output[i] = input[2*i+1];
}
output[(n-1)/2]= input[n-1];
for(i = 0; i
{
output[(n-1)/2+1+i] = input[n-3-2*i];
} }
for(i = 0 ; i
printf(\"%d\", output[i]); } printf(\"\\n\"); }
int main() { int input1[] = {3, 6, 1, 9, 7}; int input2[] = {3, 6, 1, 9, 7, 8};
int output1[5] = {0};
int output2[6] = {0};
sort( input1, 5,output1) ;
sort(input2, 6, output2) ; } 9.任务调度
(解题关键,需要一个容器来承载下标跟值的一一对应关系,最好就是定义一个结构体) #include using namespace std; struct table { int number;
int value; };
void scheduler(int task[], int system_task[], int user_task[],int n) {
struct table *sb=(struct table *)malloc(n*sizeof(struct table));
for(int i=0; i
sb[i].number=i; sb[i].value=task[i];
}
struct table temp; for(int k=0; k
for(int j=0; j
{
if(sb[j].value>sb[j+1].value)
{
temp=sb[j];
sb[j]= sb[j+1];
sb[j+1] = temp;
}
}
int cs=0,cu=0; for(int l=0; l
if(sb[l].value
{system_task[cs]=sb[l].number;
cs++;
}
else if(sb[l].value
{user_task[cu]=sb[l].number;
cu++;
}
else
continue;
} system_task[cs]=-1; user_task[cu]=-1;
free(sb);
for(int m=0;m
{
cout
}
printf(\"\\n\");
for(int n=0;n
{
cout
}
}
int main() { int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99};
int n=9;
int count_sys=0,count_user=0; for(int i=0;i
if(task[i]
count_sys++;
else if(task[i]
count_user++;
else
continue; }
int *system_task=(int *)malloc(count_sys*sizeof(int)+4); int *user_task=(int *)malloc(count_user*sizeof(int)+4);
scheduler(task, system_task, user_task,9); //int *p = system_task; //int *q = user_task; //
////printf(\"%d%d\\n\", count_sys,count_user); // for(int i=0;i
} // } // printf(\"\\n\"); // //
for(int i=0;i
free(system_task);
free(user_task); 10.将某字符变成小写后的某个字符
#include using namespace std; void TransferString(const char * pInputStr, long lInputLen, char * pOutputStr) {
for(int i=0;i
{
if(pInputStr[i]>=\'V\'&& pInputStr[i]
pOutputStr[i]=pInputStr[i]+11;//(\'a\'\'A\');
else if (pInputStr[i]>=\'A\'&& pInputStr[i]
pOutputStr[i]=pInputStr[i]+37;//(\'a\'\'A\');
else{pOutputStr[i]=pInputStr[i];}
}
cout
}
void main() { char *pInputStr=\"Axs3mW\"; int n=0; while(pInputStr[n]!=\'\\0\')
n++;
long
lInputLen=n+1;
char *pOutputStr=(char *)malloc(sizeof(char)*(n+1));
TransferString(pInputStr,lInputLen,pOutputStr);
}
11.链表的逆序
#include using namespace std; typedef struct tagListNode {
int value;
struct tagListNode *next;
}ListNode;
//要求实现函数:
void converse(ListNode *head) {
ListNode *p1,*p2,*p3;
p1=head;
p2=p1->next;
while(p2) {
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3; } head->next=NULL; head=p1; while(p1!=NULL)
{
coutvalue\";
p1=p1->next;
} }
void main() {
ListNode *p,*head,*s;
head=(ListNode*)malloc(sizeof(ListNode));
p=head;
int n=0,m=0;
while(n
{
cin>>m;
s=(ListNode*)malloc(sizeof(ListNode)); s->value=m;
p->next=s;
p=s;
n++;
}
head=head->next; p->next=NULL;
converse(head); //p=head; //while(p!=NULL) //
{ //
coutvalue\"; // p=p->next; //
//
}
}
12.单词统计
#include #include using namespace std; struct node { //int number;
int count; char a[10]; }; void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord) {
//cout
if(pInputStr1[i]>=\'A\' && pInputStr1[i]
pInputStr1[i]+=32;
i++; } const char * split = \", .\";
struct node sb[10]={0};//*sb=(struct node *)malloc(10*sizeof(struct node)); char *p={0};
p=strtok (pInputStr1,split); int j=0; while(p!=NULL)
{ //sb[j].number=j;
strcpy(sb[j].a,p);
sb[j].count=0;
j++;
p=strtok (NULL,split);
}
for(int k=0;k
for(int l=0;l
{
if (strcmp(sb[k].a,sb[l].a)==0)
sb[k].count+=1;
}
struct node max;
struct node min;
int dex1=0,dex2=0;
max=sb[0];
min=sb[0];
for(int m=0;m
{
if(sb[m].count>max.count)
{ max=sb[m];
dex1=m;}
else if((sb[m].count
{
min=sb[m];
dex2=m;}
}
/*for(int m=0;m
{
cout
cout
}*/
strcpy(pOutputHotWord,sb[dex1].a); strcpy(pOutputColdWord,sb[dex2].a); cout
}
void main() { char
pInputStr[100]={0}; cin.get(pInputStr,100);
char * pOutputHotWord=( char *)malloc(sizeof( char *)*100);
char * pOutputColdWord=( char *)malloc(sizeof( char *)*100);
memset(pOutputHotWord, 0, sizeof(pOutputHotWord)) ;
memset(pOutputColdWord, 0, sizeof(pOutputHotWord)) ; WordStat(pInputStr, pOutputHotWord,pOutputColdWord); }
13.字符串转换成规定数字
转换成相应的数字已知:yi er san si wu liu qi ba jiu 分别对应,对一段只含有这几种字符的字符串进行转换,转换成相应的数字
如:yiersansan:
#include #include using namespace std;
int WordStat(const char * pInputStr, char * pOutputWord) {
int i=0,d=0,k=0,sum=0; char *pInputStr1=(char *)malloc(100*sizeof(char)); strcpy(pInputStr1,pInputStr);//(char *) char* [9] = {\"yi\", \"er\", \"san\", \"si\", \"wu\", \"liu\", \"qi\", \"ba\", \"jiu\"}; while(pInputStr1[i]!=\'\\0\') {
if(pInputStr1[i]==\'y\' || pInputStr1[i]==\'e\'|| pInputStr1[i]==\'w\'|| pInputStr1[i]==\'q\'|| pInputStr1[i]==\'b\')
d=2;
if(pInputStr1[i]==\'l\' || pInputStr1[i]==\'j\')
d=3;
if(pInputStr1[i]==\'s\')
{ if(pInputStr1[i+1]==\'a\')
d=3;
if(pInputStr1[i+1]==\'i\')
d=2;
}
for(int j=0;j
{
if(strncmp(pInputStr1+i,[j],d)==0)
k=j+1;
} sum=sum*10+k;
i+=d; } return sum;
}
void main() { char
pInputStr[100]={0}; cin.get(pInputStr,100);
char * pOutputWord=( char *)malloc(sizeof( char *)*100);
memset(pOutputWord, 0, sizeof(pOutputWord)) ;
int transver= WordStat(pInputStr, pOutputWord); cout
}
14.一个数组中比平均数大的个数
#include #include using namespace std;
int count(int p[], int n) {
int sum=0,m=0; for(int i=0;i
sum+=p[i]; } int aver=sum/n; for(int j=0;j
if(p[j]>aver)
m++; } return m; }
void main() { cout>n; int *a=(int*)malloc(sizeof(int)*n); for(int i=0;i>a[i]; }
int m=count(a,n);
cout
}
15.求一个数组中第一大和第二大数
#include #include using namespace std;
void count(int p[], int n) {
int max=0,smax=0,k=0; for(int i=0;i
if(p[i]>max)
{ max=p[i];
k=i;
} }
for(int j=0;j
if(p[j]>smax)
smax=p[j]; } cout
void main() { cout>n; int *a=(int*)malloc(sizeof(int)*n); for(int i=0;i>a[i];
}
count(a,n);
}
16.字符变成整数
#include #include using namespace std;
int chartoint(const char * pInputStr) {
int i=0,d=0,k=0,sum=0;
while (pInputStr[i]!=\'\\0\')
{
d=pInputStr[i]-\'0\';
sum=sum*10+d;
i++;
}
return sum;
}
void main() { char
pInputStr[100]={0}; cin.get(pInputStr,100);
int transver= chartoint(pInputStr); cout
}
17.整数变字符
#include using namespace std;
void main() { int n,i=0; cin>>n; //int m=(int)sqrt(n); char p[50]={0},s[50]={0}; while(n) {
p[i]=n%10+\'0\';
i++;
n/=10; } p[i]=\'\\0\'; int m=strlen(p); //char *s=(char *)malloc(sizeof(char)*(m+1));
for (int j = 0; j
s[j]=p[m-1-j];
s[m]=\'\\0\';
cout
#include #include using namespace std;
void main() { int n,i=0; cin>>n; //int m=(int)sqrt(n); for(i=2;i
if(n%i==0)
break;
} if(i==n)
cout
}
19.1约瑟夫环
#include using namespace std; typedef struct LNode {
int data;
struct LNode*link; }LNode;
void jos(int n,int k,int m) {
LNode *p,*curr,*r;
p=(LNode*)malloc(sizeof(LNode));
p->data=1;//注意我是从开始的奥
p->link=p;
curr=p;
for(int i=2;i
LNode *s=(LNode*)malloc(sizeof(LNode)); s->data=i;
s->link=curr->link; curr->link=s; curr=s; }//循环链表的建立
while(--k) { r=p; p=p->link;
}//======================p指向序号为k的位置
int w=m; while(n--) {
while(--m)
{r=p;
p=p->link;
}//======================p指向序号为k的之后的m位置上
coutdata\";
r->link=p->link;
p=r->link;
m=w;
}
}
void main() {
jos(9,1,5); } //5->1->7->4->3->6->9->2->8-> 19.2约瑟夫环(数学方法只能求出最后的胜利者的序号)
#include int main() {
int n,m,i,s = 0;
printf(\"N M =\");
scanf(\"%d%d\",&n,&m);
for(i = 2; i
{
s = (s + m) % i;
}
printf(\"n The winner is %dn\",s+1); }
19.3约瑟夫环(容器实现) #include
#include using namespace std;
const int N = 9;
const int M = 5;
const int k = 1;
int main(int argc, char* argv[])
{
vector ring;
for(int i=0; i
ring.push_back(i+1);
vector::iterator iBegin = ring.begin();
vector::iterator iEnd;
while ( !ring.empty() )
{
iEnd = ring.end();
if(iBegin == iEnd )
iBegin = ring.begin();
for(int i=1;i
{
iBegin++;
if(iBegin >= iEnd)
iBegin = ring.begin();
}
cout
iBegin = ring.erase(iBegin);
}
}
20.判断某个整数是回文。即这样的,反过来还是
#include using namespace std;
bool func(int m); void main() { int m; cout>m; cout
int i,n=0; i=m;
while(i)
{
n=n*10+i%10;
i/=10;
}
if(m==n)
return true;
return false; }
21.判断一个字符串是不是回文
#include using namespace std; #include bool is_huiwen(char a[],int length) {
const char *src=a;
const char *end;
end=src+length-1;
while(src
{ if(*src==*end)
{ src++;end--;}
else return false;
}
return true;
}
int main() { int len;
char c[10];
cout
cin>>c;
len=strlen(c);
bool h;
h=is_huiwen(c,len);
if(h) cout
else cout
return 0; }
22.求一个字符串中的最大回文子串
就是从n个字符开始检查是不是回文,知道m个字符符合回文,那么这个就是最大回文
#include using namespace std; #include char * maxhuiwen(char a[],int length,char b[]) { int i=0,j=0,k=0; for(i=length;i>0;i--)//回文的长度
for( j=0;j
{
for( k=0;j+k
{
if(a[j+k]==a[j+i-1-k])
continue;
else{break;}
}
if(j+k>=j+i-1-k)
{
int n1=i;//长度
int n2=j;//起始位置
cout
cout
memcpy(b,a+j,i);
b[i]=\'\\0\';
return b;
}
}
}
void main() { int len;
char c[50];
cout
cin>>c;
len=strlen(c);
char * output={0}; output =(char *)malloc(sizeof(char)*(len+1)); char *s={0};
s=maxhuiwen(c,len,output); cout
23.找出^n的数
#include using namespace std;
void func(int a[],int n) {
for(int i=0;i
if(0==(a[i]&(a[i]-1)))
cout
} void main() { int a[5]; for(int i=0;i
cin>>a[i]; int n=sizeof(a)/sizeof(int); cout
24.统计一个数二进制表达中的个数
#include using namespace std;
int func(int a) {
int c=0,i=0; while(a) {
if((a & 1)==0)
{c++;} /* i++;*/
a=a>>1;
} return c;
} void main() { int a;
cin>>a; int m=func(a); cout
25.镜像反转二进制表达式,并输出十进制值#include using namespace std; int func(int a) {
int c=0,i=0; int b[32]; while(a) {
b[i]=(a & 1);
i++;
a=a>>1;
} for(int j=0;j
c=c*2+b[j];
return c;
} void main() { int a;
cin>>a; int m=func(a); cout
26.连续字符统计
#include using namespace std; #include #include using namespace std; #include char * maxcommonchar(char a[],int length) { int n=1; char *p=(char *)malloc(sizeof(char)*(length*2)); memset(p,0,sizeof(p));//需要初始化
for(int i=0;i
if (a[i]==a[i+1])
{n++;}
else
{
sprintf(p+strlen(p),\"%c%d\",a[i],n);
n=1;
}
}
return p;
}
void main() {
char *a=\"addff\";
int n=strlen(a); cout
} 27.判断一个字符串中()是否配对
#include using namespace std; bool match(char a[],int length); int main() {
char b[100];
int len;
bool m;
cout
gets(b);
len=strlen(b);
m=match(b,len);
if(m) cout
else cout
return 0; }
bool match(char a[],int length) {
char *p=a;
int count1=0;
int count2=0;
while(*p!=\'\\0\')
{
if(*p==\'(\') count1++;
if(*p==\')\') count2++;
if(count2>count1)
return false;
p++;
}
if(count1==count2)
return true;
else
return false; } 28.查找子字符串个数
#include using namespace std;
int fun(char a[],char b[]) {
int n=0;
int n1=strlen(a); int n2=strlen(b);
for(int i=0;i
}
return n; }
void main() {
char a[100],b[100];
cin>>a;
cin>>b;
int n=fun(a, b);
cout
29.1找出一个字符串中是否包含相同(不管是不是连续的)的子字符串
(要求子串长度大于等于)并输出出现频率最高的子字符串
#include using namespace std; void fun(char a[]) {
int n=strlen(a); int m=0; char b[100];
int p=0;
int s1[10],s2[10],s3[10]; for(int i=2;i
{ strncpy(b,a+j,i);
b[i]=\'\\0\';
for(int k=0;k
{ if(strncmp(b,a+j+k,i)==0)
{
m++;
}
} if(m>=2) {s1[p]=m; s2[p]=j; s3[p]=i; p++; } m=0; }
int max=0;
int l=0;
for(int q=0;q
if(s1[q]>max)
{ max=s1[q];
l=q;
}
for(int o=0;o
void main() {
char a[100];
cin>>a;
fun(a); /*if(fun(a)) {cout
}
29.2找出一个字符串中是否包含相同并且连续的子字符串,并输出出现频率最高的子字符串
#include #include using namespace std; void fun(char a[]) {
int n=strlen(a);
char *s[100]; for(int n3=0;n3
s[n3]=(char *)malloc(20*sizeof(char)); }
for(int n1=0;n1
strcpy(s[n1],a+n1);
/*for(int n2=0;n2
cout
int c=1;
int max=0;
int m=0;
int p;
for(int i=0;i
for(int j=i+1;j
{ if(strncmp(s[i],s[j],j-i)==0)
{ c++;
for(int k=j+j-i;k
{ if(strncmp(s[i],s[k],j-i)==0)
{
c++; } else{break;}
}
if(c>max)
{ max=c;
m=i;
p=j;
}
}
}
for(int o=0;o
char a[100];
cin>>a;
fun(a); /*if(fun(a)) {cout
}
30.1删除字符窜中字符数最少的字符
#include #include using namespace std; struct node { int count; char a; }; char *minWorddelete( char * pInputStr, char * pOutputWord) { int n=strlen(pInputStr);
struct node sb[100]={0};//*sb=(struct node *)malloc(10*sizeof(struct node));
for(int i=0;i
{
sb[i].a=pInputStr[i];
sb[i].count=0;
} for(int k=0;k
for(int l=0;l
{
if (sb[k].a==sb[l].a)
sb[k].count+=1;
}
struct node min;
int dex1=0;
min=sb[0];
int m=0; for(m=0;m
{
if(sb[m].count
{
min=sb[m];
dex1=m;
}
} cout
{
if(sb[p].a!=sb[dex1].a)
{
pOutputWord[q]=sb[p].a;
q++;
}
} pOutputWord[q]=\'\\0\';
return pOutputWord;
}
void main() { char
pInputStr[100]={0}; cin.get(pInputStr,100);
char * pOutputWord=( char *)malloc(sizeof( char *)*100);
memset(pOutputWord, 0, sizeof(pOutputWord)) ; char *s=minWorddelete(pInputStr, pOutputWord); cout
30.2简单的字符统计的,但是效率比较低
#include #include #include using namespace std; void main() {int a[128]; memset(a,0,sizeof(a)); string s=\"gddsgs\"; int n=s.size();
int i = 0;
for ( i = 0; i
int p=s[i]; a[p]+=1;} for (int j = 0;j0 )
cout
#include using namespace std; void cycleDisplacement(int *a, int n, int k) {
int temp=0;
if(k>0)
{while(k)
{temp=a[n-1];
for(int j=n-1;j>0;j--)//注意右移覆盖要从大的那边开始覆盖,即j--
{a[j]=a[j-1];
}
a[0]=temp;
k--;
}
}
else if(k
{temp=a[0];
for(int j=0;j
{a[j]=a[j+1];
}
a[n-1]=temp;
k++;
}
}
}
void main() { int a[]={1,2,3,4,5}; int n=5;
cycleDisplacement(a,5,-2);
for(int i=0;i
{
cout
}
}
32.求一个二维数组每列的最小值
#include using namespace std; void arraymin(int input[][4],int n1,int n2,int output[]) {
for(int i=0;i
{
output[i]=input[0][i];
for(int j=0;j
{
if(input[j][i]
output[i]=input[j][i];
}
} } void main() {
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; int n1=3; int n2=4; int *output=(int *)malloc(sizeof(int)*n2); arraymin(a,n1,n2,output); cout
for(int i=0; i
for(int j=0; j
{
cout
}
cout
cout
for(int k=0;k
cout
}
33.1两个字符串,求最长公共子串
#include using namespace std; #include char * maxcommonchar(char a[],char b[],char c[]) { int n1=strlen(a);
int n2=strlen(b); int n=(n1>=n2) ? n2:n1; //cout
int i=0,j=0,k=0,l=0;
for( i=n;i>=2;i--)//最大子串长度
for( j=0;j
for( k=0;k
{
if(strncmp(a+j,b+k,i)==0)
{
for(l=0;l
{
c[l]=a[j+l];
}
c[l]=\'\\0\';
// cout
return c;
}
}
}
void main() {
/*char *a=\"fdfdsfdsf\";
char *b=\"sdfdf\";*/
char a[100]; char b[100]; cin>>a; cin>>b;
char
output[100]={0}; char *s={0};
s=maxcommonchar(a,b,output); cout
}
从网上拷过来的程序,如下所示,他的复杂度比我的低。
#include #define M 100
//LCS问题就是求两个字符串最长公共子串的问题 char* LCS(char left[],char right[]) {
//获取左子串的长度,获取右子串的长度
int lenLeft=strlen(left),lenRight=strlen(right),k;
//注意这里要写成char型,而不是int型,否则输入整型数据时会产生错误。
//矩阵c纪录两串的匹配情况
//char *c=(char *)malloc(lenRight); char *p;
int c[M][M]={0};//当将c申明为一个二维数组时
int start,end,len,i,j;//start表明最长公共子串的起始点,end表明最长公共子串的终止点
end=len=0;//len表示最长公共子串的长度
for(i=0; i
{
//串从后向前比较,为什么要从后向前呢?是把一维数组c[ ]当二维数组来用,
//如果要从前向后,可以将c申明为一个二维数组c[M][M].但程序要做相应调整.
for(j=0;j
//for(j=lenRight-1; j>=0; j--)
{
if(left[i] == right[j])//元素相等时
{
if(i==0||j==0)
//c[j]=1;
c[i][j]=1; //这边是是因为若是i,j有一个为,说明是从某一个字符串的开头开始的,这个是公共字符串的起点
else
{
//c[j]=c[j-1]+1;
c[i][j]=c[i-1][j-1]+1; //只有前面的字符也是相等的,这边的计数器才会加一,
}
}
else
//c[j] = 0;
c[i][j]=0;
//if(c[j] > len)
if (c[i][j]>len)
{
//len=c[j];
len=c[i][j];
end=j;
}
}
}
start=end-len+1;
//数组p纪录最长公共子串
p =(char*)malloc(len+1);
for(i=start; i
{
p[i-start] = right[i];
}
p[len]=\'\\0\';
return p; }
void main() {
char str1[M],str2[M];
printf(\"请输入字符串:\");
gets(str1) ;
printf(\"请输入字符串:\");
gets(str2);
printf(\"最长子串为:\");
printf(\"%s\\n\",LCS(str1,str2));
}
33.2 n个字符串的最大公共子串
需要调用子函数的 #include using namespace std;
char * maxchar(const char * s[],char * p,int n) { int j=0,k=0,l=0,m=0;
for(j=strlen(s[0]);j>0;j--)//最大字符串的长度
for(k=0;k+j-1
{int flags1=1;
for(l=1;l
{int flags2=0;
for(m=0;m+j-1
{
if(strncmp(s[0]+k,s[l]+m,j)==0)
{//cout
flags2=1;
break;
}
}
if(!flags2)//如果循环到这个地方有某个字符串中没有s[0]中挑出的那个字符串,说明这个字符串不是最大的,将代表最大字符串的标志位设为
{//cout
flags1=0;
break;
}
}
if(flags1)
{// cout
strncpy(p,s[0]+k,j);
// p[j]=\'\\0\';
return p;
}
}
}
// void main() { const char *p1[]={\"fsdfsdf\",\"gsgfsd\",\"ryrghgjgfsd\"};
char *q=(char *)malloc(sizeof(char)*20); memset(q,0,sizeof(q));
cout
不需要调用子函数的
#include using namespace std; void main() {
const int n=3; char *s[]={\"fsdfsdf\",\"gsgfsd\",\"ryrghgjgfsd\"};
char *p=(char *)malloc(sizeof(char)*20); memset(p,0,sizeof(p)); int j=0,k=0,l=0,m=0; for(j=strlen(s[0]);j>0;j--)//最大字符串的长度
for(k=0;k+j-1
{int flags1=1;
for(l=1;l
{int flags2=0;
for(m=0;m+j-1
{
if(strncmp(s[0]+k,s[l]+m,j)==0)
{cout
flags2=1;
break;
}
}
if(!flags2)//如果循环到这个地方有某个字符串中没有s[0]中挑出的那个字符串,说明这个字符串不是最大的,将代表最大字符串的标志位设为
{//cout
flags1=0;
break;
}
}
if(flags1)
{// cout
strncpy(p,s[0]+k,j);
goto L;
}
}
L:cout
} 34.超大整数加法运算
大整数会用字符串或者数组来存,不过注意低位存字符前面几位,高位存后面,存到字符中应该存“”。这边我用的是数组 #include #include using namespace std; #define ln 100 //数字长度
void bigint(char a[],char b[]) {
int la = 0, lb = 0; int A[ln]={0}; int B[ln]={0};
la =strlen(a);
for (int i=0; i
{
A[i] = int(a[la-1-i])-48;
}
lb =strlen(b);
for (int i=0; i
{
B[i] = int(b[lb-1-i])-48;
}
int n=(la>lb)?la:lb;
for (int i=0; i
{
A[i]=A[i]+B[i];
A[i+1]=A[i+1]+A[i]/10;//将进位加到A对应位shang
A[i]=A[i]%10;//进位后原位只留下个位
}
cout
for (int j=n; j>=0; j--)
{
cout
}
cout
char a[ln];
cout
cin.getline(a, ln);
char b[ln];
cout
cin.getline(b, ln);
bigint(a,b);
} 35.排序总结
//交换排序1:冒泡法 #include using namespace std;
void BubbleSort(int a[],int length) { int temp=0;
for(int i=0;i
for(int j=0;j
{
if(a[j]>a[j+1])
{temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
} } //交换排序2: 鸡尾酒排序: 鸡尾酒排序,又被称作双向冒泡排序,是一种从冒泡算法演变成的稳定排序算法,不同于冒泡算法重复的从头到尾比较,鸡尾酒算法交替的从头到尾再从尾到头比较关键字。该算法的性能比标准的冒泡算法稍高。
注意:鸡尾酒算法可以看作是选择算法的一种变体。
void CocktailSort(int *array, int length) { int i; int temp;
int left = 0, right = length;
int finished;
do
{
finished = 1;
--right;
for (i = left; i
{
if (array[i] > array[i+1])
{
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
finished = 0;
}
}
if (finished)
{
return;
}
finished = 1;
for (i = right1);
_QuickSort(array, pivot_loc + 1, high); } }
void QuickSort(int *array, int length) { _QuickSort(array, 0, length1; j >= 0 && temp
{
a[j+1] = a[j];
}
a[j+1] = temp;//在合适位置安放a[i]
} }
//插入排序2: 二分法查找插入排序
如果比较操作的代价比交换操作大的话,可以采用二分查找法来减少比较操作的树目。该算法可以认为是插入排序的一个变种,称为二分查找排序。折半插入排序所需附加存储空间和直接插入排序相同,从时间上比较,折半插入排序仅减少了关键字间的比较次数,而记录的移动次数不变。
其实就是二分法查找与插入排序的一个结合,在已排好的字符串中用二分法查找出那个最合适的插入位置(找到的一般是比a[i]小的,即将离其最近的一个下标n),插入位置就是n+1 //void BinaryInsertionSort(int *array, int length) //{ // int i, j; // int temp; // int low, high, mid;