創建一個函式,該函式將數字 n 作為引數,并回傳小于或等于 n 且具有最高數字和的最大整數。
例子:
largestDigitSum(100) ? 99
// Digit Sum for 99 = 9 9 = 18
// All numbers from 0 to 98 and 100 itself have digit sum lesser than 18.
largestDigitSum(48) ? 48
// Digit sum for 48 = 4 8 =12
// Digit sum for 39 = 3 9 =12
// Return 48 because 48 > 39
largestDigitSum(10) ? 9
所以基本上我嘗試解決這個問題,但無論我做什么,該函式都會回傳 0。這是我的編碼嘗試:
int largestDigitSum(int n)
{
int s1 = 0, s2, c, maxi;
while (n > 0) {
c = n % 10;
s1 = c;
n /= 10;
}
while (n > 0) {
n--;
while (n > 0) {
c = n % 10;
s2 = c;
n /= 10;
}
if (s1 > s2)
maxi = n;
else
s1 = s2;
}
return maxi;
}
有人可以解釋一下我做錯了什么嗎?
編輯:
好的,感謝所有幫助我理解這個問題的人。我試著把它分成 2 個功能,現在它可以作業了!這是我的新代碼:
int digitSum(int n)
{int s=0;
while(n>0)
{s = n%10;
n/=10;}
return s;
}
int largestDigitSum(int n)
{int i, maxi=1;
for(i=2;i<=n;i )
if(digitSum(i) >= digitSum(maxi))
maxi=i;
return maxi;
}
uj5u.com熱心網友回復:
int largestDigitSum(int n)
{
int s1 = 0, s2, c, maxi;
while (n > 0) {
c = n % 10;
s1 = c;
n /= 10; // This loop will never end until n becomes 0
}
while (n > 0) { // n is 0 because of the last loop
n--; // this will never enter
while (n > 0) {
c = n % 10;
s2 = c;
n /= 10;
}
if (s1 > s2)
maxi = n; // this will never be called
else
s1 = s2;
}
return maxi; // maxi is never being initialized to begin with
}
maxi甚至沒有被初始化,所以你看到 0 的事實僅僅是未定義行為的結果。這就是該函式始終回傳 0 的原因。
uj5u.com熱心網友回復:
- 此任務僅在輸入數字無符號時才有意義。如果它已簽名,您應該搜索直到 INT_MIN(作為 anynumner >= INT_MIN)。
將函式用于重復的任務。
unsigned sumdigits(unsigned x)
{
unsigned sum = 0;
while(x)
{
sum = x % 10;
x /= 10;
}
return sum;
}
unsigned brutForce(unsigned x)
{
unsigned cmax = 0, csum, maxnum;
while(x)
{
csum = sumdigits(x);
if(csum > cmax) {cmax = csum; maxnum = x;}
x--;
}
return maxnum;
}
int main(void)
{
printf("Max number: %u\n", brutForce(100000));
}
當然,還有更有效的方法來做到這一點。
uj5u.com熱心網友回復:
最好將您正在最大化的函式的評估與尋求最大值的實際程序分開:
static unsigned int
sum_of_digits(unsigned int n)
{
unsigned int sum = 0;
while (n > 0) {
sum = n % 10;
n /= 10;
}
return sum;
}
這樣,您可以將“數字總和”功能與任何其他邏輯分開推理。
我對給出所需結果的整數 ( argmax) 和最大化值都感興趣。因此,我宣告了一個可以包含兩個值的結構:
struct max_result {
unsigned int argmax;
unsigned int max;
};
現在,我可以撰寫一個函式來尋找某個范圍內另一個函式的最大值:
static struct max_result *
max_value_of_function(
unsigned int n,
unsigned int (*fn)(unsigned int)
)
{
unsigned int i = 1;
struct max_result *r = malloc(sizeof(*r));
assert(r);
r->argmax = 0;
r->max = 0;
do {
unsigned int s = fn(i);
if (s >= r->max) {
r->max = s;
r->argmax = i;
}
if (i == n) {
break;
}
i;
} while (1);
return r;
}
這意味著您可以獨立于計算數字的數字總和來專注于尋求最大值的邏輯。
在撰寫這樣的東西時,從一些測驗用例開始總是很有用的。無需做任何花哨的事情......將給定輸入的預期輸出與函式的實際回傳值進行比較的東西。我把你的問題中列出的案例加上另外兩個有趣的案例。
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct max_result {
unsigned int argmax;
unsigned int max;
};
struct test_case {
const unsigned int input;
const unsigned int expected;
};
static
int sum_of_digits(unsigned int n)
{
int sum = 0;
while (n > 0) {
sum = n % 10;
n /= 10;
}
return sum;
}
static struct max_result *
max_value_of_function(
unsigned int n,
unsigned int (*fn)(unsigned int)
)
{
unsigned int i = 1;
struct max_result *r = malloc(sizeof(*r));
assert(r);
r->argmax = 0;
r->max = 0;
do {
unsigned int s = fn(i);
if (s >= r->max) {
r->max = s;
r->argmax = i;
}
if (i == n) {
break;
}
i;
} while (1);
return r;
}
int
main(void) {
int i;
struct test_case cases[] = {
{ 0, 0 }, { 100, 99 }, { 48, 48 }, { 10, 9 }, { UINT_MAX, 3999999999U },
};
for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i) {
struct max_result *r = max_value_of_function(cases[i].input, sum_of_digits);
if (r->argmax == cases[i].expected) {
printf(
"The positive integer with largest sum of digits in [0, %u] is %u\n"
"Sum of its digits is %u\n",
cases[i].input,
r->argmax,
r->max
);
}
else {
printf("Got %u ... expected %u\n", r->argmax, cases[i].expected);
}
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/341547.html
標籤:C
