对1-1⼀2+1⼀3-1⼀4......1⼀n进行C语言编程

2024-12-29 02:26:47
推荐回答(4个)
回答1:

这个数很大,就算超大整数,也顶不过n=20,一般在15左右,还是能存放!

#include

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{
sum *=j;
}
sum1 +=sum;
}

printf("sum1=%I64d\n", sum1);
}

另外写了两个大数相乘和相加的函数,这个n可以大到200.

#include
#include
#include
#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] = , sum1[MAX] =;
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);
}

回答2:

#include
#include
void main()
{
int i,n;float s;
printf("请输入整数:\n");
scanf("%d",&n);
s=0;
for (i=1;i<=n;i++)
s=s+pow(-1,i+1)/i;
printf("%f\n",s);
}
注:要求a的b次方,就用pow(a,b)即可。

回答3:

#include
viod main()
{
float sum=0.0;
int i,n,
float a;
a=-1;
printf("sum 1-1/2+1/3....");
scanf("%d",&n);
for(i=1;i<=n;i++)
{a=-a;
sum=sum+a/i;
}
printf("sum=%f",sum);
}

回答4:

#include "stdio.h"
main()
{ float one,result=0;
float m=1;int n,i;
printf("input n:");
scanf("%d",&n);
for (i=0;i {
if((i+1)%2==0)
one=0-1/m;
else
one=1/m;
result=result+one;
m++;
}
printf("result:%6.5f",result);
getch(); /*win_tc*/
}