码迷,mamicode.com
首页 > 其他好文 > 详细

2014华为机试西安地区B组试题

时间:2014-07-10 20:29:34      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:style   color   strong   文件   os   2014   

2014华为机试西安地区B组试题

题目一、亮着点灯的盏数
一条长廊里依次装有n(1≤n≤65535)盏电灯,从头到尾编号1、2、3、…n-1、n。每盏电灯由一个拉线开关控制。开始,电灯全部关着。
有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下;接着第二个学生把号码凡是2的倍数的电灯的开关拉一下;接着第三个学生把号码凡是3的倍数的电灯的开关拉一下;如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。
注:电灯数和学生数一致。不能写双重循环,会运行超时。
输入 65535
输出 255
题目分析:
对于任何一盏灯,由于它原来不亮,那么当它的开关被按奇数次时,灯是开着的;当它的开关被按偶数次时,灯是关着的;一盏灯的开关被按的次数,恰等于这盏灯的编号的因数的个数;要求哪些灯还亮着,就是问哪些灯的编号的因数有奇数个.显然完全平方数有奇数个因数。每个数除以一个数A等于另一个数B,那么A和B都是它的因数,于是因数是成对出现的,但是要因数是奇数,就必须A=B所以这个数就必须是一个是的平方得到的。
综上所述这道题非常简单,就是找1-65535中完全平方数的个数。我们利用枚举法

=========================================================================
参考代码:

//亮着点灯的盏数.cpp
//2014.7.9 hepanhui
#include <iostream>
using namespace std;
int main()
{
    int n;
    int cnt = 0;
    cin >> n;
    for(int x = 1; ; x++)
    {
        int y = x*x;
        if(y > n)
            break;
        cnt ++;
    }

    cout << cnt << endl;
}

调试过程中易错地方,我们要把cnt++放在跳出循环的后面,否则次数会比正确答案多一次。

题目二:相同字符
输入一个字符串,判断是否含有相同的子串(字串长度大于1),是,输出1,否,输出0。
例如12312含有两个12,所以输出1;23456则没有相同子序列,输出0.
输入:12312
输出:1
题目分析:我们首先采用双循环来处理,之后再用一个while语句,最后采用中间变量进行一个转换。

=======================================================================

参考代码:

//相同字符.cpp
//2014.7.10 hepanhui
#include <iostream>
#include <string>
const int maxn = 1000;
using namespace std;

int Equalchar(char *str)
{
    //非法输入
    if(str == NULL)
        return 0;
    int cnt = 0;
    int p,q;

    int len = strlen(str);
    for(int i = 0; i < len;i++)
    {
        for(int j=i+1; j < len;j++)
        {
            p = i;
            q = j;
            while(str[p] == str[q])
            {
                p++;
                q++;
                cnt++;
            }
            if(cnt > 1)
                return 1;
            cnt = 0;
        }
    }
    return 0;
}

int main()
{
    char s[1000];
    gets(s);
    cout << Equalchar(s) << endl;
    return 0;
}

调试过程易错的地方:
①输入字符串有可能有空格。所以不能用cin
②用gets(s)一定要注意加上头文件#include<string>
③记住要采用中间变量,因为判断的时候保证ij不发生变化,所以采用中间变量。
④记住cnt一直要每次while之后置0

题目三、整数相除
两个整数相除,将结果用字符串返回。如果是循环小数,将循环的位用括号括起来。
函数原型为 void div(const int a,const int b,char *str)
输入:1 3
输出:0.(3)
题目分析:
这道题比循环小数的题目多有一点点东西,那就是要判断ab的正负性,其他都一样。
难点在于循环循环小数的周期和循环长度,所以我们这里定义两个数组
int reminder_exist[max_INT];int reminder_pos[max_INT];

==============================================================================
参考代码:

//整数相除.cpp
//2014.7.9 hepanhui
#include<iostream>
#include<string>
using namespace std;
const int maxn = 100; //设置字符串的最大位数
const int max_INT = 10000; 
int reminder_exist[max_INT];
int reminder_pos[max_INT];

void div(const int a, const int b, char *str)
{
    int numerator,denominator,quotient, reminder, outcnt = 0;
    int flag = 0; //负数标志
    int original_numerator = a; //求整数部分用到的分子
    memset(reminder_exist, 0, sizeof(reminder_exist));
    memset(reminder_pos, 0, sizeof(reminder_pos));
    numerator = a; //由于定义const int所以我们要改变分子分母时候,所以我们通过中间变量转化
    denominator = b;

    if(a*b < 0)
        flag = 1;

    //将分子和分母变成整数
    numerator = numerator < 0 ? -numerator:numerator; 
    denominator = denominator < 0 ? -denominator:denominator;

    quotient = numerator/denominator;
    reminder = numerator%denominator;   
    int integer = quotient;

    //找出循环
    //int found_cycle = 0; 
    int cycle_pos = maxn; //循环的位置
    int cycle_len = 0; //初始化循环长度
    int i = 0;
    for(i = 0; i <= maxn; i++)
    {
        //找出余数相等的情况,求出循环周期
        if(reminder_exist[reminder])
        {
            cycle_pos = reminder_pos[reminder];
            cycle_len = i - cycle_pos;
            break;
        }
        else
        {
            reminder_exist[reminder] = 1;
            reminder_pos[reminder] = i;
        }

        numerator = reminder *10;
        quotient = numerator/denominator;
        reminder = numerator%denominator; 

        str[outcnt++] = quotient + ‘0‘; //将更新的商存入字符串中
    }
    str[outcnt++] = ‘\0‘;

    if(!flag)
    {
        cout << integer << "." ;
    }
    else
        cout << "-" << integer << ".";

    for(int j = 0; j < cycle_pos; j++)
        cout << str[j];

    cout << "(";

    for(int j = cycle_pos; j < i;j++)
            cout <<  str[j];
    cout << ")" << endl;
}


int main()
{
    int a,b,flag = 0;
    char s[maxn];
    cin >> a >> b;
    if(!a && !b)
        return 0;
    div( a, b,s);
    return 0;
}

调试过程易犯的错误:
①主函数和子函数变量的设置,和非法输入的安排自己比较混乱;
②讨论负数的情况,直接要将负数变成整数来循环,学会用 ? : 三目运算符
③定义输出是字符串型,我们不能直接str[outcnt++] = quotient;因为quotient是一个数,所以我们必须要加上+‘0’
④仍然需要提醒的是字符串数组存入的题目,要注意
1)outcnt要初始化为0
2)str[outcnt++]中++不能忘记
3)str[outcnt++] = ‘\0‘
4)主函数定义的时候char str[maxn];
5)这道题目输出格式需要仔细书写。


2014华为机试西安地区B组试题,布布扣,bubuko.com

2014华为机试西安地区B组试题

标签:style   color   strong   文件   os   2014   

原文地址:http://blog.csdn.net/to_xidianhph_youth/article/details/37592527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!