当前位置:网站首页>Array assignment
Array assignment
2022-06-27 21:39:00 【continueLR】
Catalog
2. Define a length of 10 Integer array nums , Loop input 10 It's an integer .
The maximum value of the output array 、 minimum value .
5. Move zero : Array {0,1,0,3,12} Various methods of
1. Define a length of 10 Integer array nums , Loop input 10 It's an integer .
And then lose Enter an integer , Find this integer , Find the output subscript , I didn't find the prompt .
int t = -1;// Judge whether the subscript has changed the programming idea , because t The future represents the subscript , The subscript will never be -1,
Scanner input = new Scanner(System.in);
System.out.println(" Please enter the first 10 A positive integer ");
int[]nums = new int[10];
for (int i=0;i<nums.length;i++){
nums[i] = input.nextInt();
}
System.out.println(" Please enter a positive integer ");
int n = input.nextInt();
for (int i = 0;i<nums.length;i++){
if (n==nums[i]){
t=i;
System.out.println(" Integers " + n + " In the array, the subscript is " + t);
// Nothing here return, Ensure that the same data can be output
}
}
if (t==-1){// still -1, Indicates that the number of times is not found
System.out.println(" There are no integers in the array " + n);
}
}
}
2. Define a length of 10 Integer array nums , Loop input 10 It's an integer .
The maximum value of the output array 、 minimum value .
System.out.println(" Please enter ten integers :");
Scanner input=new Scanner(System.in);
int[]nums = new int[10];
for (int i=0;i<nums.length;i++){
nums[i]=input.nextInt();
}
int max = nums[0];
int min = nums[0];
for (int i =0;i<nums.length;i++){
if (max<nums[i]){
max=nums[i];
}
if (min>nums[i]){
min=nums[i];
}
}
System.out.println(" The maximum value of the array is :"+max);
System.out.println(" The minimum value of the array is :"+min);
}
}3. Please find the sum in the array as The two integers of the target value ,
And output their array index Suppose that each input corresponds to only one answer , You can't reuse the same elements in this array .
Example : Given nums = [2, 7, 11, 15], target = 9
because nums[0] + nums[1] = 2 + 7 = 9 So the output 0,1
int[]nums={2,7,11,15};
int target=9;
for(int i =0;i<nums.length;i++) {
for(int j =0;j<nums.length;j++) {
if(nums[i]+nums[j]==target&&j!=i) {
System.out.println(" The subscript of the two integers of the target value is :"+i+" and "+j);
return;4. An array {1,3,9,5,6,7,15,4,8} Sort ,
Then use dichotomy to find the element 6 And output the sorted subscript .
int nums[] = {1, 3, 9, 5, 6, 7, 15, 4, 8};
int target = 6;
System.out.println(" Array nums Before ordering :");
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println();
Arrays.sort(nums);// Sort the input array of integers
System.out.println(" Array nums After ordering :");
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println();
// The binary search method is used
int minIndex = 0;
int maxIndex = nums.length - 1;
int midIndex = 0;
while (true) {
midIndex = minIndex + ((maxIndex - minIndex) >> 1);
if (target > nums[midIndex]) {
// The intermediate data is large
minIndex = midIndex + 1;
} else if (target < nums[midIndex]) {
// The intermediate data is small
maxIndex = midIndex - 1;
} else {
// Find target data , Data location :midIndex
break;
}
if (minIndex > maxIndex) {
// Target data not found , return -1
midIndex = -1;
break;
}
}
System.out.println(" lookup " + target + " The integer is in the... Of the array subscript " + midIndex);
}
}5. Move zero : Array {0,1,0,3,12} Various methods of
Method 1: Reassign the method at the end , Stupid method
int[]nums= {0,1,0,3,12};
int temp=0;// To calculate 0 The number of
for(int i=0;i<nums.length;i++){
if(nums[i]==0){// If nums[i]=0,temp Number plus one
temp++;
}else if(temp!=0){// If nums[i] It's not equal to 0, take nums[i] In front of you temp Exchange , And will nums[i] The assignment is 0
nums[i-temp]=nums[i];
nums[i]=0;
}
}
for(int i =0;i<nums.length;i++) {
System.out.println(nums[i]);
}
}
}
Method 2: Double cycle comparison
int temp;
int nums[]={0,1,0,3,12};
for(int i=0;i<nims.length;i++){
for(int j=0;j<nims.length;j++){
if(nums[i]==0&&nums[j]!=0){
temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;Method 3: Reference new array
int[] nums = {0, 1, 0, 3, 12};
int index = 0;// Index of the new array subscript
int[] nums2 = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums2[index++] = nums[i];// This step is the core , Most difficult to think of
}
}
for (int i = 0; i < nums2.length; i++) {
System.out.println(nums2[i]);
}
}
}
边栏推荐
- 神奇的POI读取excel模板文件报错
- oss上传调用的是哪个方法
- A set of system to reduce 10 times the traffic pressure in crowded areas
- 送你12个常用函数公式,用过的都说好
- SQL必需掌握的100个重要知识点:过滤数据
- Icml2022 | scalable depth Gaussian Markov random field
- BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
- Tiktok's interest in e-commerce has hit the traffic ceiling?
- Galaxy Kirin system LAN file sharing tutorial
- Save method of JPA stepping pit series
猜你喜欢

DO280OpenShift访问控制--security policy和章节实验

Codeforces Round #719 (Div. 3)

GFS distributed file system

PCIE知识点-008:PCIE switch的结构

Oracle的CTAS能不能将约束等属性带到新表?

Go从入门到实战——任务的取消(笔记)

AI painting minimalist tutorial

MySQL performance optimization index function, hidden, prefix, hash index usage (2)

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen

ICML2022 | 可扩展深度高斯马尔可夫随机场
随机推荐
Icml2022 | scalable depth Gaussian Markov random field
100 important knowledge points that SQL must master: creating calculation fields
excel读取文件内容方法
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
Go从入门到实战—— 多路选择和超时控制(笔记)
流程控制任务
Modify large online games through CE modifier
Go from introduction to actual combat - all tasks completed (notes)
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
ARCS模型介绍
覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎
农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
Prospects for enterprise digitalization (38/100)
SQL必需掌握的100个重要知识点:组合 WHERE 子句
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
Go from introduction to actual combat - context and task cancellation (notes)
使用storcli工具配置RAID,收藏这一篇就够了