50道C/C++编程练习题 复习必备(11-20)

C/C++编程练习题 (11-20)

  • 11. 求 e=1 + 1/1! + 1/2! + 1/3! + ...
  • 12. 求PI值,PI/4 = 1 - 1/3 + 1/5 - 1/7 + ...
  • 13. 求PI值,PI/2 = 1 + 1/3 + 1/3*2/5 + 1/3*2/5*3/7 + ...
  • 14. 输入20个数,统计其中正数、负数和零的个数。
  • 15. 输入若干个整数,计算其中的奇数之和与偶数之和,假设输入0表示结束
  • 16. 写一函数,计算x的y次方(假设x、y都为正整数)
  • 17. 求水仙花数(一个三位数,其各位数字立方和等于该数字本身)
  • 18. 编写一个函数,确定一个整数是否为完全数(一个数,等于他的因子之和)。用这个函数确定和打印1到1000之间的所有完全数
  • 19. 写一函数,求斐波那契数列的第n项
  • 20. 写一个函数,取一个整数值并返回将此整数的各数字反序的数值

使用C/C++两种语言完成50道题目,体会两种语言之间的不同。编译环境都是在VS2019,完成时间2020.06.21-2020.06.22 。由于C++ 对C的兼容性,部分main()函数没有写return 0 也可以执行成功,但是不提倡,最好加上,否则其他编译器可能报错。C语言不需要严格对齐,因此不对CSDN 的格式做过多的纠正。

11. 求 e=1 + 1/1! + 1/2! + 1/3! + …

C

#include
int main()
{
 int i;
 double e, t;
 i = 1, e = 1, t = 1;
 while (t >= 1e-7)
 {
  t = t / i;
  i += 1;
  e += t;
 }
 printf("%5lf",e);
}

C++

#include
using namespace std;
int main() 
{
 int i;
 double e,t;
 i = 1, e = 1, t = 1;
 while (t>=1e-7)
 {
  t = t / i;
  i += 1;
  e += t;
 }
 cout << e;
}

12. 求PI值,PI/4 = 1 - 1/3 + 1/5 - 1/7 + …

C

#include
int main()
{
 double i=1;
 int j = 1;
 double PI, t;
 PI = 1.00; 
 t = 1.00;
 while (t>1e-7)
 {
  i += 2;
  t = 1 / i;
  if (j%2==1)
      PI -= t;
  else
      PI += t;
  j += 1;
 }
 printf("PI的值为%5lf", 4*PI);
 return 0;
}

C++

#include
using namespace std;
int main()
{
 double i=1;
 int j = 1;
 double PI, t;
 PI = 1.00;
 t = 1.00;
 while (t>1e-7)
 {
  i += 2;
  t = 1 / i;
  if (j%2==1)
      PI -= t;
  else
      PI += t;
  j += 1;
 }
 cout << "PI的值为" << 4 * PI;
 return 0;
}

13. 求PI值,PI/2 = 1 + 1/3 + 1/32/5 + 1/32/5*3/7 + …

C

#include
int main()
{
 double PI = 1.0;
 double i = 1.0, j = 1.0;
 double t = 1.0;
 while (t > 1e-17)
 {
  i += 2;
  t = t * j / i;
  PI += t;
  j = j + 1;
 }
 printf("PI的值是%If", 2*PI);
}

C++

#include
using namespace std;
int main()
{
 double PI = 1.0;
 double i = 1.0,j=1.0;
 double t = 1.0;
 while(t>1e-17)
 {
  i += 2;
  t = t * j / i;
  PI += t;
  j = j + 1;
 }
 cout << "PI的值是" << PI*2;
}

14. 输入20个数,统计其中正数、负数和零的个数。

C

#include
int main()
{
 int a;
 int count1 = 0, count2 = 0, count3 = 0;
 printf("请输入20个数");
 for (int i = 0; i < 20; i++)
 {
  scanf_s("%d", &a);
  if (a > 0)  count1++;
  else if (a == 0) count2++;
  else count3++;
 }
 printf("正数的个数是:%d\n", count1);
 printf("零的个数是:%d\n", count2);
 printf("负数的个数是:%d\n", count3);
 return 0;
}

C++

#include
using namespace std;
int main()
{ 
 int a;
 int count1=0, count2=0, count3=0;
 cout << "请输入20个数" << endl;
 for(int i=0;i<20;i++)
 {
  cin >> a;
  if (a > 0)  count1++;
  else if (a == 0) count2++;
  else count3++;
 }
 cout << "正数的个数是:" << count1<<endl;
 cout << "零的个数是:" << count2 << endl;
 cout << "负数的个数是:" << count3 << endl;
}

15. 输入若干个整数,计算其中的奇数之和与偶数之和,假设输入0表示结束

C

#include
int main()
{
 int a, n = 0, m = 0;
 printf("输入若干个整数,输入0表示结束\n");
 scanf_s("%d",&a);
 while (a != 0)
 {
  if (a % 2 == 0) n += a;
  else m += a;
  scanf_s("%d", &a);
 }
 printf("奇数的和为%d\n",m);
 printf("偶数的和为%d", n);
}

C++

#include
using namespace std;
int main()
{
 int a, n = 0, m = 0;
 cout<<"输入若干个整数,输入0表示结束"<<endl;
 cin >> a;
 while (a != 0)
 {
  if (a % 2 == 0) n += a;
  else m += a;
  cin >> a;
 }
 cout << n << " " << m;
}

16. 写一函数,计算x的y次方(假设x、y都为正整数)

C

#include
int pownew(int, int);
int main()
{
 int x = 4, y = 3;
 int z;
 z = pownew(x, y);
    printf("%d",z);
 return 0;
}
int pownew(int x, int y)
{
 int z = 1;
 for (int i = 0; i < y; i++)
     z = z * x;
 return z;
}

C++

#include 
using namespace std;
int pownew(int,int);
int main()
{
 int x = 4, y = 5;
 int z;
 z = pownew(x, y);
 cout << z;
}
int pownew(int x, int y)
{
 int z=1;
 for (int i = 0; i < y; i++)
  z = z * x;
 return z;
}

17. 求水仙花数(一个三位数,其各位数字立方和等于该数字本身)

C

#include
int main()
{
int i = 1, j = 0, k = 0, a = 0;
for (int i = 1; i < 10; i++)
   for (int j = 0; j < 10; j++)
       for (int k = 0; k < 10; k++)
           if ((i * i * i + j * j * j + k * k * k) == (100 * i + 10 * j + k))
               a = a + 1;
printf("%d",a) ;
}

C++

#include
using namespace std;
int main()
{
int i = 1 ,j = 0, k = 0,a=0;
for (int i=1;i<10;i++)
    for (int j=0;j<10;j++)
        for (int k=0;k<10;k++)
        {
           if((i*i*i+j*j*j+k*k*k)==(100*i+10*j+k))
           {
               a = a + 1;
               cout << i<<j<< k<<endl;
           }
       }
cout << a;
}

18. 编写一个函数,确定一个整数是否为完全数(一个数,等于他的因子之和)。用这个函数确定和打印1到1000之间的所有完全数

C

#include
int perfect();
int perfect(int n)
{
 int i, s = 1;
 for (i = 2; i <= n / 2; i++)
     if (n % i == 0) s = s + i;
 if (s == n) return 1;
 else return 0;
}
int main()
{
 int n;
 for (n = 2; n <= 1000; n++)
    if (perfect(n))
       printf("%d\n",n);
 return 0;
}

C++

#include
using namespace std;
int perfect();
int perfect(int n)
{
 int i, s = 1;
 for (i = 2; i <= n / 2; i++)
     if (n % i == 0) s = s + i;
 if (s == n) return 1;
 else return 0;
}
int main()
{
int n;
for (n = 2; n <= 1000; n++)
   if (perfect(n))
     cout << n << endl;
}

19. 写一函数,求斐波那契数列的第n项

C

#include
int fib(int);
int main()
{
 int n = 6;
 int k;
 k = fib(n);
 printf("%d", k);
 return 0;
}
int fib(int n)
{
 if (n == 1 || n == 2)
     return 1;
 int f1 = 1; int f2 = 1;
 int fn = 0;
 for (int i = 3; i <= n; i++) {
     fn = f1 + f2;
     f1 = f2;
     f2 = fn;
 }
 return fn;
}

C++

#include
using namespace std;
int f(int);
int main() 
{
 int  k;
 int n=6;
 k = f(n);
 cout << k;
}
int f(int n)
{
 if (n == 1) return 1;
 if (n == 2)  return 1;
 else
     return  f(n - 1) + f(n - 2);
}

20. 写一个函数,取一个整数值并返回将此整数的各数字反序的数值

C

#include
int revese(int);
int main()
{
 int k = 345;
 int z;
 z = revese(k);
 printf("%d",z);
}
int revese(int k)
{
 int s = 0;
 int temp = 0;
 while (k > 0)
 {
  temp = k % 10;
  k = k / 10;
  s = s * 10 + temp;
 }
 return s;
}

C++

#include
using namespace std;
int revese(int);
int main()
{
 int k = 345;
 int z;
 z = revese(k);
 cout << z;
}
int revese(int k)
{
 int s = 0;
 int temp = 0;
 while(k>0)
 {
  temp = k % 10;
  k = k / 10;
  s = s * 10 + temp;
 }
 return s;
}

你可能感兴趣的:(C语言)