1. 大数相乘
int i=0,j=0,tmp=0;
#define MAX_A 12
#define MAX_B 10
void main()
{
int i=0,j=0,tmp=0;
int a[MAX_A]={1,2,3,4,5,6,7,8,9,1,2,3};
int b[MAX_B]={1,2,1,2,1,2,1,2,1,2};
int c[MAX_A+MAX_B]={0};
for(i=0; i9) c[MAX_A+MAX_B-1 -i-j-1] += tmp / 10; /*high bit*/
c[MAX_A+MAX_B -1 -i -j] = tmp %10; /*low bit*/
}
}
for(i=0;i
2. 求字符串中出现最多的字符和个数
void main()
{
int total[256],max;
char *p="abcdabcdaaaabcbbbbbbbbbbbdefg";
char *ps=p,max_char;
while(*ps!='\0'){
total[*ps]++;
if(total[*ps]>max){
max = total[*ps];
max_char = *ps;
}
ps++;
}
printf("max char=%c, number=%d\n",max_char,max);
}
3. 字符串逆转
void main()
{
char p[]="abcdefg";
int t=0,len=strlen(p);
while(1){
if(t==len/2) break;
p[t]^=p[len-1-t];
p[len-1 -t]^= p[t];
p[t] ^=p[len-1-t];
t++;
}
printf("reverse string=%s\n",p);
}
4.字符串首字母大写,后面字母小写
#include
#include
#include
up_lower(char str[])
{
int i=0,len= strlen(str);
if(str[i]>='a'&&str[i]<='z')
str[i] = str[i]-'a'+'A';
i++;
while(i='A'&&str[i]<='Z')
str[i] = str[i]+'a'-'A';
i++;
}
}
void main()
{
char a[]="aDSKLFJdfklsdjf";
up_lower(a);
printf("string=%s\n",a);
}
5. 快速排序
#include
#include
#include
void swap(int *a,int *b)
{
int tmp=*a;
*a = *b;
*b = tmp;
}
void quick_once(int array[],int head,int tail)
{
int key,i,j,mid_pos;
key = array[head];
i = mid_pos = head;
j = tail;
if(head>=tail) return;
while(i=key&&i