user-0 提问: 20! 用%d算出来是怎样都是负数 用f就是整数 为什么?

回复:
user: user-1
content: 仅供参考：#include <iostream> #include <string> #include <algorithm> using namespace std; inline int COMPARE(string str1,string str2) {//相等返回0，大于返回1，小于返回-1          if (str1.size()>str2.size()) return 1; //长度长的整数大于长度小的整数     else if (str1.size()<str2.size()) return -1;     else                              return str1.compare(str2); //若长度相等，则头到尾按位比较 } string PLUS(string number1,string number2) {     int i;     int length1 = number1.size();     int length2 = number2.size();      string result="";      reverse(number1.begin(), number1.end());     reverse(number2.begin(), number2.end());      for(i = 0; i < length1 && i < length2; i++) {         char c = (char)(number1[i] + number2[i] - 48);         result = result + c;     }      while(i < length1) {         result = result + number1[i];         ++i;     }      while(i < length2) {         result = result + number2[i];         ++i;     }      int carry = 0;     for(i = 0; i < (int)result.size(); ++i) {         int value = result[i] - 48 + carry;         result[i] = (char)(value % 10 + 48);         carry = value / 10;     }      if(carry !=0 ) {         result = result + (char)(carry + 48);     }      for(i = result.size() - 1; i >= 0; i--) {         if(result[i] != '0') break;     }      result = result.substr(0, i + 1);      reverse(result.begin(), result.end());     if(result.length() == 0) result = "0";     return result; } string MULTIPLY(string number1, string number2) {     int i, j;     int *iresult;     int length1 = number1.size();     int length2 = number2.size();     string result = "";      reverse(number1.begin(), number1.end());     reverse(number2.begin(), number2.end());      iresult = (int*)malloc(sizeof(int) * (length1 + length2 + 1));     memset(iresult, 0, sizeof(int) * (length1 + length2 + 1));      for(i = 0; i < length1; i++) {         for(j = 0; j < length2; j++) {             iresult[i+j] += ((number1[i] - 48) * (number2[j] - 48));         }     }      int carry = 0;     for(i = 0; i < length1 + length2; i++) {         int value = iresult[i] + carry;         iresult[i] = value % 10;         carry = value / 10;     }      for(i = length1 + length2 - 1; i >= 0; i--) {         if(iresult[i] != 0)break;     }      for(; i >= 0; i--) {         result = result + (char)(iresult[i]+48);     }      free(iresult);      if(result == "") result = "0";     return result; } string factorial(string n) {     string temp = "1";     string i;     for(i = "1"; COMPARE(i, n) <= 0; i = PLUS(i, "1")) {         temp = MULTIPLY(temp, i);     }     return temp; } int main(void) {     cout << factorial("100") << endl;     return 0; } //93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 //

user: user-2
content: 问题的解决方案，楼上的大神都提到了。我建议，楼主把每次计算的结果打印下看看 for (int i = 1; i < 20; i++) { k*=i; printf("k=%lld ", k); j+=k; printf("j=%lld\n", j); }

user: user-3
content: 引用 5 楼 qq_45690504 的回复:[quote=引用 1 楼 自信男孩的回复:]溢出了，建议用unsigned long long保存20!的结果  #include<stdio.h>  int main() {     int i;     unsigned long long  sum = 1;      for (i = 1; i < 20; i++)         sum *= i;      printf("%llu\n", sum);      return 0; } 供参考～ 你好! unsigned long long什么意思?[/quote]  如果学习过基本的C语言，应该知道这个unsigned long long，他也是表示整型，只不过表示范围比int要打一倍。 可以从网上了解一下～

user: user-4
content: int型数据计算不了20的阶乘，溢出了，看看十进制与二进制的转换和补码的章节内容

user: user-5
content: 楼主，数据类型，自动转换和强制转换，三个知识点百度一下，还有字符串处理，这几个基础点比指针更重要。一起加油

user: user-6
content: 引用 5 楼 qq_45690504 的回复:[quote=引用 1 楼 自信男孩的回复:]溢出了，建议用unsigned long long保存20!的结果  #include<stdio.h>  int main() {     int i;     unsigned long long  sum = 1;      for (i = 1; i < 20; i++)         sum *= i;      printf("%llu\n", sum);      return 0; } 供参考～ 你好! unsigned long long什么意思?[/quote]  64位长整型

user: user-0
content: 引用 1 楼 自信男孩的回复:溢出了，建议用unsigned long long保存20!的结果  #include<stdio.h>  int main() {     int i;     unsigned long long  sum = 1;      for (i = 1; i < 20; i++)         sum *= i;      printf("%llu\n", sum);      return 0; } 供参考～ 你好! unsigned long long什么意思?

user: user-7
content: 应该是溢出了

user: user-6
content: 这样肯定是不可以的呀

user: user-3
content: 浮点数可以的原因建议查一下浮点数的表示范围～

user: user-3
content: 溢出了，建议用unsigned long long保存20!的结果  #include<stdio.h>  int main() {     int i;     unsigned long long  sum = 1;      for (i = 1; i < 20; i++)         sum *= i;      printf("%llu\n", sum);      return 0; } 供参考～