华为上机题总结(1)

来源:互联网 发布:电信80端口什么 编辑:程序博客网 时间:2024/05/16 14:31

1.选秀节目打分,分为专家评委和大众评委,score[] 数组里面存储每个评委打的分数,judge_type[] 里存储与 score[] 数组对应的评委类别,judge_type[i] == 1,表示专家评委,judge_type[i] == 2,表示大众评委,n表示评委总数。打分规则如下:专家评委和大众评委的分数先分别取一个平均分(平均分取整),然后,总分 = 专家评委平均分  * 0.6 + 大众评委 * 0.4,总分取整。如果没有大众评委,则 总分 = 专家评委平均分,总分取整。函数最终返回选手得分。
 函数接口   int cal_score(int score[], int judge_type[], int n)

代码如下:

// scoreTest1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;int cal_score(int score[], int judge_type[], int n) {if (score==NULL || judge_type==NULL || n==0){return 0;}int sum=0,sum1=0,sum2=0;int count1=0,count2=0;for(int i=0;i<n;i++){if (judge_type[i]==1)//表示专家{sum1+=score[i];count1++;}if (judge_type[i]==2)//说明是大众评委{sum2+=score[i];count2++;}}if (count2==0)//如果没有大众评委,那么 总分 = 专家评委平均分{sum=sum1/count1;}else{sum=int(0.6*sum1/count1+0.4*sum2/count2);}return sum;}int _tmain(int argc, _TCHAR* argv[]){int score[3]={12,13,15};int judge_type[3]={1,1,2};int n=sizeof(judge_type)/sizeof(int);cout<<cal_score(score,judge_type,n)<<endl;system("pause");return 0;}

 

2.数组比较
问题描述:
 比较两个数组,要求从数组最后一个元素开始逐个元素向前比较,如果2个数组长度不等,则只比较较短长度数组个数元素。请编程实现上述比较,并返回比较中发现的不相等

元素的个数
 比如:
 数组{1,3,5}和数组{77,21,1,3,5}按题述要求比较,不相等元素个数为0
数组{1,3,5}和数组{77,21,1,3,5,7}按题述要求比较,不相等元素个数为3
 要求实现函数:
 int array_compare(int len1, int array1[], int len2, int array2[])
 【输入】 int len1:输入被比较数组1的元素个数;
 int array1[]:输入被比较数组1;
 int len2:输入被比较数组2的元素个数;
 int array2[]:输入被比较数组2;
 【输出】  无
 【返回】  不相等元素的个数,类型为int
 示例
 1)  输入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5},int len2 = 5
 函数返回:0
 2)  输入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5,7},int len2 = 6
 函数返回:3

代码如下:

 

 

// Array_Compary1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <assert.h>using namespace std;int array_compare(int len1, int array1[], int len2, int array2[])   {assert(array1!=NULL && array2!=NULL);int count=0;while(len1!=0&&len2!=0){if (array1[len1-1]!=array2[len2-1])count++;len1--;len2--;}return count;}int _tmain(int argc, _TCHAR* argv[]){int array1[] = {1,3,5};int len1 = 3;int array2[] = {77,21,1,3,5};int len2 =5;cout<<array_compare(len1,array1,len2,array2)<<endl;system("pause");return 0;}


结果为:0

 

3. 一副牌中发五张扑克牌给你:让你判断数字的组成:
 有以下几种情况:
 1:四条:即四张一样数值的牌(牌均不论花色)2:三条带 一对
 3:三条带两张不相同数值的牌
 4:两对
 5:顺子  包括 10,J,Q,K,A
6:什么都不是
 7:只有一对

代码如下:

// PuKe.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <assert.h>using namespace std;void sort(int data[],int n){int temp=0;for(int i=0;i<n;i++ ){for (int j=i+1;j<n;j++){if (data[i]<data[j]){temp=data[i];data[i]=data[j];data[j]=temp;}}}}void test(int a[],int len){int *b=new int[len];int count=0;bool temp=false;for (int i=0;i<len;i++){b[i]=a[i];}sort(b,5);for (int i=0;i<len-1;i++){if (b[i]==b[i+1])count++;}switch (count){case 0:if (b[0]-b[4]==4||(b[0]-b[3]==3&&b[4]==1)){printf("顺子");}elseprintf("什么都不是");break;case 1:printf("只有一对");break;case 2:for (int i=0;i<3;i++){if (b[i]==b[i+2]){printf("三条带两张不相同数值的牌");temp=true;break;}}if (!temp){printf("两对");}break;case 3:if (b[1]==b[3])printf("四条:即四张一样数值的牌");elseprintf("三条带 一对");break;}}int main()  { int a[5]={11,10,3,10,11};test(a,5);system("pause");return 0;}


 

 这里程序还可以这么写:

 

// PuKe_2.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <assert.h>using namespace std;void sort(int a[],int n){int temp;for(int i=0;i<n-1;i++){for (int j=i+1;j<n;j++){if (a[i]<a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}}}void PuKe1(int a[],int len){assert(a!=NULL);int count=0;int* b =new int[len];for (int i=0;i<len;i++){b[i]=a[i];}sort(b,5);for (int i=0;i<len-1;i++){if (b[i+1]==b[i]){count++;}}switch (count){case 0:if (b[4]-b[0]==4){cout<<"顺子"<<endl;}else{cout<<"什么都不是"<<endl;}break;case 1:cout<<"只有一对"<<endl;break;case 2:for (int i=0;i<len;i++){if (b[i]==b[i+2]){cout<<"三条带两张不同的牌"<<endl;break;}}cout<<"两对"<<endl;break;case 3:for (int i=0;i<len;i++){if (b[i]==b[i+3]){cout<<"四条"<<endl;break;}}cout<<"三条带一对"<<endl;break;//case 4: 由于在扑克牌中相等的点数最多只有4个}}int _tmain(int argc, _TCHAR* argv[]){int a[]={3,10,11,10,11};int len=5;sort(a,len);/*for (int i=0;i<len;i++){cout<<a[i]<<" ";}*/PuKe1(a,len);cout<<endl;system("pause");return 0;}


 


 

 

 

 

原创粉丝点击