Contest 7 Big vs Big (2)

Problem A: Big vs Big (2)

Time Limit: 3 Sec   Memory Limit: 128 MB
Submit: 332   Solved: 183
[ Submit][ Status][ Web Board]

Description

Calculate the addtion of any two positive big integers.

Requirements:
Test data  can be more than 64 digits, therefore you MUST use  a linked list to store an integer (any big).

 A sample Definition for singly-linked list.
// Singly-linked list node

  

typedef int element_type;

typedef struct STRUCTNODE* node_ptr;

  

typedef struct STRUCTNODE

{

  element_type element;

  struct STRUCTNODE* next;

}node;

  

  

//typedef struct node* node_ptr;

//typedef node* Link;

/** \brief Linked list implementation

 *

 *

 */

typedef  struct LINKLIST{

node_ptr  head; // Point to list header

node_ptr tail; // Pointer to last

node_ptr fence;// Last element on left

int leftcnt;      // Size of left

int rightcnt;     // Size of right

}*LList ;

Input

 The first line contains the number of test cases,  N.
  In the next 2*N lines, each line contains a string of number. 

Output

Out put N lines. Each line represent the sum of A and B. 

Sample Input

2
1111
2222
99999
1

Sample Output

3333
100000

HINT

#include<stdio.h>
#include<stdlib.h>
typedef int element_type;
typedef struct STRUCTNODE* node_ptr;
typedef struct STRUCTNODE{
    element_type element;
    struct STRUCTNODE* next;
    }node;
typedef struct LINKLIST{
    node_ptr head; // Point to list header
    node_ptr tail; // Pointer to last
    node_ptr fence;// Last element on left
    int leftcnt; // Size of left
    int rightcnt; // Size of right
    }*LList;

void init(LList listLink){ // Intialization
    node_ptr headNode = (node_ptr)malloc(sizeof(node));
    headNode->element = 0;
    headNode->next = NULL;
    listLink->head = headNode;
    listLink->fence = listLink->tail = listLink->head;
    listLink->leftcnt = listLink-> rightcnt = 0;
    }
void removeall(LList listLink) {//free store
    while(listLink->head != NULL) {
        listLink->fence = listLink->head;
        listLink->head = listLink->head->next;
        free(listLink->fence);
        }
        }

void clear(LList listLink) {
    removeall(listLink);
    init(listLink); }

void setStart(LList listLink) {
listLink->fence = listLink-> head;
listLink->rightcnt += listLink->leftcnt;
listLink->leftcnt = 0;
}

void setEnd(LList listLink) {
listLink->fence = listLink->tail;
listLink->leftcnt += listLink->rightcnt;
listLink->rightcnt = 0; }

int leftLength(LList listLink){
    return listLink->leftcnt;
    }
int rightLength(LList listLink){
    return listLink-> rightcnt;
    }

void insert(LList listLink, element_type item)
{
   node_ptr tmpNode = (node_ptr)malloc(sizeof(node));
   tmpNode->element = item;
   tmpNode->next = listLink->fence->next;
   listLink->fence->next = tmpNode;
   if (listLink->tail == listLink->fence)
     listLink->tail = listLink->fence->next;
     listLink->rightcnt++;
}

void append(LList listLink, element_type item) {
    node_ptr tmpNode = (node_ptr)malloc(sizeof(node));
    tmpNode->element = item;
    tmpNode->next = NULL;
    listLink->tail->next = tmpNode;
    listLink->tail = listLink->tail->next;
    listLink->rightcnt++;
}
void prev(LList listLink) {
    node_ptr temp = listLink->head;
    while (temp->next != listLink->fence){
      temp=temp->next;
      }
       listLink->fence = temp;
       listLink->leftcnt--;
       listLink->rightcnt++;
}
void print(LList listLink){
    node_ptr temp=listLink->head;
    temp=temp->next;
    while (temp!= NULL){
            printf("%d", temp->element);
            temp=temp->next;
            }
}

void addition(LList l1,LList l2){
        while(leftLength(l2)>0){
        l1->fence->element=l1->fence->element+l2->fence->element;
        prev(l1);
        prev(l2);
    }
    setEnd(l1);
    while(leftLength(l1)>1){
        if(l1->fence->element>=10){
            l1->fence->element=l1->fence->element%10;
            prev(l1);
            l1->fence->element++;
        }
        else prev(l1);
    }
    if(l1->fence->element>=10){
        l1->fence->element=l1->fence->element%10;
        prev(l1);
        //setStart(l1);
        insert(l1,1);}
}

int main(){
    int n,i,a,b;
    char c;
    LList list1=(LList)malloc(sizeof(struct LINKLIST)),list2=(LList)malloc(sizeof(struct LINKLIST));
    init(list1);init(list2);
    scanf("%d",&n);
    getchar();
    for (i=0;i<n;i++){
            while((c=getchar())!='\n'){
                a=c-'0';
                append(list1,a);
            }
            while((c=getchar())!='\n'){
                b=c-'0';
                append(list2,b);
            }
            setEnd(list1);setEnd(list2);
            if(leftLength(list1)>leftLength(list2)){
                addition(list1,list2);
                print(list1);
            }
            else {
                addition(list2,list1);
                print(list2);
                }
                printf("\n");
                clear(list1);clear(list2);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值