user-0 提问: 请高手帮忙看看我程序有啥问题？我觉得挺好的，但有些结果不对。

回复:
user: user-1
content: 国际大学生程序设计竞赛的问题，一般得先用数学知识考虑一下，而不是直接用计算机暴力解决  问题：求n的阶乘最后一位非0数字，n是1到1000的数字。  规律： 1输出1 2输出2【1乘以2（对10取模）】 3输出6【2乘以3（对10取模）】 4输出4【6乘以4（对10取模），保留个位为4】 5输出2【4乘以5对10取模后，保留个位为2】 6输出2【2乘以6对10取模后，保留个位为2】  看出规律来了吗？  算法： n输出：（n-1的输出结果 乘以 n）对10取模后的个位数字 连阶乘的运算都省了，效率非常高，而且还不会溢出

user: user-2
content: 仅供参考#include <iostream> #include <string> #include <algorithm> using namespace std; int COMPARE(string number1, string number2) {     int i,j;      int length1 = number1.size();     int length2 = number2.size();      if(number1.size() == 0) number1 = "0";     if(number2.size() == 0) number2 = "0";      j = 0;     for(i = 0; i < length1; ++i) {         if(number1[i] == '0') ++j;         else break;     }     number1 = number1.substr(j);      j = 0;     for(i = 0; i < length2; ++i) {         if(number2[i] == '0') ++j;         else break;     }     number2 = number2.substr(j);      length1 = number1.size();     length2 = number2.size();      if(length1 > length2) {         return 1;     } else if(length1 == length2) {         if(number1.compare(number2) > 0) {             return 1;         } else if(number1.compare(number2) == 0) {             return 0;         } else {             return -1;         }     } else {         return -1;     }      return 0; } 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 MINUS(string number1,string number2) {     int i;     string result = "";      int length1 = number1.size();     int length2 = number2.size();      if(COMPARE(number2,number1) > 0) {         return "-" + MINUS(number2, number1);     }      reverse(number1.begin(),number1.end());     reverse(number2.begin(),number2.end());      for(i = 0; i < length1 && i < length2; i++) {         char c = number1[i] - number2[i] + 48;         result = result + c;     }      if(i < length1) {         for(; i < length1; i++) {             result = result + number1[i];         }     }      int carry = 0;     for(i = 0; i < (int)result.length(); i++) {         int value = result[i] - 48 + carry;         if(value < 0) {             value = value + 10;             carry = -1;         } else carry = 0;         result[i]=(char)(value + 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("1000") << endl;     return 0; }

user: user-1
content: Mark一下，蛮有趣的问题，考虑一下再回来回复

user: user-3
content: 把int转换成字符串类型，对每一位进行比较可以么？

user: user-1
content: 引用 12 楼 panasonic0804 的回复:Last no zero num of (250!) is: 2 Last no zero num of (251!) is: 4 不好意思，这两个数字好像不对  我的程序输出的结果是： Last non zero digit of (250!) is: 8 Last non zero digit of (251!) is: 8 感觉#21的应该是正确的……

user: user-4
content: 按7楼思路简单写了个代码，500和1000验证是对的   int total2=0; int total3=0; int total7=0;  void get_value(int i ) { 	while (i%2==0) 	{ 		total2++; 		i=i/2; 	} 	while (i%5==0) 	{ 		i=i/5; 		total2--; 	} 	int remain = i%10; 	switch (remain) 	{ 		case 3: 			total3++; 			break; 		case 7: 			total7++; 			break; 		case 9: 			total3+=2; 			break;  	} }  int calc_number(int n) { 	int num3[]={1,3,9,7}; 	int num7[]={1,7,9,3}; 	int num2[]={6,2,4,8}; 	total2=0; 	total3=0; 	total7=0;  	for (int i =1;i<=n;i++) 	{ 		if (i%2==0 || i%5==0) 			get_value(i); 		else if (i%10==3) 			total3++; 		else if (i%10==7) 			total7++; 		else if (i%10==9) 			total3+=2;  	}    	printf("%d! ,total2=%d,total3=%d,total7=%d ",n,total2,total3,total7); 	total2 = total2%4; 	total3%=4; 	total7%=4;  	int res = num2[total2]* num3[total3]*num7[total7]%10; 	printf("res=%d \n ",res); 	return res; }

user: user-4
content: 7楼思路应该是正确的，

user: user-1
content: 不好意思，#20的代码有些问题 #include <stdio.h> void main() { 	long lastEffectNumber = 1; 	for (int nn = 1; nn <= 1000; nn++) { 		lastEffectNumber *= nn; 		while (lastEffectNumber % 10 == 0) { lastEffectNumber /= 10;} 		lastEffectNumber %= 1000*10; 		printf("Last non zero digit of (%d!) is: %d\n", nn, lastEffectNumber % 10); 	} } 完整输出： http://my.bccn.net/paste/1148/

user: user-5
content: 乘积溢出的时候结果就不对了

user: user-5
content: num7[]={7,9,3,1}

user: user-6
content: 引用 楼主 soft_hardd 的回复:int sum_r(int input_num) {          if (input_num < 1 || input_num > 1000) 	{ 		return 0; 	} 	if (input_num == 1 || input_num == 2) 	{ 		return input_num;         } 	else 	{ 		return get_last_r(get_last_r(input_num*(input_num-1)) * sum_r(input_num-2)); 	} }  问题出在当前有效位和下一个数有效相乘时结果正好为10的倍数时，所获得结果有效位是无效的。例如12 x 15时。

user: user-6
content: Last no zero num of (1!) is: 1 Last no zero num of (2!) is: 2 Last no zero num of (3!) is: 6 Last no zero num of (4!) is: 4 Last no zero num of (5!) is: 2 Last no zero num of (6!) is: 2 Last no zero num of (7!) is: 4 Last no zero num of (8!) is: 2 Last no zero num of (9!) is: 8 Last no zero num of (10!) is: 8 Last no zero num of (11!) is: 8 Last no zero num of (12!) is: 6 Last no zero num of (13!) is: 8 Last no zero num of (14!) is: 2 Last no zero num of (15!) is: 8 Last no zero num of (16!) is: 8 Last no zero num of (17!) is: 6 Last no zero num of (18!) is: 8 Last no zero num of (19!) is: 2 Last no zero num of (20!) is: 4 Last no zero num of (21!) is: 4 Last no zero num of (22!) is: 8 Last no zero num of (23!) is: 4 Last no zero num of (24!) is: 6 Last no zero num of (25!) is: 4 Last no zero num of (26!) is: 4 Last no zero num of (27!) is: 8 Last no zero num of (28!) is: 4 Last no zero num of (29!) is: 6 Last no zero num of (30!) is: 8 Last no zero num of (31!) is: 8 Last no zero num of (32!) is: 6 Last no zero num of (33!) is: 8 Last no zero num of (34!) is: 2 Last no zero num of (35!) is: 2 Last no zero num of (36!) is: 2 Last no zero num of (37!) is: 4 Last no zero num of (38!) is: 2 Last no zero num of (39!) is: 8 Last no zero num of (40!) is: 2 ... Last no zero num of (100!) is: 4 ... Last no zero num of (200!) is: 2 Last no zero num of (201!) is: 2 Last no zero num of (202!) is: 4 Last no zero num of (203!) is: 2 Last no zero num of (204!) is: 8 Last no zero num of (205!) is: 4 Last no zero num of (206!) is: 4 Last no zero num of (207!) is: 8 Last no zero num of (208!) is: 4 Last no zero num of (209!) is: 6 Last no zero num of (210!) is: 6 Last no zero num of (211!) is: 6 Last no zero num of (212!) is: 2 Last no zero num of (213!) is: 6 Last no zero num of (214!) is: 4 Last no zero num of (215!) is: 6 Last no zero num of (216!) is: 6 Last no zero num of (217!) is: 2 Last no zero num of (218!) is: 6 Last no zero num of (219!) is: 4 Last no zero num of (220!) is: 8 Last no zero num of (221!) is: 8 Last no zero num of (222!) is: 6 Last no zero num of (223!) is: 8 Last no zero num of (224!) is: 2 Last no zero num of (225!) is: 2 Last no zero num of (226!) is: 2 Last no zero num of (227!) is: 4 Last no zero num of (228!) is: 2 Last no zero num of (229!) is: 8 Last no zero num of (230!) is: 4 Last no zero num of (231!) is: 4 Last no zero num of (232!) is: 8 Last no zero num of (233!) is: 4 Last no zero num of (234!) is: 6 Last no zero num of (235!) is: 6 Last no zero num of (236!) is: 6 Last no zero num of (237!) is: 2 Last no zero num of (238!) is: 6 Last no zero num of (239!) is: 4 Last no zero num of (240!) is: 6 Last no zero num of (241!) is: 6 Last no zero num of (242!) is: 2 Last no zero num of (243!) is: 6 Last no zero num of (244!) is: 4 Last no zero num of (245!) is: 8 Last no zero num of (246!) is: 8 Last no zero num of (247!) is: 6 Last no zero num of (248!) is: 8 Last no zero num of (249!) is: 2 Last no zero num of (250!) is: 2 Last no zero num of (251!) is: 4 Last no zero num of (252!) is: 6 Last no zero num of (253!) is: 8 Last no zero num of (254!) is: 2 Last no zero num of (255!) is: 6 Last no zero num of (256!) is: 6 Last no zero num of (257!) is: 2 Last no zero num of (258!) is: 6 Last no zero num of (259!) is: 4 Last no zero num of (260!) is: 4 Last no zero num of (261!) is: 4 Last no zero num of (262!) is: 8 Last no zero num of (263!) is: 4 Last no zero num of (264!) is: 6 Last no zero num of (265!) is: 4 Last no zero num of (266!) is: 4 Last no zero num of (267!) is: 8 Last no zero num of (268!) is: 4 Last no zero num of (269!) is: 6 Last no zero num of (270!) is: 2 Last no zero num of (271!) is: 2 Last no zero num of (272!) is: 4 Last no zero num of (273!) is: 2 Last no zero num of (274!) is: 8 Last no zero num of (275!) is: 2 Last no zero num of (276!) is: 2 Last no zero num of (277!) is: 4 Last no zero num of (278!) is: 2 Last no zero num of (279!) is: 8 Last no zero num of (280!) is: 4 Last no zero num of (281!) is: 4 Last no zero num of (282!) is: 8 Last no zero num of (283!) is: 4 Last no zero num of (284!) is: 6 Last no zero num of (285!) is: 6 Last no zero num of (286!) is: 6 Last no zero num of (287!) is: 2 Last no zero num of (288!) is: 6 Last no zero num of (289!) is: 4 Last no zero num of (290!) is: 6 Last no zero num of (291!) is: 6 Last no zero num of (292!) is: 2 Last no zero num of (293!) is: 6 Last no zero num of (294!) is: 4 Last no zero num of (295!) is: 8 Last no zero num of (296!) is: 8 Last no zero num of (297!) is: 6 Last no zero num of (298!) is: 8 Last no zero num of (299!) is: 2 Last no zero num of (300!) is: 6 .... Last no zero num of (400!) is: 8 ... Last no zero num of (500!) is: 4 ... Last no zero num of (600!) is: 6 ..... Last no zero num of (700!) is: 4 ... Last no zero num of (799!) is: 2 Last no zero num of (800!) is: 6 Last no zero num of (801!) is: 6 ... Last no zero num of (899!) is: 8 Last no zero num of (900!) is: 2 Last no zero num of (901!) is: 2 Last no zero num of (902!) is: 4 Last no zero num of (903!) is: 2 Last no zero num of (904!) is: 8 Last no zero num of (905!) is: 4 Last no zero num of (906!) is: 4 Last no zero num of (907!) is: 8 Last no zero num of (908!) is: 4 Last no zero num of (909!) is: 6 Last no zero num of (910!) is: 6 Last no zero num of (911!) is: 6 Last no zero num of (912!) is: 2 Last no zero num of (913!) is: 6 Last no zero num of (914!) is: 4 Last no zero num of (915!) is: 6 Last no zero num of (916!) is: 6 Last no zero num of (917!) is: 2 Last no zero num of (918!) is: 6 Last no zero num of (919!) is: 4 Last no zero num of (920!) is: 8 Last no zero num of (921!) is: 8 Last no zero num of (922!) is: 6 Last no zero num of (923!) is: 8 Last no zero num of (924!) is: 2 Last no zero num of (925!) is: 6 Last no zero num of (926!) is: 6 Last no zero num of (927!) is: 2 Last no zero num of (928!) is: 6 Last no zero num of (929!) is: 4 Last no zero num of (930!) is: 2 Last no zero num of (931!) is: 2 Last no zero num of (932!) is: 4 Last no zero num of (933!) is: 2 Last no zero num of (934!) is: 8 Last no zero num of (935!) is: 8 Last no zero num of (936!) is: 8 Last no zero num of (937!) is: 6 Last no zero num of (938!) is: 8 Last no zero num of (939!) is: 2 Last no zero num of (940!) is: 8 Last no zero num of (941!) is: 8 Last no zero num of (942!) is: 6 Last no zero num of (943!) is: 8 Last no zero num of (944!) is: 2 Last no zero num of (945!) is: 4 Last no zero num of (946!) is: 4 Last no zero num of (947!) is: 8 Last no zero num of (948!) is: 4 Last no zero num of (949!) is: 6 Last no zero num of (950!) is: 2 Last no zero num of (951!) is: 2 Last no zero num of (952!) is: 4 Last no zero num of (953!) is: 2 Last no zero num of (954!) is: 8 Last no zero num of (955!) is: 4 Last no zero num of (956!) is: 4 Last no zero num of (957!) is: 8 Last no zero num of (958!) is: 4 Last no zero num of (959!) is: 6 Last no zero num of (960!) is: 6 Last no zero num of (961!) is: 6 Last no zero num of (962!) is: 2 Last no zero num of (963!) is: 6 Last no zero num of (964!) is: 4 Last no zero num of (965!) is: 6 Last no zero num of (966!) is: 6 Last no zero num of (967!) is: 2 Last no zero num of (968!) is: 6 Last no zero num of (969!) is: 4 Last no zero num of (970!) is: 8 Last no zero num of (971!) is: 8 Last no zero num of (972!) is: 6 Last no zero num of (973!) is: 8 Last no zero num of (974!) is: 2 Last no zero num of (975!) is: 2 Last no zero num of (976!) is: 2 Last no zero num of (977!) is: 4 Last no zero num of (978!) is: 2 Last no zero num of (979!) is: 8 Last no zero num of (980!) is: 4 Last no zero num of (981!) is: 4 Last no zero num of (982!) is: 8 Last no zero num of (983!) is: 4 Last no zero num of (984!) is: 6 Last no zero num of (985!) is: 6 Last no zero num of (986!) is: 6 Last no zero num of (987!) is: 2 Last no zero num of (988!) is: 6 Last no zero num of (989!) is: 4 Last no zero num of (990!) is: 6 Last no zero num of (991!) is: 6 Last no zero num of (992!) is: 2 Last no zero num of (993!) is: 6 Last no zero num of (994!) is: 4 Last no zero num of (995!) is: 8 Last no zero num of (996!) is: 8 Last no zero num of (997!) is: 6 Last no zero num of (998!) is: 8 Last no zero num of (999!) is: 2 Last no zero num of (1000!) is: 2

user: user-7
content: 引用 2 楼 A3872215 的回复:我语文不好 这句没看懂  ：“求n的阶乘最后一位非0数字”  我觉得意思是 因为1到n之间有很多10的倍数，所以n的阶乘肯定是后面一串0，要求的是最低的非0位

user: user-5
content: 直接计算的话,要算出每一个数是2，5 几次幂的倍数  并滤掉 2，5因子 求出不含 2，5因子的个位数 然后根据去掉末尾的0后，2的因子个数，  计算出结果中2因子乘积的个位数，和滤掉 2，5因子后乘积的个位数相乘， 算出个位数。 不过，效率不高

user: user-6
content: int get_last_r(int num) {         int rslt = 1;         while(!(rslt=num%10)) num = num / 10;         return rslt; } 楼主，你这个函数应该是求num的最低位吧。如果是，那么while(!(rslt=num%10)) num = num / 10;算法可疑。你可举例num=100.你的rslt结果是1而不是0.

user: user-5
content: （num3[b %4] *num7[c%3]*num9[d%2] *num2[e%4]） %10

user: user-5
content: 这个，这么算不会出现溢出  计算 排除2，5因子以后，因子序列 中个位是 1，3，7，9 的个数a,b,c,d 然后计算 因子2的比因子5多多少,得到因子2的个数e  剩余因子2的个数e =总的因子2 个数- 总的因子 5 个数 +总的因子10的个数  1）1，3，7，9 个数  参与形成个位数 2）2^k 的倍数中除以2^k后    个位数是 1，3，7，9 个数这些数个位为2 和5结合形成0 或者保留为2的幂，所以其中的 1，3，7，9 参与形成个位数                                         3）5^k 的倍数中除以5^k后，个位数是 1，3，7，9 个数；这些数个位为5 和2结合形成0 所以 1，3，7，9 参与形成个位数                                         4）10^k 的倍数中除以10^k  个位数是 1，3，7，9 个数 //这些数末位为0 所以1，3，7，9 参与形成个位数  结果是 1^a *3^b*7^c*9^d *2^ e =3^b*7^c*9^d *2^ e 的个位数  = num3[b %4] *num7[c%3]*num9[d%2] *num2[e%4]) %10  num3[]={3,9,7,1} num7[]={7,9,1} num9[]={9,1} num2[]={2,4,6,8};    因为： 1的幂序列的个位数1 3的幂序列的个位数3，9，7，1   7的幂序列的个位数7，9，1 9的幂序列的个位数9，1 2的幂序列的个位数 是2，4，8，6.。。。。。  这里，如何防止多统计或者漏统计，是这个算法的核心

user: user-8
content: 2和5相乘就有0了啊, 那不是4!才是最后一个吗

user: user-9
content: 引用 2 楼 A3872215 的回复:我语文不好 这句没看懂  ：“求n的阶乘最后一位非0数字” lz出来解释一下吧

user: user-10
content: 看楼主是用的递归求的阶乘，但是input>30的时候会溢出啊，你确定1~70验证是对的？