这个数很大,就算超大整数,也顶不过n=20,一般在15左右,还是能存放!
#include <stdio.h>
void main()
{
int i, j, n, k;
__int64 sum = 1, sum1=0;
scanf("%d", &n);
j = 1;
for(i = 1; i <= n; i++ )
{
sum = 1;
for (k=0;k<i; k++,j++)
{
sum *=j;
}
sum1 +=sum;
}
printf("sum1=%I64d\n", sum1);
}
另外写了两个大数相乘和相加的函数,这个n可以大到200.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
//字符串逆置,思想:在字符串末尾分别设一指针
//两指针指向的值交换
char *ReverseStr(char *str)
{
char *p, *q, ch;
//去字符串长度
int len = strlen(str) - 1;
//当两指针没有相遇,指针的值就交换
for (p = str, q = str + len; p < q; p++,q--)
{
ch = *q;
*q = *p;
*p = ch;
}
//返回结果
return str;
}
//相乘
char *BigIntMulti(char *s1, char *s2)
{
int len, m = 0; //m字符串的位数
int len1 = strlen(s1);
int len2 = strlen(s2);
int flag = 0;
//取最大字符串长度
len = len1 > len2 ? len1: len2;
//给和字符串分配空间,长度暂时设成len1*len2
char *sum = (char *) malloc((len1 * len2 + 1) * sizeof(char));
//分配的空间初始化为0
memset(sum, '0', (len1 * len2 + 1) * sizeof(char));
//从字符串末尾进行处理
for(int i = len1 - 1; i >= 0; i--)
{
//m是积字符串的下标
m = len1 - 1 - i;
//对s2字符串也是从末尾进行处理,字符串'9'-'0'就成为整数9,所以按照乘法原则进行运算
for(int j = len2 - 1, temp = 0; j >=0; j--)
{
//temp保存 s1[i]*s2[j]的积和进位之和,sum[m]可能不为'0',因为有进位,在之后的while循环之中设置的
temp = (sum[m] - '0') + (s1[i] - '0') * (s2[j] - '0') + flag;
//对temp的结果取余存入积字符串中
sum[m ++] = temp % 10 + '0';
//是否有进位
flag = temp / 10;
}
//最高一位可能有进位
while(flag)
{
//这里把进位加入到积字符串中
sum[m ++] += flag % 10;
//取余
flag /= 10;
}
}
//设置积字符串结束标记
sum[m] = '\0';
//返回结果,以上sum的结果是倒序,所以需要把逆置过来
return ReverseStr(sum);
}
//相加2,用指针处理
char *BigIntPlus(char *s1, char *s2)
{
int len, temp;
int len1 = strlen(s1);
int len2 = strlen(s2);
int flag = 0;
char *sum, *r;
len = len1 > len2 ? len1: len2;
sum = (char *) malloc((len + 2) * sizeof(char));
memset(sum, 0x00, (len + 2) * sizeof(char));
r = sum;
for(char *p = s1 + len1 - 1, *q = s2 + len2 - 1; (p >= s1 || q >= s2); p--, q--, r++)
{
if(p < s1)
temp = (*q - '0') + flag;
else if( q < s2)
temp = (*p - '0') + flag;
else
temp = (*p - '0' + *q - '0') + flag;
//把相加的结果添加到和字符串中
*r = temp % 10 + '0';
//保存相加结果的进位
flag = temp / 10;
}
//处理最后一位进位
if(flag) *r++ = flag % 10 + '0';
*r = '\0';
return ReverseStr(sum);
}
void main()
{
int i, j, n, k;
char sum[MAX] = {"1"}, sum1[MAX] ={"0"};
char tmp[MAX],tmp1[MAX],tmp2[MAX];
scanf("%d", &n);
j = 1;
for(i = 1; i <= n; i++ )
{
strcpy(sum, "1");
for (k = 0; k < i; k++, j++)
{
itoa(j, tmp, 10);
strcpy(tmp1, BigIntMulti(sum, tmp));
strcpy(sum, tmp1);
}
strcpy(tmp2, BigIntPlus(sum1, sum));
strcpy(sum1, tmp2);
}
printf("sum1=%s\n", sum1);
}
温馨提示:内容为网友见解,仅供参考
用C语言编程:sum=1+2*3+4*5*6+7*8*9*10+…… 共n项,n由键盘输入
sum[m] = '\\0';
sum=1+2*3+4*5*6+7*8*9*10+…… 共n项,n由键盘输入。求sum.
public static void main(String[] args) { Scanner in = new Scanner (System.in);System.out.println("输入n:");int n = in.nextInt();long sum = 0;long temp = 1;int number = 1;for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { temp *= ...
c语言使编程:用函数求1*2*3+4*5*6+7*8*9+...100*101*102
long int Sum(void){ long int sum_n=0;for(n=1;n<98;n++){ sum_n +=n*(n+1)*(n+2);} return sum_n;}
怎样编程1+2*3+4*5*6+7*8*9*10,,,到n项的运算呢??帮帮忙吧
sum+=temp;} cout<<sum<<endl;}
C语言计算1*2*3+3*4*5+5*6*7+7*8*9
include<stdio.h> int main(){ int sum=0;;for(int i=1;i<=7;i=i+2)sum+=i*(i+1)*(i+2);printf("%d\\n",sum);}
...+4!+5!+6!+7!+8!+9!+10!,并把结果写入D盘,用C语言编写 请各位高手帮...
int main(){ long jiechen(int n);FILE *p;int i;long sum=0;for(i=1;i<=10;i++)sum=sum+jiechen(i);p=fopen("d:\\\\qiuhe.txt","w+");fprintf(p,"%ld",sum);fclose(p);return 0;} long jiechen(int n){ int i;long sum=1;for(i=n;i>=1;i--)sum=sum*i;return ...
用C语言编写程序1+2*2+3*3*3+4*4*4*4+……N^N
int main(){ double result=0; printf("input a num: "); int num=0; scanf_s("%d",&num); fflush(stdin); for(int i=1;i<=num;i++){ int temp=1; for(int j=1;j<=i;j++){ temp*=i; } result+=temp; } printf("result= %lf",result); getchar();} ...
c语言sum=1+2+3+4+…+n写出程序
\/* Note:Your choice is C IDE *\/ #include "stdio.h" main() { int sum=0,n=100,i; for(i=1;i<=n;i++) sum+=i; printf("sum=%d\\n",sum); }
c语言怎么编写程序计算:1+22+333+4444+...的前n项的值,n由用户输入(n...
include<math.h> main(){ int i,n;long j,k,sum=0;printf("input the n:\\n");scanf("%d",&n); \/*n为项数*\/ for(i=1;i<=n;i++){ k=0;for(j=i;j>0;j--)k+=i*pow(10,j-1);printf("%ld",k); \/*打印输出情况*\/ if(i!=n) printf("+");else printf("=");...
C语言编程 计算1!+2!+3!+...+n!的值,其中n的值通过键盘输入
1、C程序 include<stdio.h> void main(){ int i,a;float n=1,sum=0;scanf("%d",&a);for(i=1;i<=a;i++){ n=n*i;sum=sum+n;} printf("1!+2!+3!+...+n!=%.0f",sum);} 2、C++程序 include<iostream.h> void main(){ int i;float n=1,sum=0;for(i=1;i<=20...