Programming to solve 1! + 2! + 3! +... + 19! + 20! Value, the problem how to do it, I should be in the process of, the result has been operating errors,
CodePudding user response:
20! A lot, to hold, for reference:
# include Int main () { int i; __int64 s=0, TMP=1; for(i=1; i<=20; I++) { TMP *=I; S +=TMP; } Printf (" % I64d \ n ", s); return 0; } CodePudding user response:
If this is clear some
int main () { /* * 1! + 2! + 3! +... + 19! + 20! * * 1 * 1 + 2 + 3 * 2 * 1 * 1 + 4 * 3 * 2 * 1 + * */ //a number is used to store each factorial Double last=1; //a number used to sum the double sum=0; //from 1 to 20 For (int I=1; I & lt;=20; I++) { The last *=I;//I=1=& gt; (1) I=1 * 2=& gt; (2 * 1 * 1) I=3=& gt; 3 * (2 * 1 * 1) I=4=& gt; 4 * (3 * 2 * 1 * 1) The sum +=last;//added together to } //print the results Printf (" lf the sum=% \ n ", sum); return 0; } CodePudding user response:
Try just now, thank you,
But in the fourth row and the second line of code, I don't understand the
CodePudding user response:
refer to the second floor small white ah really reply: this is understand some int main () { /* * 1! + 2! + 3! +... + 19! + 20! * * 1 * 1 + 2 + 3 * 2 * 1 * 1 + 4 * 3 * 2 * 1 + * */ //a number is used to store each factorial Double last=1; //a number used to sum the double sum=0; //from 1 to 20 For (int I=1; I & lt;=20; I++) { The last *=I;//I=1=& gt; (1) I=1 * 2=& gt; (2 * 1 * 1) I=3=& gt; 3 * (2 * 1 * 1) I=4=& gt; 4 * (3 * 2 * 1 * 1) The sum +=last;//added together to } //print the results Printf (" lf the sum=% \ n ", sum); return 0; } Got it, thank you, CodePudding user response:
reference qq_45773596 reply: 3/f try just now, thank you, But in the fourth row and the second line of code, I don't understand the When doing ACM problem, often will meet some of the bigger of the two integers, and the commonly used built-in integer types often appear too small: long and int scope Is [- 2 ^ 31, 2 ^ 31), namely - 2147483648 ~ 2147483647, and unsigned range is [0, 2 ^ 32), namely, 0 ~ 4294967295, that is, Regular 32-bit integer only able to handle the following number 4 billion, The encounter is bigger than 4 billion number? At this time will use c + + 64 - bit extensions, different compilers for the expansion of the 64 - bit integer Different, VC6.0 64 - bit integer respectively called __int64 and unsigned __int64, its scope is [- 2 ^ 63, 2 ^ 63), and [0, 2 ^ 64), namely - 9223372036854775808 ~ 9223372036854775807 and 0 ~ 18446744073709551615 (180 billion), for a 64 - bit integer arithmetic and 32-bit integer basic same, support arithmetic and bit operations, CodePudding user response:
refer to the second floor small white ah really reply: this is understand some int main () { /* * 1! + 2! + 3! +... + 19! + 20! * * 1 * 1 + 2 + 3 * 2 * 1 * 1 + 4 * 3 * 2 * 1 + * */ //a number is used to store each factorial Double last=1; //a number used to sum the double sum=0; //from 1 to 20 For (int I=1; I & lt;=20; I++) { The last *=I;//I=1=& gt; (1) I=1 * 2=& gt; (2 * 1 * 1) I=3=& gt; 3 * (2 * 1 * 1) I=4=& gt; 4 * (3 * 2 * 1 * 1) The sum +=last;//added together to } //print the results Printf (" lf the sum=% \ n ", sum); return 0; } Test for a moment, your program output result: Sum=2561327494111820288.000000 The individual feels this result is wrong, because this result should be an odd number, rather than an even number; The last and the sum is defined as an unsigned long long after the result is Sum=2561327494111820313 Will be the last and the sum is defined as a long double was no problem, after received: Sum=2561327494111820313.000000 In addition, for a long double output. Suggest use % 0 lf format output, because the class and will not be a floating point number (real), was supposed to get an integer; CodePudding user response:
reference 6 building self-confidence boy reply: Quote: refer to the second floor small white ah really reply: this is understand some int main () { /* * 1! + 2! + 3! +... + 19! + 20! * * 1 * 1 + 2 + 3 * 2 * 1 * 1 + 4 * 3 * 2 * 1 + * */ //a number is used to store each factorial Double last=1; //a number used to sum the double sum=0; //from 1 to 20 For (int I=1; I & lt;=20; I++) { The last *=I;//I=1=& gt; (1) I=1 * 2=& gt; (2 * 1 * 1) I=3=& gt; 3 * (2 * 1 * 1) I=4=& gt; 4 * (3 * 2 * 1 * 1) The sum +=last;//added together to } //print the results Printf (" lf the sum=% \ n ", sum); return 0; } Test for a moment, your program output result: Sum=2561327494111820288.000000 The individual feels this result is wrong, because this result should be an odd number, rather than an even number; The last and the sum is defined as an unsigned long long after the result is Sum=2561327494111820313 Will be the last and the sum is defined as a long double was no problem, after received: Sum=2561327494111820313.000000 In addition, for a long double output. Suggest use % 0 lf format output, because the class and will not be a floating point number (real), was supposed to get an integer; I'm dizzy, which is a method for ah CodePudding user response:
reference 6 building self-confidence boy reply: Quote: refer to the second floor small white ah really reply: this is understand some int main () { /* * 1! + 2! + 3! +... + 19! + 20! * * 1 * 1 + 2 + 3 * 2 * 1 * 1 + 4 * 3 * 2 * 1 + * */ //a number is used to store each factorial Double last=1; //a number used to sum the double sum=0; //from 1 to 20 For (int I=1; I & lt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull