(牛客网)华为机试(一)

(牛客网)华为机试题集解答

第一题 HJ108求最小公倍数:在这里插入图片描述

直接gcd解决

1#include<iostream> 2using namespace std; 3int gcd(int a,int b) //求最大公约数函数 4{ 5 int r; 6 while(b) 7 { 8 r=a%b; 9 a=b; 10 b=r; 11 } 12 return a; 13} 14int main() 15{ 16 int a,b,g; 17 while(scanf("%d%d",&a,&b)!=EOF) 18 { 19 if(a>b) 20 g=gcd(a,b); 21 else 22 g=gcd(b,a); 23 g=(a*b)/g; 24 printf("%d\n",g); 25 26 } 27 return 0; 28}

第二题HJ107 求立方根:

在这里插入图片描述

思路:

使用二分查找,首先将输入的数字num与0构成一个区间,然后用(num+0)/2的三次幂与num进行比较,在这里要注意在计算机中两个数相当不意味着两个数字的每一位都相同,而是如果两个数的差值为1e-7(通常情况下)便认为相同,如果不相等便比较大小,如果小于num,则使用右边的区间再中值重复操作,若是大于num就在左边的区间同样操作直到找到满足条件的数然后按小数输出。

1import java.util.Scanner; 2public class Main{ 3 public static void main(String args[]){ 4 Scanner scanner = new Scanner(System.in); 5 while (scanner.hasNext()) 6 { 7 double input = scanner.nextDouble(); 8 double result = getCubeRoot(input); 9 System.out.printf("%.1f\n", result); 10 } 11 scanner.close(); 12 } 13 private static double getCubeRoot(double input){ 14 double min = 0; 15 double max = input; 16 double mid = 0; 17 18 // 注意,这里的精度要提高一点,否则某些测试用例无法通过 19 while ((max - min) > 1e-7) 20 { 21 mid = (max + min) / 2; 22 if (mid * mid * mid > input) 23 max = mid; 24 else if (mid * mid * mid < input) 25 min = mid; 26 else 27 return mid; 28 } 29 return max; 30 } 31}

第三题 HJ106 字符逆序:

在这里插入图片描述在这里插入图片描述

思路:队列或栈都可以直接,或者将字符串存入数组,然后按下标从大到小输出也可以,一定要注意输入的时候空格也是字符只有遇到回车符才结束。

1import java.util.*; 2public class Main{ 3 public static void main(String args[]){ 4 Scanner scanner=new Scanner(System.in); 5 while(scanner.hasNext()){ 6 String str=scanner.nextLine(); 7 StringBuilder sb = new StringBuilder(str); 8 System.out.println(sb.reverse().toString()); 9 } 10 } 11}

第四题 HJ105 记负均正

在这里插入图片描述

思路:

将输入的数字全放入一个数组中,然后遍历数组,在遍历的过程中使用两个计数器分别记录数组中负数的个数和正数的个数,还有有个累加器来记录正数的数字之和,这题重要的是输出格式中均值是小数类型。

1import java.util.*; 2import java.io.*; 3public class Main{ 4 public static void main(String args[]) throws Exception{ 5 BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in)); 6 String[] nums = bufferedreader.readLine().split(" "); 7 int Num1 = 0; //记录负数的个数 8 int Num2 = 0; //记录非负数的个数 9 int sum = 0; 10 for(int i=0;i<nums.length;i++){ 11 int num = Integer.parseInt(nums[i]); 12 if(num<0){ 13 Num1++; 14 } 15 else{ 16 sum+=num; 17 Num2++; 18 } 19 } 20 System.out.println(Num1); 21 System.out.println(Math.round(sum*10.0/Num2)/10.0); 22 23 } 24}

预知后事,请听下回分解!!!!

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java爬虫之JSoup使用教程

title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid

(牛客网)华为机试(一) - HelloWorld