杭电计算机2010年笔试真题详解

第一题:

猜数字的游戏,不太难的。题目:随即产生一个3位的正整数,让你进行猜数字,如果猜小了,输出:“猜小了,请继续”。如果猜大了,输出:“猜大了,请继续”。如果猜对了。输出:“恭喜你,猜对了”。不过最多只能猜10次,如果猜了10次还没有猜对,就退出程序,输出:“Bye Bye”。 还是比较简单的,就是三位随机数没有处理好。
这道题的难点就是如何表示出三位随机数

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    bool flag=false;//判断一下是否命中 
    int randnum=rand()%900+100;//表示三位数 这是一个难点 
    for(int i=0;i<10;i++){
        int temp;
        cin>>temp;
        if(temp<randnum)
            cout<<"猜小了"<<endl;
        else if(temp>randnum)
            cout<<"猜大了"<<endl;
        else{
            cout<<"命中了" <<endl;
            flag=true;
            break;          
        }
    }
    if(!flag)
        cout<<"Bye Bye"<<endl;
    return 0;
}

第二题

是字符串求和。编写函数 FindAndSum,输入一个字符串,把字符串中的数字作为整数进行求和,并输出结果。Sample : 输入:There are some apple. 输出:0 。输入:124and 1524 输出:1648 。这个题目是最简单的,只要读入的时候记得使用 gets 函数就可以了,scanf 函数遇到空格的时候输入就结束了。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    char str[1000] ;
    while(gets(str)){
        char num[100];
        int cnt=0;
        int sum=0;
        for(int i=0;i<strlen(str);i++){
            if(str[i]>='0'&&str[i]<='9')
                num[cnt++]=str[i];
            else{
                if(cnt){
                    num[cnt]=0;//字符串结束标志 
                    sum+=atoi(num);
                    cnt=0;//每输入完一个整数就初始化 
                }
            } 
        }
        if(cnt)//如果数组里面还有数据,则加上
            sum+=atoi(num); 
        cout<<sum<<endl; 
    }
    return 0;
}

第三题

是文件操作和结构体对象数组的处理问题,处理一个文件 student.txt,文件当中包括一组学生的信息,包括名字、学号、英语成绩、语文成绩、数学成绩、科学成绩,如下:
姓名 学号 英语 语文 数学 科学
张三 20100601 78 89 62 75
李四 20100602 78 54 98 86
王五 20100603 78 69 85 75
……………………………………
从这个文件当中读入学生的信息,然后按照总成绩从高到低进行排序并输出学生信息。由于长时间没有做过有关文件操作的题目,感觉很多都记不起来了,仅仅凭着一点记忆把代码写出来了,后面的结构体数组的排序处理就比较简单了。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<fstream>
using namespace std;
struct student{//姓名       学号      英语  语文   数学   科学
    char name[20];
    char ID[20];
    int english;
    int chinese;
    int math;
    int science;
}stu[100];
bool cmp(student a,student b){
    int suma=a.chinese+a.english+a.math+a.science;
    int sumb=b.chinese+b.english+b.math+b.science;
    return suma>sumb;
} 
int main(){
    fstream in;
    int num=0;//记录文件中的条目; 
    in.open("student.txt",ios::in);
    while(in>>stu[num].name>>stu[num].ID>>stu[num].english>>stu[num].chinese>>stu[num].math>>stu[num].science){
        num++;
    }
    sort(stu,stu+num,cmp);
    for(int i=0;i<num;i++)
        cout<<stu[i].name<<" "
        <<stu[i].ID<<" "
        <<stu[i].english<<" "
        <<stu[i].chinese<<" "
        <<stu[i].math<<" "
        <<stu[i].science<<" "
        <<endl; 
    return 0;
}

这道题的关键在于读取文件,比较头疼的一点是我显示出来的字符集是乱码的,而我更改字符集为utf-8后仍然未得到解决,这点比较头疼。还有有一点就是我并没有对第一行进行处理,也就是这个程序并不能处理表头
这一年的题目有很强的相似性,与后面几年的好几道题都是类似的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值