zoukankan      html  css  js  c++  java
  • [置顶] 从二级指针看华为和迅雷两道小题

    原题:http://blog.csdn.net/v_july_v/article/details/11921021

    1. 华为

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*
     * HuaWei: A string compressed example
     * */
    void stringZip(const char *pInputStr, char *pOutputStr) {
        int len = strlen(pInputStr);
        char *tmp = (char *)malloc(sizeof(len * sizeof(char)));
        int start = 0;
        int end = start + 1;
        int cnt = 0;
        int i = 0;
    
        while(pInputStr[start]) {
    	if(pInputStr[start] == pInputStr[end]) {
    	    cnt++;
    	    end++;
    	}
    	else {
    	    if(cnt == 0)  tmp[i++] = pInputStr[start];
    	    else if(cnt > 0) {
    		tmp[i++] = cnt + 1 + '0';
    		tmp[i++] = pInputStr[start];
    		cnt = 0;
    	    }
    	    start = end;
    	    end = start + 1;
    	}
        }
        tmp[i] = '';
        strncpy(pOutputStr, tmp, strlen(tmp));
        free(tmp);
    }
    void main() {  
        char str[1024];  
        char compress_str[1024];
        printf("Input a string:");
        gets(str);  
        stringZip(str, compress_str);
        printf("The compressed string is:");
        puts(compress_str);
    }  
    


    2. 迅雷

    #include <stdio.h>
    #include <stdlib.h>
    /*
     * XunLei: difference of linked list
     * */
    typedef struct node {
        int elem;
        struct node *next;
    }node;
    
    void create(node **head, int n) {
        int elem;
        int i;
        node *p;
        node *r;
        for(i = 0;i < n;i++) {
    	if(scanf("%d", &elem) != 1) {
    	    perror("input error!
    ");
    	    exit(1);
    	}
    	p = (node *)malloc(sizeof(node *));
    	if(!p) {
    	    perror("memory error!
    ");
    	    exit(2);
    	}
    	p->elem = elem;
    	p->next = 0;
    	if(!*head) *head = p;
    	else {
    	    r->next = p;
    	}
    	r = p;
        }
    }
    void show(node *head) {
        node *p = head;
        while(p) {
    	printf("%d
    ", p->elem);
    	p = p->next;
        }
    }
    void difference(node **LA, node *LB) {
        node *p, *q, *pre;
        node *head;
        p = *LA;
        pre = *LA;
        head = p;
        while(p) {
    	q = LB;
    	while(q) {
    	    if(p->elem == q->elem) break;
    	    q = q->next;
    	}
    	if(q) {
    	    if(pre == p) {
    		p = p->next;
    		pre->next = 0;
    		free(pre);
    		pre = p;
    		head = p;
    	    }
    	    else {
    		pre->next = p->next;
    		p->next = 0;
    		free(p);
    		p = pre->next;
    	    }
    	}
    	else {
    	    pre = p;
    	    p = p->next;
    	}
        }
        *LA = head;
    }
    void main() {
        node *head1 = NULL, *head2 = NULL;
        create(&head1, 5);
        printf("----------
    ");
        create(&head2, 3);
        difference(&head1, head2);
        printf("----------
    ");
        show(head1);
    }
    


  • 相关阅读:
    STM32CubeMX 使用
    Zookeeper集群搭建
    golang zookeeper监听事件报错
    git push 报错
    springboot使用postgresql模式下数据库表找不到
    不要在循环中访问数据库,这样会严重影响数据库性能
    SQL查询效率(Oracle)
    游标 数据集 效率比较
    oracle 视图
    INDEX SKIP SCAN 和 INDEX RANGE SCAN以及索引会失效
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3343497.html
Copyright © 2011-2022 走看看