Day 013 一维数组练习

这篇博客通过四个编程实例介绍了Java中的一维数组操作,包括数组排序、求3*3矩阵对角线元素之和、有序数组插入新元素以及数组元素位移。详细讲解了冒泡排序算法和数组元素的动态调整方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、 对10个整数进行按照从小到大的顺序排序

2、求一个3*3矩阵对角线元素之和 <提示> 程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。

3、有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。        <提示>程序分析:首先判断此数第一次小于数组中哪个元素,然后将此数插入,插入后此元          素之后的数,依次后移一个位置。

4、有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数


1、 对10个整数进行按照从小到大的顺序排序

        // 声明一个长度为10的数组
        int[] nums = new int[10];

        // 输入10个整数并存入数组中
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入10个整数");
        for (int i = 0; i < nums.length; i++) {
            nums[i] = sc.nextInt();
        }
        
        // 冒泡排序
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = 0; j < nums.length - 1 - i; j++) {
                if(nums[j+1]<nums[j]){
                    int temp = nums[j];
                    nums[j]=nums[j+1];
                    nums[j+1]=temp;
                }
            }
        }
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }
        sc.close();

2、求一个3*3矩阵对角线元素之和 <提示> 程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。

// 定义一个二维数组
        Scanner sc = new Scanner(System.in);
        int[][] nums = new int[3][3];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[i].length; j++) {
                System.out.println("请输入第" + (i + 1) + "行第" + (j + 1) + "个数:");
                nums[i][j] = sc.nextInt();
            }

        }

        // 求和
        int sum = 0;
        fo

### 关于C++一维数组的有趣练习题和示例 #### 练习题目:查找最大值及其索引位置 此程序接收一系列整数输入并找出其中的最大值以及该最大值的位置。 ```cpp #include <iostream> using namespace std; int main() { const int SIZE = 10; int numbers[SIZE]; cout << "Enter " << SIZE << " integers:" << endl; for (int i = 0; i < SIZE; ++i) { cin >> numbers[i]; } int maxIndex = 0; for (int j = 1; j < SIZE; ++j){ if(numbers[j] > numbers[maxIndex]){ maxIndex = j; } } cout << "The largest number is " << numbers[maxIndex] << ", at index " << maxIndex << "." << endl; } ``` 这段代码展示了如何遍历整个数组来找到最大的元素,并记录下它的索引[^1]。 #### 实际应用案例:温度转换工具 创建一个简单的命令行应用程序,它接受摄氏度作为输入并将它们存储在一个数组里;之后可以将这些数值批量转成华氏度输出给用户查看。 ```cpp #include <iostream> using namespace std; void convertToFahrenheit(int* celsiusArray, double* fahrenheitArray, int size); int main(){ const int DAYS_IN_WEEK = 7; int dailyTemperatures[DAYS_IN_WEEK]; // Store Celsius temperatures here cout << "Please enter this week&#39;s daily high temperature in degrees Celsius."<<endl; for(int k=0;k<DAYS_IN_WEEK;++k){ cout<<"Day "<<(k+1)<<": "; cin>>dailyTemperatures[k]; } double convertedTemps[DAYS_IN_WEEK]; convertToFahrenheit(dailyTemperatures,convertedTemps,DAYS_IN_WEEK); cout<<"\nHere are those same temperatures expressed as Fahrenheit:"<<endl; for(int l=0;l<DAYS_IN_WEEK;++l){ cout<<"Day "<<(l+1)<<": "<<convertedTemps[l]<<" F"<<endl; } } // Function definition outside of &#39;main&#39; void convertToFahrenheit(int* celsiusArray,double* fahrenheitArray,int length){ for(int m=0;m<length;++m){ *(fahrenheitArray+m)=(*celsiusArray+m)*9.0/5.0+32; } } ``` 上述例子不仅涉及到了基本的一维数组操作还引入了函数指针的概念用于处理数据变换逻辑.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值