Problem A: Big vs Big (2)
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 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);
}
}