2013-04-30 56 views
0

我在爲這個問題編寫C程序時遇到了一些問題。也許我正在讀錯了這個問題,並以錯誤的方式去做。有人可以幫我嗎?這是他們的方式,我試圖做到這一點編寫一個程序來查找正奇數和小於或等於30的正偶數的乘積之和

#include<stdio.h> 
void main(void) 
{ 
    int j, sum=0; 
    long int product=1; 
    for(j=1;j<=30;j=j+2) 
    { 
     sum=sum+j; 
    } 
    for(j=2;j<=30;j=j+2) 
    { 
     product=product*j; 
    } 
    printf("\nThe sum of positive odd numbers is: %d", sum); 
    printf("\nThe product of positive even numbers is: %d", product); 
} 

我得到的輸出是:

The sum of positive odd numbers is: 225 
The product of positive even numbers is: -1409286144 

我得到的產品部分錯誤。我曾嘗試使用unsigned long int,long long,unsigned long long。什麼都沒有

+1

你的產品是溢滿您的存儲類型的限制。嘗試從'long int'換成更大的存儲類型。 – StarPilot 2013-04-30 23:04:26

+1

2 * 4 * 8 ... * 28大致爲30位數字。你幾乎需要一個浮點類型來保存它。對於它的價值:N個連續奇數的總和(從1開始)給出N平方,因此您可以更快更容易地計算該部分。 – 2013-04-30 23:05:56

+0

使用模塊代替將您的代碼濃縮到1循環。僞代碼 - for(j = 1,j <= 30,j ++)如果j%2 = 0,那麼product = product * j,否則sum = sum + j – 2013-04-30 23:10:58

回答

4

嘗試使用%ld,而不是%dprintf

printf("\nThe product of positive even numbers is: %ld", product);

因爲它是一個long int而不是int

如果您使用long long int,則需要%lld。鑑於這是一款非常大的產品,您可能需要很長的尺寸。我不知道您的平臺的long int是32位還是64位,但您肯定需要64位數字。

long long格式字符串可以根據您的確切平臺和編譯器而有所不同,但現在大多數情況下已標準化爲%lld。特別是,舊的微軟編譯器有時使用%I64d

+1

非常感謝! %lld的long long int爲我工作。 – 2013-05-01 13:43:24

+0

@StilesCrisis我害怕'printf()'中的'%ld'在我的編譯器Mingw和'ideone'中產生了一個不正確的輸出(負數)http://ideone.com/GRSMLE – 2013-05-01 15:38:17

+0

@SheerFish:你在32位模式,這是你看到的溢出。在32位平臺上,爲了得到正確的結果,你必須使用'long long int'。 – StilesCrisis 2013-05-01 17:24:41

1

有沒有問題,只要所有的奇數少於30而言的總和,因爲它是唯一225。但所有的偶數(或奇數爲此事)的產品不到30爲巨大 number.For你需要一個更大容量的數據類型。在下面的程序中,我簡單地使用double而不是long int代替product,並且我使用%e格式說明符以簡潔的方式在prinf()中顯示產品,儘管您可以使用%f

#include<stdio.h> 


int main(void) //Return type of main() is "int",not "void" as you've used 
{ 
    int j, sum=0; 
    double product=1; //Change type of "product" to "double" 

    for(j=1;j<=30;j=j+2) 
    { 
     sum=sum+j; 
    } 
    for(j=2;j<=30;j=j+2) 
    { 
     product=product*j; 
    } 

    printf("The sum of positive odd numbers is: %d\n", sum); 
    printf("The product of positive even numbers is: %e",product); //Use %e 
} 

輸出 The sum of positive odd numbers is: 225

 The product of positive even numbers is: 4.284987e+16 
+0

感謝您的解釋和答案。有用。 – 2013-05-01 13:46:30

+0

@RaedShahid'printf()'中的'%ld'是否按照上面提到的海報解決了您的問題?它發現錯誤的結果。 http://ideone.com/GRSMLE – 2013-05-01 15:39:27

+0

'double'可能會提供一個近似值,但不太可能得到實際的正確答案。根據您的需要,這可能就足夠了。 'long long int'將是精確的,但當結果增長> 2^64時,也會開始失敗,而double將繼續以精度降低的方式工作。 – StilesCrisis 2013-05-01 17:26:39

-1

計算使用unsinged INT(32位)

#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 

typedef unsigned short UInt16; 
typedef unsigned UInt32; 
typedef struct _unums { 
    size_t size; 
    UInt16 *nums;//array 
} UNums; 

void UNums_init(UNums *num, UInt16 n){ 
    num->nums = malloc(sizeof(UInt16)); 
    num->nums[0] = n; 
    num->size = 1; 
} 

void UNums_mul(UNums *num, UInt16 n){ 
    UInt16 carry = 0; 
    size_t i; 

    for(i=0;i<num->size;++i){ 
     UInt32 wk = n; 
     wk = wk * num->nums[i] + carry; 
     num->nums[i] = wk % 10000; 
     carry = wk/10000; 
    } 
    if(carry){ 
     num->size += 1; 
     num->nums = realloc(num->nums, num->size * sizeof(UInt16)); 
     num->nums[i] = carry; 
    } 
} 

void UNums_print(UNums *num){ 
    size_t i = num->size; 
    int w = 0; 
    do{ 
     --i; 
     printf("%0*hu", w, num->nums[i]); 
     if(!w) w = 4; 
    }while(i!=0); 
} 

void UNum_drop(UNums *num){ 
    free(num->nums); 
    num->nums = NULL; 
} 

int main(void){ 
    UNums n; 
    UInt16 i; 
    assert(sizeof(UInt32) == 4);//32bit 
    assert(sizeof(UInt16) == 2);//16bit 

    UNums_init(&n, 1); 
    for(i=2;i<=30;i+=2) 
     UNums_mul(&n, i); 
    UNums_print(&n);//42849873690624000 
    UNum_drop(&n); 
    return 0; 
} 
+0

downvote的原因是什麼?請告訴我,如果程序中有錯誤。 – BLUEPIXY 2013-05-01 10:16:11

+0

它不是我。但我不明白你在這裏做了什麼,我是新手在C,你可以告訴我的問題,我問:p – 2013-05-01 13:48:20

+0

Okey-dokey 你有沒有通過寫乘法計算? – BLUEPIXY 2013-05-01 13:57:48

相關問題