您的位置:首页 > 其它

CareerCup2.5

2013-03-08 14:13 387 查看
//============================================================================
// Name : Career25.cpp
// Author : SK2
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

struct node {
int data;
node * next;
};

node* addUp(node * one, node * two) {
node * oneCur = one;
node * twoCur = two;
int carry = 0;
node * head, *current;
for(int i = 0; !(oneCur == NULL && twoCur == NULL) ; i++) {
int oneDigit = 0, twoDigit = 0;
if(oneCur != NULL) oneDigit = oneCur->data;
else oneDigit = 0;
if(twoCur != NULL) twoDigit = twoCur->data;
else twoDigit = 0;

int res = oneDigit + twoDigit + carry;
int digit = res%10;
carry = res / 10;

if(i == 0) {
node * nod = new node();
head = nod;
current = head;
head->data = digit;

} else {
node * nod = new node();
nod->data = digit;
current->next = nod;
current = current->next;
}

if(oneCur != NULL) oneCur = oneCur->next;
if(twoCur != NULL) twoCur = twoCur->next;
}

return head;
}

int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

node a, b, c;
a.data = 7; a.next = &b;
b.data = 1; b.next = &c;
c.data = 6; c.next = NULL;

node x, y, z;
x.data = 5; x.next = &y;
y.data = 9; y.next = &z;
z.data = 2; z.next = NULL;

node * t = addUp(&a, &x);
while(t != NULL) {
cout<<t->data<<endl;
t = t->next;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: