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;
}
// 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;
}
相关文章推荐
- Hard 大文本找两个单词最短距离 @CareerCup
- CareerCup Arrange the 2 x 1 boards on the 2 x n board
- CareerCup Write the code to find lexicographic minimum in a circular array
- LinkLists 两个链表相加 @CareerCup
- CareerCup Find the usernames existing in both documents
- CareerCup Insert 0s to maximize A*B
- Careercup - Microsoft面试题 - 5175246478901248
- Careercup Facebook Keep the number beautiful...
- CareerCup Chapter 4 Trees and Graphs
- careercup-树与图 4.3
- Career Cup 1-7
- careercup3.5
- CareerCup1.5
- CareerCup-4.2
- career_cup
- Recursion 爬楼梯问题 @CareerCup
- Sorting_Searching 读入数字流求rank @CareerCup
- Hard 找到由其它单词组成的最长单词 @CareerCup
- CareerCup What is the difference between a computers heap and it's stack?
- Arrays_Strings 翻转字符串 @CareerCup