Java经典编程题50道之三十五
有一个数组,将其最大的元素与第一个元素交换,最小的元素与最后一个元素交换,然后输出数组。
public class Example35 {
public static void main(String[] args) {
int[] a = { 5, 7, 6, 1, 9, 4, 2, 3, 8 };
convertElement(a);
}
public static void convertElement(int[] a) {
System.out.print("测试数组为:");
for (int r : a) {
System.out.print(r + " ");
}
int max = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > a[max]) {
max = i;
}
}
int temp = a[0];
a[0] = a[max];
a[max] = temp;
int min = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] < a[min]) {
min = i;
}
}
int temp1 = a[min];
a[min] = a[a.length - 1];
a[a.length - 1] = temp1;
System.out.print("\n变换后的数组为:");
for (int r : a) {
System.out.print(r + " ");
}
}
}
Java经典编程题50道之三十五的更多相关文章
- Java经典编程题50道之三十六
有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数. public class Example36 { public static void main(String[] a ...
- Java经典编程题50道之三十九
写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. public class Example39 { public static void main(String[] a ...
- Java经典编程题50道之三十八
编写一个函数:输入n为偶数时,调用函数求1/2+1/4+...+1/n:当输入n为奇数时,调用函数1/1+1/3+...+1/n. public class Example38 { public ...
- Java经典编程题50道之三十四
输入3个数a,b,c,按大小顺序输出. public class Example34 { public static void main(String[] args) { sort ...
- Java经典编程题50道之三十二
取一个整数a从右端开始的4-7位. public class Example32 { public static void main(String[] args) { cut(12 ...
- Java经典编程题50道之三十
有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. public class Example30 { public static void main(String[] arg ...
- Java经典编程题50道之四十五
判断一个整数能被几个9整除. public class Example45 { public static void main(String[] args) { f(729); ...
- Java经典编程题50道之十五
输入三个整数x,y,z,请把这三个数由小到大输出. public class Example15 { public static void main(String[] args) { ...
- Java经典编程题50道之五十
有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud"中. public ...
随机推荐
- Jquery实现两级联动
最后结果如下: 关键代码如下: <select name="customerCondition['credibilityBegin']" id="credibili ...
- linux mysql下忘记root密码解决办法
1 修改MySQL的登录设置 # vi /etc/my.cnf 在[mysqld]的中加上一句:skip-grant-tables 2 重新启动mysqld # /etc/init.d/mysqld ...
- java—— finall 关键词
_ *{ margin: 0; padding: 0; } .on2{ margin: 10px 0; cursor: pointer; user-select: none; color: white ...
- python_计算1+……+100中偶数和
如何计算1+--+100中偶数和? 1. 把奇数去掉,通过if,判断累加数除以2的余数,是否为1,判断是否是奇数 2. 通过continue 跳过对奇数的累加 #!/usr/bin/python3 d ...
- Python 中if的使用
reference : https://docs.python.org/3/reference/expressions.html#conditional-expressions 6.11. Cond ...
- linkin大话面向对象--封装和隐藏
软件开发追求的境界:高内聚,低耦合 高内聚:尽可能把模块的内部数据,功能实现细节隐藏在模块内部独立完成,不允许外部直接干预 低耦合:仅暴露少量的方法给外部使用 到底为什么要对一个雷或者对象实现良好的封 ...
- MongoDB投影有$slice如何只显示该字段
简单的投影 稍微用过MongoDB的都知道,投影很简单,就直接 db.student.find({_id:ObjectId('5a5085aed8f10c1a6cc0395b')},{comments ...
- android在一个应用程序员启动另一个程序
一般我们知道了另一个应用的包名和MainActivity的名字之后便可以直接通过如下代码来启动: Intent intent = new Intent(Intent.ACTION_MAIN); int ...
- 【转】5 Best Place to Learn Linux – Linux Tutorial Sites
Linux have amazed every tech guy and make them curious to hands on Linux. Many of us not feel Linux ...
- 【转】GLONASS全球卫星导航系统
GLONASS是“GLOBAL NAVIGATION SATELLITE SYSTE(全球卫星导航系统)”的缩写,作用类似于美国的GPS.欧洲的伽利略卫星定位系统.最早开发于苏联时期,后由俄罗斯继续该 ...