Nvidia 面试题之一
大概的题目是这样的:
实现一个函数 void * storenum(struct node* list,int n , int m);
作用于分式值小于1的除法. eg: 1/4 = 0.25 这里 m = 4, n = 1 把输出的结果储存到list链表里面
/****************************************
Code writer : EOF
code date : 2014.10.11
e-mail : jasonleaster@gmail.com
If you find bugs in my code,
please touch me, thank you : )
****************************************/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node* next;
};
int storenum(struct node* list,int m,int n);
int main()
{
struct node header;
storenum(&header,1,16);
struct node *p_node = &header;
/*
** Print out answer.
*/
for(;(p_node->next);)
{
printf("%d ",p_node->num);
p_node = p_node->next;
}
/*
** Free List we allocated.
*/
struct node *del_node = NULL;
for(p_node = header.next;(header.next);)
{
del_node = header.next;
header.next = del_node->next;
free(del_node);
}
return 0;
}
int storenum(struct node* list,int m,int n)
{
if(!list)
{
return 0;
}
int factor = 10;
if(m >= n)
{
printf("Hey guys read again the rule of this game.!\n");
}
else
{
list->num = 0; //0.***
list->next = NULL;
for(;m%n != 0;)
{
list->next = (struct node*)malloc(sizeof(struct node));
list->next->next = NULL;
list = list->next;
list->num = m*factor/n;
m = (m * factor)%n;
}
list->next = (struct node*)malloc(sizeof(struct node));
list->next->next = NULL;
list = list->next;
list->num = m*factor/n;
m = (m * factor)%n;
}
return 0;
}