白書字符串?dāng)?shù)組

數(shù)字與字符串轉(zhuǎn)換

  • 使用itoa函數(shù),注意要指定轉(zhuǎn)換的進(jìn)制..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{   
    int number=12345;
    char string[7]={0};
    itoa(number,string,10);
    printf("integer=%d\n",number);
    printf("string=");
    for(int i=0;i<6;i++)
        printf("%c",string[i]);
    printf("\n");
    return 0;
}
  • 用sprintf,sscanf來寫入寫出..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{   
    int number=12345;
    int temp;
    char string[7]={0};
    sprintf(string,"%d",number);
    printf("string=");
    for(int i=0;i<6;i++)
        printf("%c",string[i]);
    printf("\n");

    sscanf(string,"%d",&temp);
    printf("integer= %d\n",temp);
    return 0;
}

1.蛇形填數(shù)

注意邊界條件的判斷

#include<stdio.h>
int a[100][100]={0};
int main()
{
     int n,tot,x,y;
     scanf("%d",&n);
     x=0;y=n-1;
     tot=a[x][y]=1;
     while(tot<n*n)
     {
         while(x+1<n && !a[x+1][y]) a[++x][y]=++tot;
         while(y-1>=0 && !a[x][y-1]) a[x][--y]=++tot;
         while(x-1>=0 && !a[x-1][y]) a[--x][y]=++tot;
         while(y+1<n && !a[x][y+1]) a[x][++y]=++tot;
     }
     for(x=0;x<n;x++)
     {
         for(y=0;y<n;y++)
             printf("%3d",a[x][y]);
         printf("\n");
     }
        return 0;
}

2.豎式乘法

查找數(shù)字是否出現(xiàn)過,可以用字符串操作strchr查找字符串中是否出現(xiàn)過字符c,即把數(shù)字轉(zhuǎn)換成字符去與輸入的字符串對比..

#include<stdio.h>
#include<string.h>
int main()
{
     int abc,de,x,y,z,ok,i,count=0;
     char s[100],buf[100]="";
     scanf("%s",s);
     for(abc=100;abc<=999;abc++)
         for(de=10;de<=99;de++)
         {
             x = abc*(de%10); y = abc*(de/10); z = abc*de;
             sprintf(buf,"%d%d%d%d%d",abc,de,x,y,z);
                                            //把十進(jìn)制的數(shù)字以char輸出到字符數(shù)組中
             ok=1;
             for(i=0;i<strlen(buf);i++)
                 if(strchr(s,buf[i])==NULL) ok=0;//s中首次出現(xiàn)字符buf[i]的位置
             if(ok)
             {
    printf("<%d>\n",++count);
    printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n",abc,de,x,y,z);
             }
         }
        printf("The number of solutions = %d\n",count);
        return 0;
}

3.最長的回文子串

遍歷尋找子串,從左起開始循環(huán)檢測

#include<stdio.h>
#include<string.h>
#include<ctype.h>
char buf[5000],s[5000];
int main()
{
    fgets(buf,sizeof(buf),stdin);
    int n,m=0,ok=1,max=0;
    int i,j,k;
    n=strlen(buf);
    for(i=0;i<n;i++)
        if(isalpha(buf[i])) s[m++]=toupper(buf[i]);
    for(i=0;i<m;i++)
        for(j=i;j<m;j++)
        {
            ok=1;
            for(k=i;k<=j;k++)
                if(s[k]!=s[j-(k-i)]) 
                    ok=0;
            if(ok && j-i+1>max) max=j-i+1;
        }
     printf("max = %d\n",max);
     return 0;
}

從中間向兩邊擴(kuò)展,從中間開始枚舉,奇數(shù)偶數(shù)有區(qū)別注意區(qū)分,總的復(fù)雜度是o(m^2),比遍歷要小,另外要原樣輸出必須存下字符的位置..
這里輸入用的是fgets也可以用gets..

#include<stdio.h>
#include<string.h>
#include<ctype.h>
char buf[5000],s[5000];
int main()
{
    fgets(buf,sizeof(buf),stdin);
    int n,m=0,ok=1,max=0;
    int i,j,k,x,y,p[100]={0};
    n=strlen(buf);
    for(i=0;i<n;i++)
        if(isalpha(buf[i])) {p[m]=i;s[m++]=toupper(buf[i]);}
    for(i=0;i<m;i++)
    {
        for(j=0;i-j>=0 && i+j<m;j++)
        {
            if(s[i-j]!=s[i+j]) break;
            if(j*2+1>max) {max=2*j+1;x=p[i-j];y=p[i+j];}
        }
        for(j=0;i-j>=0 && i+j+1<m;j++)
        {
            if(s[i-j]!=s[i+j+1]) break;
            if(j*2+2>max) {max=2*j+2;x=p[i-j];y=p[i+j+1];}
        }
    }
    for(i=x;i<=y;i++)
        printf("%c",buf[i]);
    printf("\n");
     return 0;
}

4.統(tǒng)計(jì)單詞的長度

輸入一個gets的字符串,提取其中的字符

#include<stdio.h>
#include<string.h>
#include<ctype.h>
const int MAX=189819;

int main()
{
     int count=1,sumLength=0;
     char word[MAX];
     gets(word);
     int preIndex=0;
     for(int i=0;i<strlen(word);i++)
     {
         if(word[i]==' ') 
         {
             if(preIndex==0)
                 sumLength+= i;
             count++;
             sumLength += i-preIndex;
             preIndex=i+1;
         }
     }

     if(count ==0)
         printf("No Input");
     else
         printf("%lf\n",(double)sumLength/count);

     return 0;
}

5.計(jì)算器

從輸入的一行字符串中提取數(shù)字,根據(jù)操作符來分隔左操作數(shù)和右操作數(shù)..先要轉(zhuǎn)存到字符數(shù)組中,再換算成數(shù)字..

#include<stdio.h>
#include<string.h>
#include<ctype.h>
const int MAX=189819;

int main()
{
     int x,y,i,power;
     char str[MAX];
     char strX[4]={0},strY[4]={0};
     char opr;
     gets(str);
     
     int p=0;
     for(i=0;str[i]!='+' && str[i]!='-' && str[i]!='*' && str[i]!='/';i++)
         if(str[i]>='0' && str[i]<='9')
             strX[p++]=str[i];
     strX[p]='\0';
     //printf("%s",strX);

     opr=str[i];
     //printf("%c",opr);
     p=0;
     while(i<strlen(str))
     {
         if(str[i]>='0' && str[i]<='9')
             strY[p++]=str[i];
         i++;
     }
     strY[p]='\0';
     //printf("%s",strY);
     x=0;power=1;
     for(i=strlen(strX)-1;i>=0;i--)
     {
         x += (strX[i]-'0')*power;
         power *= 10;
     }

     y=0;power=1;
     for(i=strlen(strY)-1;i>=0;i--)
     {
         x += (strY[i]-'0')*power;
         power *= 10;
     }

     if(opr == '+') printf("%d\n",x+y);
     if(opr == '-') printf("%d\n",x-y);
     if(opr == '*') printf("%d\n",x*y);

     return 0;
}

6.進(jìn)制轉(zhuǎn)換

十進(jìn)制轉(zhuǎn)換成其他進(jìn)制,就一直求余就行了,如果忘了,推一次二進(jìn)制除法就可以寫出來..

#include<stdio.h>
#include<string.h>
#include<ctype.h>
const int MAX=189819;

int main()
{    
  int n,b;
  int ans[MAX]={0};
  scanf("%d%d",&n,&b);
  int i=0;
  while(n)
  {
      ans[i++]=n%b;
      n = n/b;
  }
  for(i=i-1;i>=0;i--)
      printf("%d",ans[i]);
  printf("\n");

  return 0;
}

7.字符串轉(zhuǎn)換

輸入的字符串錯位,可以用單個輸入getchar來做。首先要輸入的c在常量字符串當(dāng)中的位置,其次要考慮空格的輸出。

#include <stdio.h>
#include <math.h>
char *s="`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./";

int main()
{
    int i,c;
    while((c=getchar())!=EOF)
    {
        for(i=1;s[i]&&s[i]!=c;i++);
        if(s[i]) putchar(s[i-1]);
        else putchar(c);
    }
    return 0;
}

8.周期串的檢驗(yàn)

#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
    char word[100];
    scanf("%s",word);
    int len = strlen(word);
    for(int i=1;i<=len;i++)
        if(len%i==0)
        { 
            int ok=1;
            for(int j=i;j<=len;j++)
                if(word[j]!=word[j%i]) {ok=0;break;}
            if(ok) {printf("%d\n",i);break;}
          
        }
    return 0;
}

華為的一道題

#include <stdio.h>
#include <stdlib.h>
int a[10];

void swap(int &a,int &b)
{
    if(a!=b)
    {
        a ^=b;
        b ^=a;
        a ^=b;
    }

}

void insert_sort(int a[],int n)
{
    for(int i=1;i<n;i++)
        for(int j=i-1;j>=0 && a[j]>a[j+1];j--)
            swap(a[j],a[j+1]);
}

int remove_duplicate(int a[],int n)
{
    int index=0;
    for(int i=1;i<n;i++)
        if(a[index]!=a[i])
            a[++index]=a[i];
    return index+1;
}



int main()
{
    int i,n;
    for(i=0;i<10;i++)
        scanf("%d",&a[i]);
    insert_sort(a,10);

    //for(i=0;i<10;i++)
        //printf("%d ",a[i]);
    n=remove_duplicate(a,10);

   // for(i=0;i<n;i++)
     // printf("%d ",a[i]);
    for(i=n-1;i>=n-5;i--)
        printf("%d",a[i]);

    printf("\n"); 
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評論 19 139
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,679評論 0 4
  • 今天去看了幾套小房子,都是一室一廳的,有裝修過就沒有住過人的,也有住過人的,但現(xiàn)在一直空置的,朝北朝東各一套,相對...
    牌牌閱讀 352評論 0 0
  • 我是誰 我是貝殼叔叔,80后無房無車無存款無工作, 大學(xué)肄業(yè) ,來自廣西現(xiàn)居杭州。 我要做什么? 今天是2017年...
    貝殼叔叔0閱讀 368評論 0 1
  • 1.本周為了練好口才,我的行動和成果有哪些? 1)本周學(xué)習(xí)群里小伙伴“前端”的方法,也試著不寫稿子錄制了《喝雞湯吃...
    董姝閱讀 281評論 0 0

友情鏈接更多精彩內(nèi)容