2015-03-31 22 views
-8

我需要輸入一個1000位的數字並處理它的每個數字。因此,我希望將數字拆分爲單個數字,並將它們放入1000個數組的每個字段中。我如何使用C編程來實現這一點?C編程:如何在數組中插入一個數字,以便數字的每個數字都進入數組的每個字段?

編輯:我一直在Project Euler問題8.我知道我必須做的:將每個數字放入一個數組,並使用滑動窗口來查找最大的產品。但我不確定實施情況。我已經搜索了其他答案,但恐怕所有這些答案都是C以外的任何語言,並且看起來很容易。我想在C中實現相同的功能,所以要求論壇。

+0

有很多的這些問題和答案已經在SO,這一比如:http://stackoverflow.com/questions/4962341/how-do-i-turn-an-integer-into-不使用字符串的數組類型 – 2015-03-31 19:13:38

+0

1000位數字的數據類型是什麼? – DWright 2015-03-31 19:14:36

+1

你是否嘗試過'populate_array_with_digit_splits(array,value);'?請注意,您可能必須編寫該功能。 – mah 2015-03-31 19:14:58

回答

1
#include <stdio.h> 


#define MAX_NUMS 5 // change me to 1000 
int main(int argc, const char * argv[]) 
{ 
    char numberString[ MAX_NUMS + 1 ]; 
    int numberNumeric [ MAX_NUMS ]; 
    printf("Enter number "); 
    scanf("%s",numberString); 
    for (int i=0; i < MAX_NUMS; ++i) 
    { 
     printf("converting %c\n",numberString[i]); 
     numberNumeric[i] = (numberString[i] - 0x30); // convert ascii to integer 
    } 

    // Your array of 1-digit numbers 
    for (int i=0; i < MAX_NUMS; ++i) 
    { 
    printf("%i ",numberNumeric[i]); 
    } 
    return 0; 
} 
相關問題