贪心算法 - 罗马数字

时间:2016-03-21 21:12:09

标签: c greedy

我正在尝试实现一个C程序,它通过贪心算法将整数n(0到1024之间)转换为罗马数字。我试图通过以下方式做到这一点:

#include <stdio.h>
#include <string.h>
void convert(int);
int max(int[], int, int);

int main(){

    //User Input
    int n;
    printf("Enter a digit between 0 and 1024: ");
    scanf("%d", &n);

    //Validation
    while((n < 0)||(n > 1024)){
        printf("That number is not between 0 and 1024. Please try again: ");
        scanf("%d", &n);
    }

    //Output
    printf("\nAs a Roman numeral, this was written: ");
    if (n == 0) printf("nulla");    //Romans wrote 'nulla' for zero
    else convert(n);

    return 0;
}

void convert(int n){
    //Case n = 0
    if (n == 0)  return;

    else{
        //Case n > 0
        char* romanNums[] = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
        int arabicNums[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
        int biggestNo = max(arabicNums, 12, n); //Biggest number in arabicNums[] smaller than n

        printf("%s", romanNums[biggestNo]); //Print the biggest number as a Roman numeral
        convert(n - biggestNo);             //Convert the remaining

    }
}

//This function determines the maximum number in arr[] smaller than n
int max(int arr[], int size, int n){
    int i, max;
    for(i = 0; i < size; i++){
        if (n < arr[i]) max = i;
    }
    return max;
}

我已尝试调试和修改代码的各个方面,但它无法正常工作。我很感激任何反馈。

UPDATE 我设法修改了我的程序,以便正确输出值1,4,5等,但是复合值(即需要另一次迭代convert()的值) )导致“Romans.exe没有响应”。这是新代码:

#include <stdio.h>
#include <string.h>
void convert(int);
int max(int[], int, int);

int main(){

    //User Input
    int n;
    printf("Enter a digit between 0 and 1024: ");
    scanf("%d", &n);

    //Validation
    while((n < 0)||(n > 1024)){
        printf("That number is not between 0 and 1024. Please try again: ");
        scanf("%d", &n);
    }

    //Output
    printf("\nAs a Roman numeral, this was written: ");
    if (n == 0) printf("nulla");    //Romans wrote 'nulla' for zero
    else convert(n);

    return 0;
}

void convert(int n){
    //Case n = 0
    if (n == 0)  return;

    else{
        //Case n > 0
        char* romanNums[] = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
        int arabicNums[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
        int biggestNo = max(arabicNums, 13, n); //Biggest number in arabicNums[] smaller than n

        printf("%s", romanNums[biggestNo]); //Print the biggest number as a Roman numeral
        convert(n - arabicNums[biggestNo]); //Convert the remaining

    }
}

//This function determines the maximum number in arr[] smaller than n
int max(int arr[], int size, int n){
    int i, max;
    for(i = size; i > 0; i--){
        if (n <= arr[i]) max = i;
    }
    return max;
}

2 个答案:

答案 0 :(得分:3)

您的代码中有2个缺失点:

关于函数max:

for(i = 0; i < size; i++){
    if (n < arr[i]) max = i;
}

必须是:

for(i = 0; i <= size; i++){
    if (n >= arr[i]) max = i;   // Equal is require. Isn't it?
}

在你的主要功能上:功能转换:

convert(n - biggestNo);             //Convert the remaining

必须是:

convert(n - arabicNums[biggestNo]);

largestNo是seq编号,不是div的值

答案 1 :(得分:1)

当arabicNums数组的大小为13时,您将大小12传递给max函数。