代码总结 2

1、 蛇形 输出

#include<iostream>
using namespace std;
void getresult( int n){
    int p[1000][1000];
    int count=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<=i;j++)
            p[i-j][j]=++count;
    }
       for(int i=0;i<n;i++){
        for(int j=0;j<n-i-1;j++)
           cout<<p[i][j]<<' ';
           cout<<p[i][n-i-1]<<endl;
            
       }
    return ;
}
int main(){
    int n;
    while(cin>>n){
        getresult(n);
    }
    return 0;
}


2、    abc  123456789  变成abc00000   12345678 90000000
#include<stdio.h>
#include<string.h>
void print(char*str1,char* str2)
{
    int len1=strlen(str1);
    int len2=strlen(str2);
    int i,count=0;
    int a=len1/8;
    int b=len1%8;
    int c=len2/8;
    int d=len2%8;
    if(a>0)
    {
      for(i=0;i<a*8;i++)
      {
        printf("%c",str1[i]);
        count++;
        if(count==8)
          {
            printf("\n");
            count=0;
        }
      }
    }
    for(i=a*8;i<8+a*8;i++)
    {
      if(i<b+a*8)
          printf("%c",str1[i]);
      else
          printf("%c",'0');

    }
    printf("\n");
    count=0;
       if(c>0)
    {
      for(i=0;i<c*8;i++)
      {

        printf("%c",str2[i]);
        count++;
        if(count==8)
          {
            printf("\n");
            count=0;
          }
      }
    }
    for(i=c*8;i<8+c*8;i++)
    {
      if(i<d+c*8)
          printf("%c",str2[i]);
      else
          printf("%c",'0');

    }


}


int main()
{
  char str1[100];
  char str2[100];
  gets(str1);
  gets(str2);
  print(str1,str2);


return 0;
}


3、求最长子串

#include<stdlib.h>
#include <string.h>
#include <iostream>
 using namespace std;


 const int MAXN = 1000;

 int mycmp(const void* p1, const void* p2)
 {
     return strcmp(*(char* const*)p1, *(char* const*)p2);
 }

 int getLen(char* p, char* q)
 {
     int ret = 0;
     while (*p && *p++ == *q++)
     {
         ++ret;
     }
     return ret;
 }

 char* foo(char result[], char s[])
 {
     int len = strlen(s);
     char** suffix = new char*[len];
     for (int i = 0; i < len; ++i)
     {
         suffix[i] = s + i;
     }
     qsort(suffix, len, sizeof (char*), mycmp);
     //for (int i = 0; i < len; ++i)
     //{
     //    cout << suffix[i] << endl;
     //}
     int maxlen = 0, maxi = 0, maxj = 0, temp = 0;
     for (int i = 0; i < len - 1; ++i)
     {
         temp = getLen(suffix[i], suffix[i + 1]);
         if (temp > maxlen)
         {
             maxlen = temp;
             maxi = i;
             maxj = i + 1;
         }
     }
     //cout << maxlen << endl;
     //cout << suffix[maxi] << endl;
     //cout << suffix[maxj] << endl;
     //printf("%.*s\n", maxlen, suffix[maxi]);
     for (int i = 0; i < maxlen; ++i)
     {
         result[i] = suffix[maxi][i];
     }
     result[maxlen] = '\0';
     return result;
 }

 int main()
 {
     char s[MAXN];
     char result[MAXN];
     while (cin >> s)
     {
         cout << foo(result, s) << endl;
     }
     return 0;
}

4、通过亦或求不同数字

int main(void)
{


    int a[]={1,2,8,2,1,10,5,5,6,6,7,7};//           通过^求不同元素
    int len=sizeof(a)/sizeof(int);
    int temp=0,temp1=0,b=1,i,j;
    for(i=0;i<len;i++)
    {
        temp^=a[i];

    }

    printf("%d\n",temp);

    for(j=0;j<8;j++)
    {
        if(temp&(b<<j))
            break;

    }
    b=1;
    temp=0;
    for(i=0;i<len;i++)
    {
        if(a[i]&(b<<j))
         temp^=a[i];
        else
           temp1^=a[i];

    }


    printf("%d %d\n",temp,temp1);

    return 0;
}


最大子数列和

5、、、

#include <iostream>

using namespace std;


int Max(int a[],int N)
{
  int max=0,sum=0;
  int i;
  for(i=0;i<N;i++)
  {
      sum+=a[i];
      if(sum>max)
          max=sum;
      else if(sum<0)                           //   最大子数列和
         sum=0;
  }

return max;
}


int main()
{
    int a[10]={-1,5,-5,6,8,-3,7,-2,-5,2};

    cout << Max(a,10) <<"\n"<< endl;
    return 0;
}

6、空格替换为 ‘20’

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

void tran(char*p);
int main()
{

    char a[20]="he ni hao";
    char*p=a;

    int lenth=strlen(a);
    int count=0;
    while(*p!='\0')
    {
        if(*p==' ')

            count++;

        p++;

    }


    count=lenth+2*count;

    char *b=(char*)malloc(count+1);

    while(lenth>=0)                                 //空格 替换为%20
    {
        if(a[lenth]!=' ')

          b[count--]=a[lenth--] ;
        else
        {
            lenth--;                                ///
            b[count--]='0';
            b[count--]='2';
            b[count--]='%';
        }

    }


    printf("%s\n",b);


      chan(a);
       printf("%s\n",a);
}

7、汉诺塔  

#include<stdio.h>
int count=0;
void hannuota(int n,char x,char y,char z);
int main()
{
    int n;
  printf("yigong cishu \n");
  scanf("%d",&n);
  hannuota(n,'x','y','z');
  printf("yidongdecishu  %d\n",count);

}

void hannuota(int n,char x,char y,char z)                       //汉诺塔
{
    if(1==n)
    {
        printf("%c->%c\n",x,z);
        count++;
    }
    else
    {
        hannuota(n-1,x,z,y);
        printf("%c->%c\n",x,z);
        count++;
        hannuota(n-1,y,x,z);
    }
}


8



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值