常见算法

xiaoxiao2021-02-28  53

用来统计每个ascii字符的出现次数,最后给出出现数值

 

void histogram(char *src) {         int i;         int hist[256];         for(i=0;i<256;i++)         {                 hist[i] = 0;         }         while(*src != '\0')         {                 hist[*src++]++;          }         for(i=0;i<=255;i++)         {                 if(hist[i] != 0)                 printf(" %c  occur times %d\n",i,hist[i]);         } }

//螺旋队列问题

int foo(int x,int y) {         #define max(a,b) ((a) < (b) ? (b):(a))         #define abs(a) ((a)>0?(a):-(a))

        int t = max(abs(x),abs(y));         int u = t + t;         int v = u - 1;         v = v*v + u;         if(x == -t)                 v += u + t - y;         else if(y == -t)                 v += 3*u + x - t ;         else if( y == t )                 v += t - x;         else                 v += y -t ;         return v; }

void test42() {

        int a;         scanf("%d",&a);         int x,y;         for(y=-a;y<=a;y++)         {                 for(x = -a;x<=a;x++)                         printf("] ",foo(x,y));                 printf("\n");         } }

 

//求素数

int judge(int a) {         int j;         for(j=2;j<=sqrt(a);j++)         {                 if(a % j == 0)                 return 1;         }         return 0; }

void test43() {         int i;         for(i=2;i<100;i++)         {                 if(judge(i) == 0)                 printf("%d ",i);                 if(i % 10 == 0)                 printf("\n");         }         printf("\n");

        //return 0; }

 

 

转载请注明原文地址: https://www.6miu.com/read-46668.html

最新回复(0)