zoukankan      html  css  js  c++  java
  • 2012武汉华为机试题

    题目:有一个数组a[N]如a[10]={0,1,2,3,4,5,6,7,8,9}每隔两个数删除一个数,如0,1,2(删除),3,4,5(删除),6,7,8(删除),9,

    到数组尾部回到数组头部继续删除,要求编写一个函数实现实现上述操作,返回最后一个数的数组下标。

    函数接口:int getLast(int iLen)

    参数:数组初始元素个数iLen

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node *List;
    typedef struct node *PNode;
    
    typedef struct node
    {
    	int data;
    	struct node *next;
    }Node;
    
    int getLast(int iLen)
    {
    	int i;
    	List L;
    	PNode tempNode,current;
    	L = (List)malloc(sizeof(Node));
    	L->next = NULL;
    	current = L;
    	for (i=0; i<iLen; i++)
    	{
    		tempNode = (PNode)malloc(sizeof(Node));
    		tempNode->data = i;
    		current->next = tempNode;
    		current = tempNode;
    	}
    	current->next = L->next;
    	current = L;
    	while (iLen > 1)
    	{
    		current = current->next->next;
    		tempNode = current->next;
    		current->next = tempNode->next;
    		printf("%d\n",tempNode->data);
    		free(tempNode);
    		iLen--;
    	}
    	return current->data;
    }
    
    int main()
    {
    	printf("last of 20 is %d",getLast(20));
    	return 0;
    }
  • 相关阅读:
    UIScrollView(滚动视图)
    NSJSONSerialization(json序列化)
    手势(UIGestureRecognizer)
    mac常用命令
    ios设备相关
    UITextField
    cocos2d学习笔记
    NSTimer(定时器)
    git命令
    Java 线程的终止-interrupt
  • 原文地址:https://www.cnblogs.com/zechen11/p/2180700.html
Copyright © 2011-2022 走看看