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);
    }
    


  • 相关阅读:
    uva 10369 Arctic Network
    uvalive 5834 Genghis Khan The Conqueror
    uvalive 4848 Tour Belt
    uvalive 4960 Sensor Network
    codeforces 798c Mike And Gcd Problem
    codeforces 796c Bank Hacking
    codeforces 768c Jon Snow And His Favourite Number
    hdu 1114 Piggy-Bank
    poj 1276 Cash Machine
    bzoj 2423 最长公共子序列
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3343497.html
Copyright © 2011-2022 走看看