急!!C++中数组问题求解答

大一计算机数组实验,真的不会写,有无专业人士帮帮忙😭任务好多真的很忙,谢谢大家了!
1.将一个整数的各位数字按从低位到高位的顺序存入某一维数组中,然后判断该整数是否为回文数(即判断一维数组是否为镜像数组)。
回文数是指正读与反读都一样的数,例如:12321是回文数,12312不是回文数。
2.输入n个整数并存入某一维数组中,找出其中的最大值,并将其删除。如果有多个相同的最大值,则只删除最后一个。
3.输入一组数据并存入某一维数组中,用选择排序法完成数组元素的降序排列并输出。

  1. 判断一个整数是否为回文数,可以先将该整数转换为字符串,然后判断字符串是否为回文字符串。
#include <iostream>
#include <string>

using namespace std;

bool isPalindrome(int n) {
    string s = to_string(n);
    int i = 0, j = s.length() - 1;
    while (i < j) {
        if (s[i] != s[j]) {
            return false;
        }
        i++;
        j--;
    }
    return true;
}

int main() {
    int n;
    cin >> n;
    if (isPalindrome(n)) {
        cout << n << " is a palindrome number." << endl;
    } else {
        cout << n << " is not a palindrome number." << endl;
    }
    return 0;
}
  1. 找出一个数组中的最大值并删除,可以从数组末尾向前查找第一个最大值,并删除该位置的元素。
#include <iostream>

using namespace std;

void findAndDeleteMax(int a[], int n) {
    int maxIndex = n - 1;
    for (int i = n - 2; i >= 0; i--) {
        if (a[i] >= a[maxIndex]) {
            maxIndex = i;
        }
    }
    cout << "Max value is " << a[maxIndex] << ", located at index " << maxIndex << endl;
    for (int i = maxIndex; i < n - 1; i++) {
        a[i] = a[i + 1];
    }
}

int main() {
    int a[] = {1, 3, 2, 5, 3, 6};
    int n = sizeof(a) / sizeof(a[0]);
    findAndDeleteMax(a, n);
    n--;
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}
  1. 对一个数组进行选择排序,可以使用双重循环,每次找到未排序部分中的最大值,并将其与未排序部分的最后一个元素交换。
#include <iostream>

using namespace std;

void selectionSort(int a[], int n) {
    for (int i = n - 1; i > 0; i--) {
        int maxIndex = i;
        for (int j = 0; j < i; j++) {
            if (a[j] > a[maxIndex]) {
                maxIndex = j;
            }
        }
        if (maxIndex != i) {
            int temp = a[i];
            a[i] = a[maxIndex];
            a[maxIndex] = temp;
        }
    }
}

int main() {
    int a[] = {3, 1, 4, 2, 5};
    int n = sizeof(a) / sizeof(a[0]);
    selectionSort(a, n);
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}
  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7764857
  • 这篇博客你也可以参考下:纯c代码 输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组
  • 除此之外, 这篇博客: 【数据结构与算法】- 排序(算法)中的 十:有一种简单的排序算法,称为计数排序,这种排序算法对一个待排序的表(用数组表示)进行排序,并将排序结果存放到另一个新的表中,必须注意的是,表中所有待排序的关键码互不相同,计数排序算法针对表中的每个记录,扫描待排序的表一趟,统计表中有多少个记录的关键码比该记录的关键码小,假设针对某个记录统计出的计数值为c,则这个记录在新有序表中的合适存放位置为c 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include<iostream>
    using namespace std;
    
    void countsort(int a[],int b[],int n)
    {
    	//遍历每一趟
    	int count=0;
    	for(int i=0;i<n;i++)
    	{
    		//找到每个元素小的个数
    		count = 0;
    		for(int j=0;j<n;j++)
    		{
    			if(a[j]<a[i]) count++;
    		}
    		b[count] = a[i];
    	}
    }
    int main()
    {
    	int a[6]={3,5,7,2,4,9},b[6];
    	countsort(a,b,6);
    	for(int i=0;i<6;i++) cout<<b[i]<<" ";
    	return 0;
    }

  • 以下回答由chatgpt基于相关博客总结生成:
    1. 实现将一个整数的各位数字按从低位到高位的顺序存入某一维数组中,然后判断该整数是否为回文数的C++代码:
    #include <iostream>
    using namespace std;
    
    bool isPalindrome(int num) {
        int temp = num, len = 0;
        while (temp > 0) {   // 计算数字长度,初始值为0
            len++;
            temp /= 10;
        }
        int nums[len];
        temp = num;
        int i = 0;
        while (temp > 0) {   // 将数字按顺序存到数组中
            nums[i++] = temp % 10;
            temp /= 10;
        }
        for (int j = 0; j < len / 2; j++) {  // 判断数组是否为镜像数组
            if (nums[j] != nums[len - 1 - j]) {
                return false;
            }
        }
        return true;
    }
    
    int main() {
        int num;
        cout << "请输入一个整数:";
        cin >> num;
        if (isPalindrome(num)) {
            cout << num << "是回文数。" << endl;
        } else {
            cout << num << "不是回文数。" << endl;
        }
        return 0;
    }
    
    1. 实现在C++中,输入n个整数并存入某一维数组中,找出其中的最大值,并将其删除的代码:
    #include <iostream>
    using namespace std;
    
    int getMax(int nums[], int n) {  // 获取最大值
        int index = 0, maxVal = nums[0];
        for (int i = 1; i < n; i++) {
            if (nums[i] >= maxVal) {
                index = i;
                maxVal = nums[i];
            }
        }
        return index;
    }
    
    void deleteMax(int nums[], int &n) {  // 删除最大值
        int index = getMax(nums, n);
        for (int i = index; i < n - 1; i++) {
            nums[i] = nums[i + 1];
        }
        n--;
    }
    
    int main() {
        int n;
        cout << "请输入数组长度:";
        cin >> n;
        int nums[n];
        cout << "请输入" << n << "个整数:";
        for (int i = 0; i < n; i++) {
            cin >> nums[i];
        }
        int maxVal = nums[getMax(nums, n)];
        deleteMax(nums, n);
        cout << "最大值为:" << maxVal << endl;
        cout << "删除最大值后的数组为:";
        for (int i = 0; i < n; i++) {
            cout << nums[i] << " ";
        }
        cout << endl;
        return 0;
    }
    
    1. 实现使用选择排序法完成数组元素的降序排列并输出的C++代码:
    #include <iostream>
    using namespace std;
    
    void selectSort(int nums[], int n) {  // 选择排序
        for (int i = 0; i < n - 1; i++) {
            int maxIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (nums[j] > nums[maxIndex]) {
                    maxIndex = j;
                }
            }
            if (maxIndex != i) {
                swap(nums[i], nums[maxIndex]);
            }
        }
    }
    
    int main() {
        int n;
        cout << "请输入数组长度:";
        cin >> n;
        int nums[n];
        cout << "请输入" << n << "个数字:";
        for (int i = 0; i < n; i++) {
            cin >> nums[i];
        }
        selectSort(nums, n);
        cout << "排序后的数组为:";
        for (int i = 0; i < n; i++) {
            cout << nums[i] << " ";
        }
        cout << endl;
        return 0;
    }