要面试了,写写小程序练练手,顺便记录一下

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; i<MAX_A; i++){
          for(j=0;j<MAX_B;j++){
                    tmp= b[MAX_B-j-1] * a[MAX_A-i-1] + c[MAX_A+MAX_B-1 -i-j]; /*current multiply*/
                    if(tmp>9)    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<MAX_A+MAX_B;i++)
          printf("%d",c[i]);                /*output result*/
}

 

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<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 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<len){
       if(str[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<stdio.h>
#include<stdlib.h>
#include<string.h>
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<j){
        while(array[j]>=key&&i<j)
               j--;
        if(i<j){
            swap(array+j,array+mid_pos);
            mid_pos=j;
        }else break;
        while(array[i]<=key&&i<j)
              i++;
        if(i<j){
            swap(array+i,array+mid_pos);
            mid_pos=i;
        }else break;
   }
   quick_once(array,head,mid_pos-1);
   quick_once(array,mid_pos+1,tail);
}
void main()
{
   int i,a[]={1,2,3,4,5,6,7,8,9};
   quick_once(a,0,sizeof(a)/sizeof(int)-1);
   for(i=0;i<sizeof(a)/sizeof(int);i++) printf("%d ",a[i]);
}


 

 

 

你可能感兴趣的:(c,String,面试,BI,UP,output)