01/10/2018, 12:35

Code số đối xứng lớn nhất bị lỗi

Mọi người xem dùm em vs.
Đã có mảng sắp xếp ngon lành cành đào vậy rồi mà sao ghép số lại bị lỗi như vậy?
Thanks so much !!!

superuser10 viết 14:51 ngày 01/10/2018

Code here

//============================================================================
// Name        : SoDoiXungLonNhat.cpp
// Author      : GHH81HC
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define MAX 100
#define TEN 10


enum ElementOfNumber {
    Zero = 0,
    One,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
	Undefined = 1994
};
typedef ElementOfNumber ELEMENTS;

struct Counter {
    ELEMENTS Factor;
    int AmountOfFactors;
};
typedef Counter COUNTER;

struct Number {
	signed long long int Number;
    int AmountOfElements;
    int *p_Elements;
    COUNTER *p_Factor;
    int OddPosition;
};
typedef Number NUMBER;



NUMBER InputNum;

void toSplitNumber (signed long long int numberToSplit) {
    int buffer[MAX];
    int index = 0;
    do {
        buffer[index] = numberToSplit%10;
        numberToSplit =  numberToSplit/10;
        index++;
    } while (numberToSplit != 0);

    InputNum.AmountOfElements = index;
    InputNum.p_Elements = new int [index];
    memcpy(InputNum.p_Elements,buffer,sizeof(int)*index);
}


void countAmoutOfElements (void) {
    int index = 0;
    InputNum.p_Factor = new COUNTER [TEN];

    for(;index < 10; index++ ){
        InputNum.p_Factor[index].AmountOfFactors = 0;
		InputNum.p_Factor[index].Factor = (ELEMENTS)1994;
    }

    for(index = 0;index < InputNum.AmountOfElements; index++) {
        InputNum.p_Factor[InputNum.p_Elements[index]].AmountOfFactors++;
        InputNum.p_Factor[InputNum.p_Elements[index]].Factor = (ELEMENTS)InputNum.p_Elements[index];
    }

//    for(index = 0;index < 10; index++) {
//    	cout << "We have all " << InputNum.p_Factor[index].AmountOfFactors << " the factor " << index << " in the number" << endl;
//    }
}


bool isThisSymmetryNum (void) {
	int index = 0;
	int countOdd = 0;
	for(index = 0;index < 10; index++) {
		if (InputNum.p_Factor[index].AmountOfFactors % 2 != 0) {
			countOdd ++;
			InputNum.OddPosition = index;
			}
		}
	//cout << "So lan xuat hien le cua mot factor nao do: " << countOdd << endl;

	int isThisEven = InputNum.AmountOfElements % 2;
	//cout << "So chu so la chan hay le: " << InputNum.AmountOfElements << endl;
	if (isThisEven == 0) {
		if (countOdd == 0) return true;
	}
	else {
		if (countOdd == 1) return true;
	}
	return false;
}

NUMBER OutputNum;
void toArrangeNumber (void) {
	bool SymmetryNumber = false;
	int outdex = 9;
	int outport = 0;

	OutputNum.p_Elements = new int [InputNum.AmountOfElements];
	OutputNum.AmountOfElements = InputNum.AmountOfElements;
	for (;outport < InputNum.AmountOfElements; outport++) {
		OutputNum.p_Elements[outport] = 0;
	}


	SymmetryNumber = isThisSymmetryNum();
	int isThisEven = InputNum.AmountOfElements % 2;

	if (SymmetryNumber == true) {
		if (isThisEven == 0) {
			outport = 0;
			for (;outdex >= 0; outdex--) {
				int temp = InputNum.p_Factor[outdex].AmountOfFactors/2;
				if (InputNum.p_Factor[outdex].AmountOfFactors >= 2) {
					while (InputNum.p_Factor[outdex].AmountOfFactors > temp) {
						OutputNum.p_Elements[outport] = InputNum.p_Factor[outdex].Factor;
						outport++;
						InputNum.p_Factor[outdex].AmountOfFactors--;
					}
				}
			}
		}
		else {
			outdex = 9;
			outport = 0;
			OutputNum.p_Elements[InputNum.AmountOfElements/2] = InputNum.p_Factor[InputNum.OddPosition].Factor;
			//cout << "TEST " << InputNum.AmountOfElements/2+1 << "---" << OutputNum.p_Elements[InputNum.AmountOfElements/2] << endl;

			for (;outdex >= 0; outdex--) {
				int temp = InputNum.p_Factor[outdex].AmountOfFactors/2;
				if (InputNum.p_Factor[outdex].AmountOfFactors %2 == 0) {
					if (InputNum.p_Factor[outdex].AmountOfFactors >= 2) {
						while (InputNum.p_Factor[outdex].AmountOfFactors > temp) {
							OutputNum.p_Elements[outport] = InputNum.p_Factor[outdex].Factor;
							outport++;
							InputNum.p_Factor[outdex].AmountOfFactors--;
						}
					}
				}
				else {
					if (InputNum.p_Factor[outdex].AmountOfFactors >= 2) {
						while (InputNum.p_Factor[outdex].AmountOfFactors > temp+1) {
							OutputNum.p_Elements[outport] = InputNum.p_Factor[outdex].Factor;
							outport++;
							InputNum.p_Factor[outdex].AmountOfFactors--;
						}
					}
				}
			}
		}
	}

	for (outport = 0; outport < OutputNum.AmountOfElements/2; outport++) {
		OutputNum.p_Elements[(OutputNum.AmountOfElements-1)-outport] = OutputNum.p_Elements[outport];
	}
//
//	for (outport = 0; outport < OutputNum.AmountOfElements; outport++) {
//		cout << OutputNum.p_Elements[outport] << "	";
//	}
//	cout << endl;
}


void toMergeNumber (void) {
	OutputNum.Number = 0;
	int index = 0;

	cout << "Cac phan tu trong mang: " << endl;
	for (index = 0; index < OutputNum.AmountOfElements; index++) {
			cout << OutputNum.p_Elements[index] << "	";
		}
	cout << endl;

	for (index = 0;index < OutputNum.AmountOfElements; index++) {
		cout << OutputNum.p_Elements[index] << "..." << (OutputNum.AmountOfElements-1)-index << "..." << OutputNum.p_Elements[index]*pow(10,(OutputNum.AmountOfElements-1)-index) << endl;
		OutputNum.Number = OutputNum.Number + OutputNum.p_Elements[index]*pow(10,(OutputNum.AmountOfElements-1)-index);
		cout << "Tinh toan tung buoc: " << (OutputNum.AmountOfElements-1)-index << "---" << OutputNum.Number << endl;
	}
	cout << "So duoc nhap vao la: " << InputNum.Number << endl;
	cout << "So doi xung lon nhat la: " << OutputNum.Number << endl;
}

int main() {
	bool SymmetryNumber = false;
	cout << "Nhap so vao:" << endl;
	cin >> InputNum.Number;
	cout << InputNum.Number << endl;

	toSplitNumber(InputNum.Number);
	countAmoutOfElements();
	SymmetryNumber = isThisSymmetryNum();
	if (SymmetryNumber) cout << "YES" << endl;
	else cout << "NO" << endl;
	toArrangeNumber ();
	toMergeNumber ();
	return 0;
}

Ket qua

Bài liên quan
0