回 帖 发 新 帖 刷新版面

主题:[原创]一套C++练习题(含答案)供大家参考!

今天帮别人做的,有些题目比较有代表性,在此连答案全部贴出来,给需要的朋友做参考。全部程序在VISUAL C++ 6.0环境下均调试通过。如果您对其中某个程序有更好的解答方法,欢迎跟贴交流。因为时间有限,没有写注释,如果您哪段程序不理解,也可以跟贴交流。

一、    选择题:
1、C++源程序文件的扩展名是:A
A) .CPP        B) .C        C) .DLL         D) .EXE

2、将小写字母n赋值给字符变量one_char,正确的操作是:C
A) one_char = ‘\n’;      B) one_char = “n”;
C) one_char = 110;      D) one_char = ‘N’;

3、整型变量i定义后赋初值的结果是:B
   int i=2.8*6;
A) 12     B) 16     C) 17    D) 18

4、下列表达式的值为false的是:C
A) 1<3 && 5<7      B) !(2>4)     C) 3&0&&1    D) !(5<8)||(2<8)

5、设int a=10, b=11, c=12;表达式(a+b)<c&&b==c的值是:B
A) 2       B) 0       C) –2      D) 1

6、下列程序执行完后,x的值是:C
  int x=0;
  for (int k=0;k<90; k++)
if (k)  x++;
A) 0         B) 30      C) 89     D) 90

7、下列程序段循环次数是:A
  int x = -10;
  while (++x)  cout<<x<<endl;
A) 9        B) 10      C) 11         D) 无限

8、表示“大于10而小于20的数“,正确的是:D
A) 10<x<20   B) x>10||x<20   C) x>10&x<20     D) !(x<=10||x>=20)

9、若整型变量x=2,则表达式x<<2的结果是:D
A) 2        B) 4     C) 6      D) 8

10、设a=1, b=2,则(a++)+b与a+++b这两个表达式的值分别为:A
A) 3, 3    B) 3, 4     C) 4, 3    D) 4,4

二、程序填空题。
1、下列程序计算1000以内能被3整除的自然数之和。
#include <iostream.h>
void main( )
{   int x=1, sum;
sum=0_______;
while (true)
{   if (x>1000)  break;
    if (x%3==0) sum+=x;
    x++;
}
cout<<sum<<endl;
}
三、假定输入10个整数:32,64,53,87,54,32,98,56,98,83。下列程序的输出结果是?
#include <iostream.h>
void main( )
{   int a,b,c,x;
   a=b=c=0;
   for (int k=0; k<10; k++)
   {   cin>>x;
switch(x%3)
{   case 0:  a+=x; break;
    case 1:  b+=x; break;
    case 2:  c+=x; break;
}
   }
cout<<a<<”,”<<b<<”,”<<c<<endl;
}
结果:
141,64,452


四、写出下列程序运行结果。
#include <iostream.h>
void main( )
{   int j,k;
for (j=5; j>0; j--)
{  for (k=j; k>0; k--)
     cout<<”*”;
  cout<<endl;
}
}
结果:
*****
****
***
**
*

五、写出下列程序的运行结果。
#include <iostream.h>
#include <iomanip.h>
void main()
{   
cout<<”x_width=”<<cout.width()<<endl;
cout<<”x_fill=”<<cout.fill()<<endl;
cout<<”x_precision=”<<cout.precision()<<endl;
cout<<123<<” ”<<123.45678<<endl;
cout<<”***x_width=10,x_fill=&,x_precision=8***”<<endl;
cout.width(10);
cout.fill(‘&’);
cout.precision(8);
cout<<123<<” “<<123.45678<<endl;
cout.setf(ios::left);
cout<<123<<” “<<123.45678<<endl;
cout.width(10);
cout<<123<<” “<<123.45678<<endl;
cout<<”x_width=”<<cout.width()<<endl;
cout<<”x_fill=”<<cout.fill()<<endl;
cout<<”x_precision=”<<cout.precision()<<endl;
}
结果:
x_width=0
x_fill=
x_precision=6
123 123.457
***x_width=10,x_fill=&,x_pre
&&&&&&&123 123.45678
123 123.45678
123&&&&&&& 123.45678
x_width=0
x_fill=&
x_precision=8


六、程序题。
1、    编写程序,求解方程ax2+bx+c=0的根。
#include <iostream>
#include <cmath>
using namespace std;
void main()
{
    int a,b,c;
    float x1,x2,z;
    cin>>a>>b>>c;
    z=b*b-4*a*c;
    if(z>0)
    {
        x1=((-b)+sqrt(z))/(2*a);
        x2=((-b)-sqrt(z))/(2*a);
        cout<<"The result: x1="<<x1<<"  x2="<<x2<<endl;
    }
    else
        if(z==0)
        {
            x1=-b/(2*a);
            cout<<"The result: x1="<<x1<<endl;
        }
        else
            cout<<"no result";
}


2、    编写程序输出所有的水仙花数。所谓水仙花数是指一个三位数,其各位数的立方和等于该数。例如:153=13+53+33。
#include <iostream>
using namespace std;
void main()
{
    int a,b,c;
    for(int i=100;i<=999;i++)
    {
        a=i/100;
        b=i%100/10;
        c=i%10;
        if(a*a*a+b*b*b+c*c*c==i)
            cout<<i<<endl;
    }
}


3、    编写程序,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值。
#include <iostream>
using namespace std;
void main()
{
    int n,s,sum=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        s=0;
        for(int j=1;j<=i;j++)
            s+=j;
        sum+=s;
    }
    cout<<sum<<endl;
}

4、某百货公司为了促销,采用购物打折的办法。
(1)    在1000元以上者,按九五折优惠;
(2)    在2000元以上者,按九折优惠;
(3)    在3000元以上者,按八五折优惠;
(4)    在5000元以上者,按八折优惠。
编写程序,输入购物款数,计算并输出优惠价。(要求用switch语句编写)
#include <iostream>
using namespace std;
void main()
{
    int cost;
    double price,d;
    cin>>cost;
    if(cost>5000)
        d=0.8;
    switch(cost/1000)
    {
    case 0:d=1.0;break;
    case 1:d=0.95;break;
    case 2:d=0.9;break;
    case 3:case 4:d=0.85;break;
    }
    price=cost*d;
    cout<<price<<endl;
}


4、    将一张一元纸币兑换成一分、二分和五分的硬币,假定每种至少一枚,计算共有多少种兑换法并打印出各种兑换法。
#include <iostream>
using namespace std;
void main()
{
    int a,b,c,count=0;
    for(a=1;a<100;a++)
        for(b=1;b<100;b++)
            for(c=1;c<100;c++)
                if(c*5+b*2+a==100)
                {
                    count++;
                    cout<<"1分:"<<a<<",2分:"<<b<<",5分:"<<c<<endl;
                }
    cout<<"Counts:"<<count<<endl;
}


6、“同构数”是指这样的整数:它恰好出现在其平方数的右端。如:376*376=141376。请找出10000以内的全部“同构数”。
#include <iostream>
using namespace std;
void main()
{
    for(int i=1;i<10000;i++)
    {
    if(i<10&&i==i*i%10)
        cout<<i<<endl;
    else
        if(i<100&&i==i*i%100)
            cout<<i<<endl;
        else
            if(i<1000&&i==i*i%1000)
                cout<<i<<endl;
            else
                if(i==i*i%10000)
                    cout<<i<<endl;
    }
}

回复列表 (共67个回复)

31 楼

6楼的请分析下算法的时间复杂度,为什么你的那个是效率最高的
我想分析出前面几种做法跟你做法的时间复杂度是最能说名问题的

32 楼

学学级数就好了:)

33 楼

#include "stdafx.h"
#include "iostream.h"
float add(int n)//计算1+(1+2)+(1+2+3)+`````````````````
{
     int  a;
     if (n==1)
         a=1  ;
     else
         a=add(n-1)+(1+n)*n/2;
     return   a;
}
main()
{  int  n;
  cout<<"输入数据n\n" ;
  cin>>n ;
  cout<<add(n) ;
}
                     新手哈,多指点

34 楼

遇到这类问题,我觉得应该首先考虑的是    数  学    公式。而不是考虑什么程序本身的循环等等。6楼的 酷

35 楼

#include<stdio.h>
#include<conio.h>
int main()
{
  printf("                          \n");
  printf(" upupupupup    upupupupupup    \n");
  printf("         up    up        up        \n");
  printf("         up    upupupupupup                  \n");
  printf("  pupupupup  upupupupupupupup    \n");
  printf("  up         up     up     up   \n");
  printf("  up         up     up     up   \n");
  printf("  up         up     up     up  \n");        
  printf("  upupupupup upupupupupupupup   \n");
  printf("          up        up           \n");
  printf("          up        up          \n");
  printf("    upupupup        up   up   \n");
  printf("        upup  upupupupupupup  \n");
  printf("          up               up   \n");
  getch();
  return 0;
}

36 楼

#include<stdio.h>
#include<conio.h>
int main()
{
  printf("                          \n");
  printf(" upupupupup    upupupupupup    \n");
  printf("         up    up        up        \n");
  printf("         up    upupupupupup                  \n");
  printf("  pupupupup  upupupupupupupup    \n");
  printf("  up         up     up     up   \n");
  printf("  up         up     up     up   \n");
  printf("  up         up     up     up  \n");        
  printf("  upupupupup upupupupupupupup   \n");
  printf("          up        up           \n");
  printf("          up        up          \n");
  printf("    upupupup        up   up   \n");
  printf("        upup  upupupupupupup  \n");
  printf("          up               up   \n");
  getch();
  return 0;
}

37 楼

有个问题
int i=2.8*6;
这句虽然如此,但如果
这样编程的话会有警告的,VC6.0下的,这样不是很好的,我觉得不应该出这样的题目!!!

38 楼

我现在在做一个课题,
需要用到用C++来编写关于逐步回归的程序
希望各位大老能给我点帮助,
谁有程序给我发过来啊,
或者谁有时间给我编一下 ,
我将 不胜感激!!

39 楼

我现在在做一个课题,
需要用到用C++来编写关于逐步回归的程序
希望各位大老能给我点帮助,
谁有程序给我发过来啊,
或者谁有时间给我编一下 ,
我将 不胜感激!!

40 楼

我现在在做一个课题,
需要用到用C++来编写关于逐步回归的程序
希望各位大老能给我点帮助,
谁有程序给我发过来啊,
或者谁有时间给我编一下 ,
我将 不胜感激!!
我的联系方式
QQ:75855136
电话:029-85384828

我来回复

您尚未登录,请登录后再回复。点此登录或注册