字节跳动笔试题:1. 小于N的质数数量;2. 逆时针二维数组;3. 判断a+b>c
1. 小于N的质数数量
import java.util.Scanner; /**
* 计算小于N的质数数量
* @author Turing
*
*/
public class Main4 {
public static void main( String[] args ) {
Scanner sc = new Scanner(System.in);
int [] arr = new int[100];
int num = 0;
while(sc.hasNextLine()){
String str = sc.nextLine().trim();
if(str.equals("")){
break;
}else{
arr[num++] = Integer.valueOf(str);
}
}
for (int i = 0; i < num; i++) {
System.out.println(primesNum(arr[i]));
}
} public static int primesNum(int n){
boolean[] num = new boolean[n];
int number = 0;
for (int i = 2; i < n; i++) {
if(!num[i]){
number++;
for (int j = 2; i*j < n; j++) {
num[i*j]=true;
}
}
}
return number;
}
}
2. 逆时针二维数组 60%
import java.util.Scanner; /**
* 逆时针打印矩阵
* @author Turing
*
*/
public class Main3 {
public static void main( String[] args ) {
Scanner sc = new Scanner(System.in);
String[] results = new String[100];
int index = 0;
while(sc.hasNextLine()){
String str = sc.nextLine().trim();
if(str.equals("")){
break;
}else{
String[] strs = str.split(" ");
int M = Integer.valueOf(strs[0]);
int N = Integer.valueOf(strs[1]);
int [][] matrix = new int [M][N];
int value = 1;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = value;
value++;
}
}
results[index++] = spiral(matrix, M, N);
}
}
for (int i = 0; i < index; i++) {
System.out.println(results[i]);
} } public static String spiral(int[][] matrix,int M,int N){
String result = "";
if(M!=0){
int row1 = 0;
int row2 = M -1;
int col1 = 0;
int col2 = N-1;
while(row1<=row2 && col1<=col2){
for (int i = col2; i >=col1; i--) {
result += matrix[row1][i] +" ";
}
for (int i = row1+1; i <=row2; i++) {
result += matrix[i][col1] +" ";
}
if(row1<row2 && col1<col2){
for (int i = col1+1; i < col2; i++) {
result += matrix[row2][i] +" ";
}
for (int i = row2; i > row1; i--) {
result += matrix[i][col2] +" ";
}
}
row1++;
row2--;
col1++;
col2--;
}
}
return result.trim();
}
}
3. 判断a+b>c
import java.util.Scanner; /**
* a + b > C
* int64 int64 int64
* [-2^36,-2^63-1]
* @author Turing
*
*/
public class Main2 {
public static void main( String[] args ) {
Scanner sc = new Scanner(System.in);
boolean[] results = new boolean[100];
int index = 0;
while(sc.hasNextLine()){
String str = sc.nextLine().trim();
if(str.equals("")){
break;
}else{
String[] strs = str.split(" ");
long a = Long.valueOf(strs[0]);
long b = Long.valueOf(strs[1]);
long c = Long.valueOf(strs[2]);
results[index++] = abc(a, b, c);
}
}
for (int i = 0; i < index; i++) {
System.out.println(results[i]);
}
} public static boolean abc(long a, long b, long c){
if(a>0 && b>0 && a+b<0){
return true;
}
if(a<0 && b<0 && a+b>0){
return false;
}
return a+b>c?true:false;
}
}
字节跳动笔试题:1. 小于N的质数数量;2. 逆时针二维数组;3. 判断a+b>c的更多相关文章
- 【面试题003】c数组做为参数退化的问题,二维数组中的查找
[面试题003]c数组做为参数退化的问题,二维数组中的查找 一,c数组做为参数退化的问题 1.c/c++没有记录数组的大小,因此用指针访问数组中的元素的时候,我们要确保没有超过数组的边界, 通过下面 ...
- 剑指Offer:面试题3——二维数组中的查找(java实现)
问题描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路:取数组中的元素与 ...
- 【剑指Offer面试题】九度OJ1384:二维数组中的查找
下决心AC全部剑指offer面试题. 九度OJ面试题地址:http://ac.jobdu.com/hhtproblems.php 书籍:何海涛--<剑指Offer:名企面试官精讲典型编程题> ...
- 剑指Offer面试题:2.二维数组中的查找
一.题目:二维数组中的查找 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...
- P38、面试题3:二维数组中的查找
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 首先选取数组中右上角的数字 ...
- 《剑指Offer》面试题-二维数组中的查找
题目1384:二维数组中的查找 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7318 解决:1418 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到 ...
- 剑指offer(纪念版) 面试题3:二维数组中的查找
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 第一种方法题目说不可行 ...
- 【剑指Offer学习】【面试题:二维数组中的查找】PHP实现
最近一直看剑指Offer.里面很多算法题.于是就想着用PHP来显示一下. 题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的 ...
- 剑指offer面试题3 二维数组中的查找(c)
剑指offer面试题三:
随机推荐
- .NET Core 内置的 System.Text.Json 使用注意
System.Text.Json 是 .NET Core 3.0 新引入的高性能 json 解析.序列化.反序列化类库,武功高强,但毕竟初入江湖,炉火还没纯青,使用时需要注意,以下是我们在实现使用中遇 ...
- 用OC基于链表实现链队列
一.简言 在前面已经用C++介绍过链队列的基本算法,可以去回顾一下https://www.cnblogs.com/XYQ-208910/p/11692065.html.少说多做,还是上手撸代码实践一下 ...
- Linux 安装 MySQL 出现 Could NOT find Curses
通过源码安装 MySQL 数据库,下载了 mysql-5.5.24 的版本,在使用 cmake 时产生了报错,如下: CMake Error at cmake/readline.cmake: (MES ...
- swoole的process模块创建和使用子进程
swoole中为我们提供了一个进程管理模块 Process,替换PHP的 pcntl 扩展,方便我们创建进程,管理进程,和进程间的通信. swoole提供了2种进程间的通信: 1.基于 unix so ...
- 使用zabbix监控linux的io
zabbix自带的监控linux的模板中并没有监控io这项,而实际生产中又需要监控io,如何监控呢. 错误的示例 这里我特意贴出错误的示例出来,是因为我在网上搜如何使用zabbix监控io的文章时,好 ...
- js获取当前日期,包括星期几
function getCurrentDate() { var myDate = new Date(); var year = myDate.getFullYear(); // ...
- 安装keepalived OpenSSL is not properly installed on your system. !!!
错误信息: configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not includ ...
- Python【day 14-4】sorted filter map+递归文件夹+二分法查找
def func(x): #普通函数 return x*x ret1 = func(10) #匿名函数 f = lambda x:x*x # 匿名函数写法: 匿名函数名=lambda 参数:返回值 ' ...
- Winform中在ZedGraph中最多可以添加多少条曲线
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- HTML中引用CSS的几种方法
HTML中引用CSS的方法主要有 行内样式 内嵌式 链接式 导入样式 行内样式 指写在标签里的Style元素的值 <p style="color: #FF0000;"> ...