因此,为了得到精确的结果,必须自己实现乘法运算。下面是我作出的一个解决方案。
---------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 1024
/* 打印串整数 */
void sint_print(unsigned char *a)
{
unsigned char *ptr = a
while (*ptr != 0xff)
{
ptr++
}
while (ptr >a)
{
ptr--
printf("%c", '0' + *ptr)
}
printf("\n")
}
/* 克隆串肢段整数 */
void sint_colon(unsigned char *a, unsigned char *b)
{
unsigned char *ptr1 = a
unsigned char *ptr2 = b
while (*ptr1 != 0xff)
{
*ptr2++ = *ptr1++
}
*ptr2 = 0xff
}
/* 赋值串整数 */
void sint_assign(unsigned char *a, int val)
{
unsigned char *ptr = a
if (val == 0)
{
*ptr++ = 0x00
}
else
{
while (val >0)
{
int digit = val % 10
*ptr++ = (unsigned char)digit
val /= 10
}
}
*ptr = 0xff
}
/* 串整数相乘 */
void sint_multip(unsigned char *a, unsigned char *b, unsigned char *c)
{
unsigned char *ptra = a
unsigned char *ptrb = b
unsigned char *ptrc = c
*ptrc = 0xff
ptra = a
while (*ptra != 0xff)
{
ptrb = b
while (*ptrb != 0xff)
{
ptrc = c + (ptra - a) + (ptrb - b)
if (*ptrc == 0xff)
{
*ptrc = (*ptra) * (*ptrb)
*(ptrc+1) = 0xff
}
else
{
*ptrc += (*ptra) * (*ptrb)
}
if (*ptrc >9)
{
if (*(ptrc+1) == 0xff)
{
*(ptrc+1) = (*ptrc)/10
*(ptrc+2) = 0xff
}
else
{
*(ptrc+1) += (*ptrc)/10
}
*ptrc = (*ptrc)%10
}
ptrb++
}
ptra++
}
}
int main(int argc, char **argv)
{
unsigned char a[BUFLEN]
unsigned char b[BUFLEN]
unsigned char c[BUFLEN]
sint_assign(a, 1)
for (int i = 1i <= 100i++)
{
sint_assign(b, i)
sint_multip(a, b, c)
sint_colon(c, a)
}
sint_print(a)
return 0
}
----------------------------------------------------------
运行结果为:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
//测试已通过, November 27 ,2914 by Season#include<stdio.h>
int main(){
int i=1
double sum=1
while(i<=100)
局穗sum*=i++
printf("\nWhile for 1*2*..*100=%ld",sum)
sum=1
for(i=1i<=100i++)
sum*=i
printf("\nFor for 隐饥1*2*..*100=%ld",sum)
sum=1
i=1
do{
sum*=i++
}while(i<=100)
printf("\nDo...While for 桐携卜1*2*..*100=%ld",sum)
return 0
}
不需要具体的数值,2乘到100就是1乘到100也就是100!如果100!都不举正局知道是什么我也没什么正让好说了,
数字+! 叫清盯做阶乘自己上网查是什么东西!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)