金山WPS2013C++试卷B -- 编程实践

1.请实现这么一个函数:传入一个int值,在屏幕输出类似LED显示屏效果的字母拼图

 

开始看到这题目,有点傻眼了。。。这怎么搞,先来看看大家怎么搞,原来都是先把几个数字字符样式存好,好吧,这么简单。。。

直接上代码吧

// project1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//要显示的样式
const string LEDnumber[10][7]={
    {" --- ", //0
     "|   |",
     "|   |",
     "|   |",
     "|   |",
     "|   |",
     " --- "},

    {"     ", //1
     "    |",
     "    |",
     "     ",
     "    |",
     "    |",
     "     "},

    {" --- ", // 2
    "    |",
    "    |",
    " --- ",
    "|    ",
    "|    ",
    " --- "},

   {" --- ", //3
    "    |",
    "    |",
    " --- ",
    "    |",
    "    |",
    " --- "},

   {"     ", //4
    "|   |",
    "|   |",
    " --- ",
    "    |",
    "    |",
    "     "},

   {" --- ", //5
    "|    ",
    "|    ",
    " --- ",
    "    |",  
    "    |",
    " --- "},

   {" --- ", //6
    "|    ",
    "|    ",
    " --- ",
    "|   |",
    "|   |",
    " --- "},

   {" --- ", //7
    "    |",
    "    |",
    "     ",
    "    |",
    "    |",
    "     "},

   {" --- ", //8
    "|   |",
    "|   |",
    " --- ",
    "|   |",
    "|   |",
    " --- "},

   {" --- ", //9
    "|   |",
    "|   |",
    " --- ",
    "    |",
    "    |",
    " --- "}
};

//保存每位数字字符
unsigned int digits[50];
//请实现这么一个函数:传入一个int值,在屏幕输出类似LED显示屏效果的字母拼图
void LEDprint(long long num){
    int i=0;
    while(num>0){
        digits[i++] = num%10;
        num = num/10;
    }
    //总共需要打印7行
    for(int line=0;line<7;line++){
        for(int k= i-1;k>=0;k--){
            cout<<LEDnumber[digits[k]][line]<<" ";
        }
        cout<<endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    //printf("5432\n" + 1);
  //改了类型
    long long num=1234567890;
    LEDprint(num);
    return 0;
}

2.输入一个小于100000000(一亿)的正整数,并在屏幕上打印这个数字的中文写法

本题包括其扩展的题目最大的难点在于对0的处理

// project1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;

//保存每个数字的中文
map<int,string> digitInChinese;
//保存十进制中每位的中文
map<int,string> placeInChinese;

void initMaps(){
    digitInChinese.insert(pair<int,string>(0,""));
    digitInChinese.insert(pair<int,string>(1,""));
    digitInChinese.insert(pair<int,string>(2,""));
    digitInChinese.insert(pair<int,string>(3,""));
    digitInChinese.insert(pair<int,string>(4,""));
    digitInChinese.insert(pair<int,string>(5,""));
    digitInChinese.insert(pair<int,string>(6,""));
    digitInChinese.insert(pair<int,string>(7,""));
    digitInChinese.insert(pair<int,string>(8,""));
    digitInChinese.insert(pair<int,string>(9,""));

    placeInChinese.insert(pair<int,string>(0,""));//个位
    placeInChinese.insert(pair<int,string>(1,""));
    placeInChinese.insert(pair<int,string>(2,""));
    placeInChinese.insert(pair<int,string>(3,""));
    placeInChinese.insert(pair<int,string>(4,""));
    placeInChinese.insert(pair<int,string>(5,""));
    placeInChinese.insert(pair<int,string>(6,""));
    placeInChinese.insert(pair<int,string>(7,""));
    placeInChinese.insert(pair<int,string>(8,"亿"));
}

//打印数字的中文写法
void printInChinese(int num){
    int digits[50];
    memset(digits,0,sizeof(int)*50);

    int bits=0;
    while(num>0){
        digits[bits++] = num%10;
        num = num/10;
    }
    
    //打印
    bool occur0=false;
    for(int i=bits-1;i>=0;i--){
        if(digits[i]){
            if(occur0){//如果之前出现过0,此时补零
                occur0=false;
                cout<<digitInChinese[0];
            }
            cout<<digitInChinese[digits[i]]<<placeInChinese[i];
        }
        else{//如果当前数字为0
            occur0=true;
            //如果当前位是万位并且十万,百万,千万位至少有一个不为0
            if(i==4 && (digits[i+1]||digits[i+2]||digits[i+3]))
                cout<<placeInChinese[i];
        }
    }
    cout<<endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
    initMaps();
    int num=10010001;
    printInChinese(num);
    num=1010101;
    printInChinese(num);
    num=100000000;
    printInChinese(num);
    num=100;
    printInChinese(num);
    num=1000101;
    printInChinese(num);
    num=10000110;
    printInChinese(num);
    return 0;
}

3.已知完全弹性碰撞公式如下:

其中m1 m2为小球质量,v1 v2为原始速度,v1' v2'是碰撞后的速度。

struct ball_t {

double m; // 质量

double v; // 速度,速度为正表示球体往x轴正方向运动

double pos; // 在x坐标轴的位置

};

请实现以下函数:

void progress(ball_t & b1, ball_t & b2, double leftWall, double rightWall, double t);

这个函数输入两个球的当前状况(包括质量,速度,在x轴的位置),以及左右墙壁的位置,输出两个球在t秒钟后的状况(包括质量,速度,在x轴的位置)。

特殊说明:球体碰撞墙面也是完全弹性碰撞,即球体速度变为原本的负数。

感觉不难,但写的很糟,随便先贴上吧,以后看有没有心情改

// project1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;

struct ball_t {
double m; // 质量
double v; // 速度,速度为正表示球体往x轴正方向运动
double pos; // 在x坐标轴的位置
ball_t(double m=0,double v=0,double pos=0):m(m),v(v),pos(pos){}
};

/*假设在一个时间片内至多只能发生一次碰撞
 *即使当b1夹在b2与一面墙之间...
 *即一个球要么碰上左墙壁,要么碰上右墙壁,要么碰上另个球,要么什么也没碰上
 *
 *这个函数输入两个球的当前状况(包括质量,速度,在x轴的位置),以及左右墙壁的位置
 *输出两个球在t秒钟后的状况(包括质量,速度,在x轴的位置)
 */
void progress(ball_t & b1, ball_t & b2, double leftWall, double rightWall, double t){
    int interval=1;//间隔
    while(t>0){

        double newpos_b1=b1.pos+b1.v*interval;
        double newpos_b2=b2.pos+b2.v*interval;
        if(((b1.pos-b2.pos)>=0 && (newpos_b1-newpos_b2)<=0)||((b1.pos-b2.pos)<=0 && (newpos_b1-newpos_b2)>=0)){//两球相碰
            b1.v = (b1.v*(b1.m-b2.m)+2*b2.m*b2.v)/(b1.m+b2.m);
            b2.v = (b2.v*(b2.m-b1.m)+2*b1.m*b1.v)/(b2.m+b1.m);
            cout<<"collides"<<endl;
        }
        else{//可能会各自碰墙
            b1.pos=newpos_b1;
            b2.pos=newpos_b2;

            if(newpos_b1<leftWall){
                b1.pos = 2*leftWall-newpos_b1;
                b1.v = -b1.v;
                cout<<"b1 collides leftwall."<<endl;
            }
            if(newpos_b1 > rightWall){
                b1.pos = 2*rightWall-newpos_b1;
                b1.v = -b1.v;
                cout<<"b1 collides rightwall."<<endl;
            }
            if(newpos_b2<leftWall){
                b2.pos = 2*leftWall-newpos_b2;
                b2.v = -b2.v;
                cout<<"b2 collides leftwall."<<endl;
            }
            if(newpos_b2 > rightWall){
                b2.pos = 2*rightWall-newpos_b2;
                b2.v = -b2.v;
                cout<<"b2 collides rightwall."<<endl;
            }
        }

        t-=interval;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    ball_t b1(7,10,49);
    ball_t b2(5,-10,51);
    double leftWall=0,rightWall=100,t=30;
    progress(b1,b2,leftWall,rightWall,t);
    cout<<b1.m<<" "<<b1.pos<<" "<<b1.v<<endl;
    cout<<b2.m<<" "<<b2.pos<<" "<<b2.v<<endl;
    b1.pos=0;b1.v=10;
    b2.pos=100;b2.v=-10;
    progress(b1,b2,leftWall,rightWall,t);
    cout<<b1.m<<" "<<b1.pos<<" "<<b1.v<<endl;
    cout<<b2.m<<" "<<b2.pos<<" "<<b2.v<<endl;
    return 0;
}

4.一个工程由如下文件组成:

head1.h head2.h src1.cpp src2.cpp main.cpp

最终编译结果为xxx.exe(或者xxx,如果在linux下的话)

请写出你熟悉的某种编译器将这5个文件转换为最终结果xxx.exe(或xxx)的详细过程。写出每一个步骤,并作相关的解释,如果你熟悉某编译器的话,请写出这个编译器每一步用到的命令行。

 

这个答案是转的http://blog.csdn.net/huahuahailang/article/details/12223283

 

先将head1.hsrc1.cpp文件生成1.o文件

head2.hsrc2.cpp文件生成2.o文件

再将main.cpp文件生成main.o文件

最后将这些.o文件生成xxx.exe

 

g++ -o 1.o  head1.h src1.cpp

g++ -o 2.o  head2.h src2.cpp

g++ -o main.o main.cpp

g++ -o xxx.exe main.o 1.o 2.o

 

posted @ 2013-11-22 18:49  IT_cnblogs  阅读(329)  评论(0编辑  收藏  举报