华为 2015 笔试题练习

一.2015 华为

1:

 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>

int cmp(const void *a, const void *b)
{
	return *(int*)a - *(int*)b;
}

int main()
{
	char strIn[4096] = {'\0'};
	char strTemp[4096] = {'\0'};
	int numResult[4096] = { 0 };
	int sizeIn;
	int resultCount = 0; //结果数组索引
	static int count = 0; //连续的数字计数
	
	while( gets(strIn) != NULL)
	{
		
		sizeIn = strlen(strIn); //取得输入的字符串的大小,包括最后的结束符
		if(sizeIn != 0)
		{
			//取得整形数组
		for(int i =0; i < sizeIn;)
		{
			//取得整数
			if(strIn[i] >= '0' && strIn[i] <= '9')
			{
				strTemp[count] = strIn[i];
				count++;
			}
			else //遇到非数字
			{
				if(count != 0) //如果有数字,则取出来
				{
					//将得到的字符串转换成整形
					numResult[resultCount++] = atoi(strTemp);
					memset(strTemp, 0, 4096);
					count = 0;
				}
			}
			i++;
		}
		if(count != 0) //取得最后的整数
		{
			numResult[resultCount++] = atoi(strTemp);
			count = 0;
		}
		qsort(numResult, resultCount, sizeof(int), cmp);
		for(int i = 0; i < resultCount - 1; i++)
		{
			printf("%d ", numResult[i]);
		}
		printf("%d", numResult[resultCount - 1]); //打印最后一个元素
		printf("\n");
		resultCount = 0;
		count = 0;
		memset(strIn, 0, 4096);
		memset(strTemp, 0, 4096);
		memset(numResult, 0, 4096);
		}
	}	
}

  

附:快速排序使用方法

C标准库快速排序:http://blog.csdn.net/masibuaa/article/details/5633498

C++快速排序:http://blog.csdn.net/zzzmmmkkk/article/details/4266888

2.描述:10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:

(1) 及格线是10的倍数;
(2) 保证至少有60%的学生及格;
(3) 如果所有的学生都高于60分,则及格线为60分
输入:输入10个整数,取值0~100
输出:输出及格线,10的倍数
输入样例:61 51 49 3020 10 70 80 90 99
输出样例:50
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;

int main()
{
    vector<int> scoreVec;
    int scoreTemp;
    int line = 0;
    while(1)
    {
        for(int i = 0; i < 10; i++)
        {
            scanf("%d", &scoreTemp);
            scoreVec.push_back(scoreTemp);
        }
        sort(scoreVec.begin(), scoreVec.end());
        if(scoreVec[0] >= 60)
            line = 60;
        else
        {
            line = (scoreVec[3] + scoreVec[4]) / 2 /10 *10;
            cout << line << endl;
            scoreVec.clear();
        }
    }
}

 3.描述:一条长廊里依次装有n(1 ≤ n ≤ 65535)盏电灯,从头到尾编号1、2、3、…n-1、n。每盏电灯由一个拉线开关控制。开始,电灯全部关着。

有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下;接着第二个学生把号码凡是2的倍数的电灯的开关拉一下;接着第三个学生把号码凡是3的倍数的电灯的开关拉一下;如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。注:电灯数和学生数一致。

 

输入:电灯的数量
输出:亮着的电灯数量
样例输入:3
样例输出:1
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;

int main()
{
    int num = 0; //电灯数

    while(1)
    {
        int number = 0;
        int count[65535] = {0};
        cin >> num;
        number = 0;
        //对每一个电灯统计被拉的次数,如果是偶数,则关;如果是奇数,则开。
        for(int i = 1; i <= num; i++) //学生
        {
            for(int j = 1; j <= num; j++) //电灯
            {
                if(j % i == 0)
                {
                    count[j]++;
                }
            }
        }
        for(int j = 1;j <= num; j++)
        {
            if(count[j] % 2 != 0 )
            {
                number++;
            }
            //count[j] = 0;
        }
        cout << number << endl;
    }
}

 

posted @ 2015-06-30 22:07  Mr.Ethan  阅读(249)  评论(0编辑  收藏  举报