超急(仅限未来12小时)!!!C++的题目!!!

来源:百度知道 编辑:UC知道 时间:2024/07/03 04:27:37
题目1:编程计算PAI=4-4/3+4/5-4/7+4/9.........,并回答程序要计算多少项才 能得到3.14,3.141,3.1415,3.14159。
请问以下程序哪里有错?
#include <stdio.h>
#include <math.h>
double p, d1, d2, s, ep;
long c; int k;
void main()
{
for (k=1, ep=100.0; k<=4; k++, ep*=10.0)
{
p=3.1415926;
d1=4.0; d2=1.0; c=0; s=0;
do
{ if (c%2==0)
s+=d1/d2;
else s-=d1/d2;
c++; d2+=2.0;
}
while ((int)(s*ep)-(int)(p*ep)!=0||fabs(s-p)>=0.5/ep);
printf("p=%f, s=%f, c=%ld\n", p, s, c);
scanf("%*c);
}
}

题目2:回文整数是指正读和反读相同的整数,编程,输入一个整数,判断它是不 是回文整数。
请问以下程序哪里有错?
#include <stdio.h>
void main ()
{
int x,s,a;

printf("请输入整数x.\n");
scanf("%d",&x);
a=x
while (x)
{
s=s*10+x%10; x/=10;
}

if (a==s)
printf("此整数是回文数.\n");

我刚看到,以下是我把你的改了一下,也不知道对你对,你参考一下吧!
一.
#include <stdio.h>
#include <math.h>
double p, d1, d2, s, ep;
long c; int k;
int a;
void main()
{
for (k=1, ep=100.0; k<=4; k++, ep*=10.0)
{
p=3.1415926;
d1=4.0; d2=1.0; c=0; s=0.0;
do
{ if (c%2==0)
s+=d1/d2;
else s-=d1/d2;
c++; d2+=2.0;
}
while ((int)(s*ep)-(int)(p*ep)!=0||fabs(s-p)>=0.5/ep);
a=(int)(s*ep);
printf("p=%f, s=%f, c=%ld\n", p, (float)a/ep, c);
scanf("%*c");
}
}
二.

#include <stdio.h>
void main ()
{
int x,s=0,a;

printf("请输入整数x.\n");
scanf("%d",&x);
a=x;
while (x)
{
s=s*10+x%10; x/=10;
}

if (a==s)
printf("此整数是回文数.\n");
else
printf("此整数不是回文数.\n");
}
三.

#include <stdi