gpt4 book ai didi

c - 贪心算法 - 罗马数字

转载 作者:行者123 更新时间:2023-11-30 18:29:37 25 4
gpt4 key购买 nike

我正在尝试实现一个转换整数 n 的 C 程序(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;
}

我尝试过调试和修改代码的各个方面,但它不起作用。如果有任何反馈,我将不胜感激。

<小时/>

更新 我已设法修改我的程序,使其正确输出值 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 个缺失点:

最大功能:

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]);

biggestNo 是 seq 编号,而不是 div 的值

关于c - 贪心算法 - 罗马数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36141693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com