题目标号:A-1002
这个题目在最后提交的时候总是最后几个测试点过不了,有了前一次的经验,猜到肯定是特殊情况没有考虑进去。最后排查到,是当系数为0时,那个阶数也打印出来了。调整了一下,再最后获取总的个数时,浪费了点时间把它的非零值装入了一个链表里了。
题目:
菜鸟代码:
#include "stdio.h"
#include "stdlib.h"
typedef struct Node
{
int N;
float an;
struct Node *node;
}Num ;
int insertNum(Num *head , int N , float an )
{
Num * node ;
Num * hnode = head ;
if(head->N == -1)
{
hnode->an = an ;
hnode->N = N ;
return 1 ;
}
while(hnode->node != NULL)
{
if( hnode->N == N )
{
hnode->an = hnode->an + an ;
return 0;
}
hnode = hnode->node ;
}
if( hnode->N == N )
{
hnode->an = hnode->an + an ;
return 0;
}
node = (Num*)malloc(sizeof(Num)) ;
node->an = an;
node->N = N;
node->node = NULL ;
hnode = head;
while(hnode->node != NULL)
{
if( node->N > hnode->N )
{
Num *temp = (Num*)malloc(sizeof(Num)) ;
temp->N = hnode->N ;
temp->an = hnode->an;
hnode->N = node->N ;
hnode->an = node->an;
node->N = temp->N ;
node->an = temp->an ;
}
hnode = hnode->node ;
}
if( node->N > hnode->N )
{
Num temp = *hnode;
*hnode = *node ;
*node = temp ;
}
hnode->node = node;
return 0;
}
Num * getCount(Num *n)
{
int count = 0;
Num *index = n;
Num *store = (Num *)malloc(sizeof(Num));
Num *search = store ;
store->node = NULL ;
while( index != NULL)
{
if(index->an != 0)
{
Num *node = (Num *)malloc(sizeof(Num));
count++;
node->an = index->an;
node->N = index->N;
node->node = NULL ;
store->node = node ;
store= node ;
}
index = index->node ;
}
search->N = count ;
return search;
}
void showNum(Num * n)
{
Num * data = getCount(n) ;
if(data->N == 0)
{
printf("0\n");
return;
}else
{
printf("%d " ,data->N );
data = data->node;
}
while(data->node != NULL)
{
printf("%d %.1f " , data->N ,data->an);
data = data->node;
}
printf("%d %.1f\n" , data->N ,data->an);
}
Num * initNum(Num * n)
{
n->an = 0;
n->N = -1;
n->node = NULL;
return n;
}
int main(void)
{
int count = 2;
Num *dnum ;
Num *head = (Num*)malloc(sizeof(Num));
head = initNum(head);
dnum = head;
while(count--)
{
int num ;
scanf("%d" , &num);
while(num--)
{
int temp ;
float data = 0.0;
scanf("%d %f" , &temp , &data);
temp = insertNum( head , temp , data ) ;
}
}
showNum(head);
return 0;
}