CareerCup2.5




//============================================================================
// 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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章