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

C/C++编程练习题 (1-10)

  • 1. 输入3个数,求最大值
  • 2. 编程序,求方程ax2+bx+c=0的根
  • 3. 输入一个成绩,打印相应的等级
  • 4. 输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边
  • 5. 输入20个数,求其最大、最小和平均值
  • 6. 输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值
  • 7. 输入若干个数,输入-999表示结束,求平均值及最大值
  • 8. 求和 s=1X1 + 2X2 + 3X3 +...+ 100X100
  • 9. 印度国王的奖励,求和 2的0次方加到2的63次方
  • 10. 求和 s=1! + 2! + 3! +...+ 10!

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

1. 输入3个数,求最大值

C

#include
int main() 
{
 int a, b, c,m;
 scanf_s("%d%d%d", &a, &b,&c);
 m = a;
 if (b > m)
     m = b;
 if (c > m)
     m = c;
 printf("max number is %d", m);
 return 0;
 }

C++

#include
using namespace std;
int main() {
    int a, b, c, m;
    cin >> a >> b >> c;
    cout << a << endl;
    cout << b << endl;
    cout << "the meaning of c =" << c << endl;
    m = a;
    if (b > m)
        m = b;
    if (c > m)
         m = c;
    cout << m<<endl;
}

2. 编程序,求方程ax2+bx+c=0的根

C

#include
#include
int main()
{
 double a, b, c, d, x1, x2;
 printf( "请输入一元二次方程a,b,c对应的值,用空格隔开\n");
 scanf_s("%5lf%5lf%5lf", &a, &b, &c);
 printf ( "a=%5lf,b=%5lf,c=%5lf\n",a,b,c);
 if (a == 0 & b == 0)
 {
     printf( "方程无解\n");
     return 0;
 }
 if (a == 0)
 {
      printf("x的值是%5lf\n", -c / b );
      return 0;
 }
 d = b * b - 4 * a * c;
 if (d < 0)
 {
      printf("方程无解\n");
      return 0;
 }
 if (d == 0)
{
     x1 = (-b) / (2 * a);
     printf("x1=x2,他们的值是%5lf\n", x1);
     return 0;
}
 if (d > 0)
 {
     x1 = (-b + sqrt(d)) / (2 * a);
     x2 = (-b - sqrt(d)) / (2 * a);
     printf("x1,x2,他们的值是x1=%5lf,x2=%5lf\n", x1,x2);
     return 0;
 }
}

C++

#include
using namespace std;
#include
int main()
{
double a, b, c,d,x1,x2;
cout << "请输入一元二次方程a,b,c对应的值,用空格隔开"<<endl;
cin >> a >>b >> c;
cout << "a=" << a << ",b=" << b << ",c=" << c << endl;
if (a == 0 and b == 0)
{
    cout << "方程无解" << endl;
    return 0;
}
if (a == 0)
{
     cout << "x的值是" << -c / b << endl;
     return 0;
 }
 d = b * b - 4 * a*c;
 if (d<0)
 {
      cout << "方程无解" << endl;
      return 0;
  }
 if (d==0)
 {
      x1 = (-b) / (2 * a);
      cout << "x1=x2=" <<x1 << endl;
      return 0;
 }
 if(d>0)
 {
      x1 = (-b + sqrt(d)) / (2 * a);
      x2 = (-b - sqrt(d)) / (2 * a);
      cout << "x1=" << x1<< ",x2="<<x2 << endl;
      return 0;
 }
}

3. 输入一个成绩,打印相应的等级

C

#include
int main()
{
 printf("请输入您的成绩,回车结束\n");
 int grade;
 scanf_s("%d", &grade);
 if (grade >=90) printf("您的等级是A");
 else if (grade >= 60) printf("您的等级是B");
 else printf("您的等级是C");
}

C++

#include
using namespace std;
int main()
{
 cout << "请输入您的成绩,分数在0-100 之间的整数,回车结束"<<endl;
 int grade;
 cin >> grade;
 if (grade >= 90) cout << "您的等级是A";
 else if (grade >= 60) cout << "您的等级是B";
 else  cout << "您的等级是C";
}

4. 输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边

C

#include
int main()
{
double a, b, c;
printf( "请输入三角形的三条边,以空格分隔,回车结束\n");
scanf_s("%5lf%5lf%5lf", &a, &b, &c);
if ((a + b) > c and (a + c) > b and  (b + c) > a)
    printf( "可以组成三角形");
else
    printf( "不可以组成三角形");
}

C++

#include
using namespace std;
int main()
{
 double a, b, c;
 cout << "请输入三角形的三条边,以空格分隔,回车结束"<<endl;
 cin >> a >> b >> c;
 if ((a + b) > c and (a + c) > b and  (b + c) > a)
     cout << "可以组成三角形";
 else
     cout << "不可以组成三角形";
}

5. 输入20个数,求其最大、最小和平均值

C

#include
int main() 
{
 double total;
 int minnumber, maxnumber, a;
 printf( "请输入20个数字,以回车结束" );
 scanf_s("%d",&a);
 total = minnumber = maxnumber = a;
 for (int i = 1; i < 20; i++)
 {
      scanf_s("%d", &a);
      if (maxnumber < a)
 	 {
  	 maxnumber = a;
  	 }
      if (minnumber > a)
          minnumber = a;
      total = total + a;
 }
 printf("最大值是:%d\n",maxnumber);
 printf("最小值是:%d\n", minnumber);
 printf("平均值是%3lf",total/20);
}

C++

#include
using namespace std;
int main()
{
    double a, minnumber, maxnumber, total;
    cout << "请输入20个数字,以回车结束" << endl;
    cin >> a;
    total = minnumber = maxnumber = a;
    for(int i=1; i<20;i++)
    {
        cin >> a;
        if (maxnumber<a)
        {
             maxnumber = a;
        }
        if (minnumber > a)
        minnumber = a;
        total = total + a;
    }
    cout << "最大值是:" << maxnumber<<endl;
    cout << "最小值是:" << minnumber << endl;
    cout << "平均值是:" << total / 20;
}

6. 输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值

C

#include
int main()
{
 int n, a, maxnumber, minnumber, total;
 printf("请输入要计算数字的个数 和数字,用空格分开\n");
 scanf_s("%d", &n);
 scanf_s("%d" ,& a);
 maxnumber = minnumber = total = a;
 for (int i = 1; i < n; i++)
 {
     scanf_s("%d", &a);
     if (a > maxnumber)
         maxnumber = a;
     if (a < minnumber)
         minnumber = a;
     total += a;
 }
 float d;
 d = total / n;
 printf( "平均值为%3lf\n",d);
 printf("最大值为%d\n", maxnumber);
 printf("最小值为%d\n", minnumber);
}

C++

#include
using namespace std;
int main()
{
 int n, a, maxnumber, minnumber,total;
 cout << "请输入要计算数字的个数" << endl;
 cin >> n;
 cin >> a;
 maxnumber = minnumber = total = a;
 for(int i=1;i<n;i++)
 {
     cin >> a;
     if (a > maxnumber)
         maxnumber = a;
     if (a < minnumber)
         minnumber = a;
     total += a;
 }
 cout << "最大值为" << maxnumber << endl;
 cout << "最小值为" << minnumber << endl;
 double d;
 d = total / n;
 cout << "平均值为" << d<<endl;
}

7. 输入若干个数,输入-999表示结束,求平均值及最大值

C

#include
int main()
{
 int a, maxnumber, total;
 int n = 1;
 printf( "输入若干个数,输入 -999表示结束" );
 scanf_s("%d",&a);
 maxnumber = a;
 total = 0;
 while (a != -999)
{
     if (a > maxnumber)
         maxnumber = a;
     total += a;
     n = n + 1;
     scanf_s("%d", &a);
}
 printf("最大值为:%d", maxnumber);
 float d = total / n;
 printf( "平均值为%3lf",d);
 return 0;
}

C++`

using namespace std;
int main()
{
 int a,maxnumber, total;
 int n = 1;
 cout << "输入若干个数,输入 -999表示结束"<<endl;
 cin >> a;
 maxnumber = a;
    total = 0;
 while(a!=-999)
 {
     if (a > maxnumber)
         maxnumber = a;
     total += a;
     cout << "total=" << total << endl;
     n = n + 1;
     cin >> a;
 }
 cout << "最大值为:" << maxnumber<<endl;
 cout << "平均值为:" << total / n;
 return 0;
}

8. 求和 s=1X1 + 2X2 + 3X3 +…+ 100X100

C

#include
int main()
{
 int sum = 0, temp;
 for (int i = 1; i <= 100; i++)
 {
  temp = i * i;
  sum += temp;
 }
 printf("1-100平方的和为%d",sum);
}

C++

#include
using namespace std;
int main() 
{
 int sum=0,temp;
 for (int i = 1; i <= 100; i++) 
 {
  temp = i * i;
  sum += temp;
 }
 cout << "1-100平方的和为" << sum;
 return 0;
}

9. 印度国王的奖励,求和 2的0次方加到2的63次方

C

#include
int main()
{
 double sum = 1, temp = 1;
 for (int i = 1; i < 64; i++)
 {
  temp = temp * 2;
  sum = sum + temp;
 }
 printf("%lf", sum);
 return 0;
}

C++

#include
using namespace std;
int main()
{
 double sum = 1,temp=1;
 for(int i=1;i<64;i++ )
 {
  temp = temp * 2;
  sum = sum + temp;
 }
 cout << sum << endl;
 return 0;
}

10. 求和 s=1! + 2! + 3! +…+ 10!

C

#include
int main()
{
 double sum = 1, temp = 1;
 for (int i = 1; i < 64; i++)
 {
  temp = temp * 2;
  sum = sum + temp;
 }
 printf("%9lf", sum);
 return 0;
}

C++

#include
using namespace std;
int main()
{
 double sum = 1,temp=1;
 for(int i=1;i<64;i++ )
 {
  temp = temp * 2;
  sum = sum + temp;
 }
 cout << sum << endl;
 return 0;
}

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