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