• [置顶] 从二级指针看华为和迅雷两道小题


    原题: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);
    }
    


  • 相关阅读:
    流水线操作verilog
    16x16移位相加乘法器verilog实现
    Nios II对flash进行读写(DE2)
    initial使用的要点
    边沿检测电路设计verilog
    DDoS攻防战 (四):CC攻击防御系统部署
    DDoS攻防战(三):ip黑白名单防火墙frdev的原理与实现
    DDoS攻防战 (二) :CC攻击工具实现与防御理论
    DDoS攻防战 (一) : 概述
    IP流量重放与pcap文件格式解析
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3343497.html
Copyright © 2020-2023  润新知