zoukankan      html  css  js  c++  java
  • 算法题

    1.

    int countSegments(char * s){
        int i,sum,flag;
        i=sum=flag=0;
        for(i=0;s[i]!='';i++){
            if(s[i]==' ')flag=0;
            else if(flag==0){
                sum++;
                flag=1;
            }
        }
        return sum;
    }

    特别注意这里对于字符的判断应该为单引号''

    2.

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    
    
    struct ListNode* deleteDuplicates(struct ListNode* head){
    struct ListNode* p=head;
    while(p!=NULL&&p->next!=NULL){//需要也判断p,这样才能保证p->next不为野指针
        if(p->val==p->next->val){
            p->next=p->next->next;
        }
        else
        {
            p=p->next;
        }
    }
    return head;
    }

     3.

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    
    
    int maxDepth(struct TreeNode* root){
     
    while(root!=NULL){
      int left=maxDepth(root->left);
      int right=maxDepth(root->right);
      return left>right?left+1:right+1;
    }
    return 0;
    }

     4.

    int reverse(int x){
    long count=0;
    while(x!=0){
        count=count*10+x%10;
        
        x=x/10;
    }
    if(count>2147483647||count<-2147483648)
    return 0;
    else
    return count;
    
    }

     5.水仙花数

    #include <stdio.h>
    #define MAXS 30
    #include<string.h>
    
    int main()
    {
        int n;
        int x=0;
        int  y=0;
        int z=0;
        scanf("%d",&n);
        if(n<100||n>999){
            printf("ERROE");
        }
        else{
        int tmp=n;
        x=n%10;
        n=n/10;
        y=n%10;
        n=n/10;
        z=n%10;
        if(tmp==(int)(pow(x,3)+pow(y,3)+pow(z,3))){
            printf("YES");
        }
        else{
            printf("NO");
        }
        }
            
    
        return 0;
    }

     6.杨辉三角

    #include <stdio.h>
    #include <stdlib.h>
    /*
    1
    1 1
    1 2 1
    1 3 3 1
    
    */
    int main()
    {
      int a[10][10];
      int i=0;
      int j=0;
      //第一列和对角线 
      for(i=0;i<10;i++){
          a[i][i]=1;
          a[i][0]=1;
      }
      //中间部分(从第三行开始 i=2) 
      for(i=2;i<10;i++){
          for(j=1;j<i;j++){
          a[i][j]=a[i-1][j]+a[i-1][j-1]; 
          }
          
      }
      //打印 
       for(i=0;i<10;i++){
          for(j=0;j<=i;j++){
          printf("%d ",a[i][j]); 
          }
          printf("
    ");
          
      }
    }

     7.树的遍历:前中后,知二求一

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
     
    typedef struct node
    {
        char data;
        struct node *l,*r;
    } tree;
    tree * creat()
    {
        tree *t;
        t=(tree *)malloc(sizeof(tree));
        t->l=NULL;
        t->r=NULL;
        return t;
    }
    tree *creatree(int n, char *a, char *b)
    {
        int i;
        if(n==0)
            return NULL;
        tree *t;
        t=creat();
        t->data=a[0];
        for(i=0; i<n; i++)
        {
            if(a[0]==b[i])
                break;
        }
        printf("i=%d a+1=%c ,b=%c ",i,*(a+1),*b);
        printf("n-1-i=%d a+1+i=%c ,b+1+i=%c
    ",n-1-i,*(a+1+i),*(b+1+i));
        t->l=creatree(i,a+1,b);
        t->r=creatree(n-1-i,a+1+i,b+1+i);
        return t;
    }
    void back_show(tree *t)
    {
        if(t)
        {
            back_show(t->l);
            back_show(t->r);
            printf("%c",t->data);
        }
    }
    int main()
    {
        tree *t;
        int n;
        char a[55],b[55];
        scanf("%s %s",a,b);
        n=strlen(a);
        t=creatree(n,a,b);
        back_show(t);
        printf("
    ");
        return 0;
    }

     汉诺塔:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
     
    void hanio(int n,char a,char b,char c){
        if(n==1){
            printf("%c->%c
    ",a,c);
        }
        else{
            hanio(n-1,a,c,b);
            printf("%c->%c
    ",a,c);    
            hanio(n-1,b,a,c);
        }
    }
    
    
    void main(){
        hanio(3,'A','B','C');
        
    } 
  • 相关阅读:
    字符数组+数组复习
    C语言博客作业05-指针
    C语言博客作业04 数组
    C语言博客作业03 函数
    Java与C# socket通信
    JDBC复制数据库(sqlite)
    mysql Connector/net不能更新或删除(转载)
    MATLAB回归、插值、逼近、拟合【转载】
    前端请求RestController发送数据方法汇总
    elementUI el-input 输入框 设置高度和宽度
  • 原文地址:https://www.cnblogs.com/miaobo/p/12742714.html
Copyright © 2011-2022 走看看