数组逆序关键是 b[i]=a[a.length-1-i]

本文提供了一个简单的Java程序示例,展示了如何通过创建一个名为Numbers的公共类及其静态方法来实现一维整数数组的反转。该程序首先初始化了一个包含10个元素的数组,并按升序填充了这些元素。接着,通过遍历原始数组并将其元素逆序赋值到新数组中实现了数组的反转。此外,还提供了一个用于打印数组内容的方法。
public class Numbers{

public  static void main(String args[]){


    int oldArr[]=new int[10];
    int newArr[]=new int[10];
   
    for(int i=0;i<oldArr.length;i++){
    oldArr[i]=i;
    }
    print(oldArr);
    //System.out.println(oldArr[i]);
    for(int i=0;i<oldArr.length;i++){
    newArr[i]=oldArr[oldArr.length-1-i];
   
    }
    print(newArr);
    }
public static void print(int temp[]){
for (int i=0;i<temp.length;i++){
System.out.println("数组是 "+temp[i]);
}
System.out.println("\n");
}
   
    }
   
    
    
    
    





#include <stdio.h> #include <string.h> #include <stdbool.h> #define MAX_DIGITS 100 // 大整数结构体 typedef struct { int digits[MAX_DIGITS]; // 存储各位数字,digits[0]是个位 int length; // 数字的长度 bool isNegative; // 是否为负数 } BigInt; // 初始化大整数 void initBigInt(BigInt *num) { memset(num->digits, 0, sizeof(num->digits)); num->length = 0; num->isNegative = false; } // 函数声明 BigInt addBigInt(const BigInt *a, const BigInt *b); BigInt subBigInt(const BigInt *a, const BigInt *b); BigInt mulBigInt(const BigInt *a, const BigInt *b); BigInt divBigInt(const BigInt *a, const BigInt *b, BigInt *remainder); // 从字符串初始化大整数 void initBigIntFromString(BigInt *num, const char *str) { initBigInt(num); int len = strlen(str); int start = 0; // 处理负数 if (str[0] == '-') { num->isNegative = true; start = 1; } // 从字符串末尾开始读取数字 int i = len - 1; for (i = len - 1; i >= start; i--) { if (str[i] >= '0' && str[i] <= '9') { num->digits[num->length++] = str[i] - '0'; } } // 去除前导零 while (num->length > 1 && num->digits[num->length - 1] == 0) { num->length--; } // 处理0的情况 if (num->length == 1 && num->digits[0] == 0) { num->isNegative = false; } } // 比较两个大整数的绝对值大小 // 返回1表示a > b,0表示a == b,-1表示a < b int compareAbsolute(const BigInt *a, const BigInt *b) { if (a->length > b->length) return 1; if (a->length < b->length) return -1; int i = a->length - 1; for (i = a->length - 1; i >= 0; i--) { if (a->digits[i] > b->digits[i]) return 1; if (a->digits[i] < b->digits[i]) return -1; } return 0; } // 比较两个大整数的大小 // 返回1表示a > b,0表示a == b,-1表示a < b int compareBigInt(const BigInt *a, const BigInt *b) { // 符号不同 if (a->isNegative && !b->isNegative) return -1; if (!a->isNegative && b->isNegative) return 1; // 都是正数 if (!a->isNegative && !b->isNegative) { return compareAbsolute(a, b); } // 都是负数 else { return -compareAbsolute(a, b); } } // 大整数加法 BigInt addBigInt(const BigInt *a, const BigInt *b) { BigInt result; initBigInt(&result); // 处理符号不同的情况,转换为减法 if (a->isNegative != b->isNegative) { if (a->isNegative) { BigInt temp = *a; temp.isNegative = false; result = subBigInt(b, &temp); } else { BigInt temp = *b; temp.isNegative = false; result = subBigInt(a, &temp); } return result; } result.isNegative = a->isNegative; int carry = 0; int maxLength = (a->length > b->length) ? a->length : b->length; int i = 0; for (i = 0; i < maxLength; i++) { int sum = a->digits[i] + b->digits[i] + carry; result.digits[result.length++] = sum % 10; carry = sum / 10; } if (carry > 0) { result.digits[result.length++] = carry; } return result; } // 大整数减法(假设a的绝对值大于等于b的绝对值) BigInt subAbsolute(const BigInt *a, const BigInt *b) { BigInt result; initBigInt(&result); int borrow = 0; int i = 0; for (i = 0; i < a->length; i++) { int diff = a->digits[i] - borrow; if (i < b->length) { diff -= b->digits[i]; } if (diff < 0) { diff += 10; borrow = 1; } else { borrow = 0; } result.digits[result.length++] = diff; } // 去除前导零 while (result.length > 1 && result.digits[result.length - 1] == 0) { result.length--; } return result; } // 大整数减法 BigInt subBigInt(const BigInt *a, const BigInt *b) { BigInt result; initBigInt(&result); // 处理符号不同的情况,转换为加法 if (a->isNegative != b->isNegative) { BigInt temp = *b; temp.isNegative = a->isNegative; result = addBigInt(a, &temp); return result; } // 比较绝对值大小 int cmp = compareAbsolute(a, b); if (cmp == 0) { // 绝对值相等,结果为0 result.digits[result.length++] = 0; return result; } if (cmp > 0) { // a的绝对值大于b的绝对值 result = subAbsolute(a, b); result.isNegative = a->isNegative; } else { // b的绝对值大于a的绝对值 result = subAbsolute(b, a); result.isNegative = !a->isNegative; } return result; } // 大整数乘法 BigInt mulBigInt(const BigInt *a, const BigInt *b) { BigInt result; initBigInt(&result); result.isNegative = a->isNegative != b->isNegative; // 处理乘数为0的情况 if ((a->length == 1 && a->digits[0] == 0) || (b->length == 1 && b->digits[0] == 0)) { result.digits[result.length++] = 0; result.isNegative = false; return result; } // 模拟竖式乘法 int i = 0; int j = 0; for (i = 0; i < a->length; i++) { for (j = 0; j < b->length; j++) { int product = a->digits[i] * b->digits[j]; int pos = i + j; // 累加到对应位置 result.digits[pos] += product; // 处理进位 if (result.digits[pos] >= 10) { result.digits[pos + 1] += result.digits[pos] / 10; result.digits[pos] %= 10; } } } // 确定结果长度 result.length = a->length + b->length; while (result.length > 1 && result.digits[result.length - 1] == 0) { result.length--; } return result; } // 大整数左移(乘以10的n次方) void leftShift(BigInt *num, int shift) { if (num->length == 1 && num->digits[0] == 0) { return; } int i = num->length - 1; for (i = num->length - 1; i >= 0; i--) { num->digits[i + shift] = num->digits[i]; } for (i = 0; i < shift; i++) { num->digits[i] = 0; } num->length += shift; } // 大整数右移(除以10的n次方) void rightShift(BigInt *num, int shift) { if (shift >= num->length) { num->digits[0] = 0; num->length = 1; return; } int i = 0; for (i = 0; i < num->length - shift; i++) { num->digits[i] = num->digits[i + shift]; } num->length -= shift; if (num->length == 0) { num->digits[0] = 0; num->length = 1; } } // 大整数除法 BigInt divBigInt(const BigInt *a, const BigInt *b, BigInt *remainder) { BigInt quotient; initBigInt(&quotient); // 处理除数为0的情况 if (b->length == 1 && b->digits[0] == 0) { printf("Error: Division by zero\n"); quotient.digits[quotient.length++] = 0; return quotient; } // 处理被除数为0的情况 if (a->length == 1 && a->digits[0] == 0) { quotient.digits[quotient.length++] = 0; initBigInt(remainder); remainder->digits[remainder->length++] = 0; return quotient; } quotient.isNegative = a->isNegative != b->isNegative; BigInt dividend; initBigInt(&dividend); memcpy(&dividend, a, sizeof(BigInt)); dividend.isNegative = false; BigInt divisor; initBigInt(&divisor); memcpy(&divisor, b, sizeof(BigInt)); divisor.isNegative = false; int cmp = compareAbsolute(&dividend, &divisor); if (cmp < 0) { // 被除数小于除数,商为0,余数为被除数 quotient.digits[quotient.length++] = 0; quotient.isNegative = false; memcpy(remainder, a, sizeof(BigInt)); return quotient; } else if (cmp == 0) { // 被除数等于除数,商为1-1,余数为0 quotient.digits[quotient.length++] = 1; initBigInt(remainder); remainder->digits[remainder->length++] = 0; return quotient; } // 确定商的位数 int shift = dividend.length - divisor.length; leftShift(&divisor, shift); quotient.length = shift + 1; int i = shift; for (i = shift; i >= 0; i--) { while (compareAbsolute(&dividend, &divisor) >= 0) { // 执行减法 BigInt temp = subAbsolute(&dividend, &divisor); memcpy(&dividend, &temp, sizeof(BigInt)); quotient.digits[i]++; } // 除数右移一位 rightShift(&divisor, 1); } // 去除商的前导零 while (quotient.length > 1 && quotient.digits[quotient.length - 1] == 0) { quotient.length--; } // 设置余数 memcpy(remainder, &dividend, sizeof(BigInt)); remainder->isNegative = a->isNegative; // 余数为0时符号为正 if (remainder->length == 1 && remainder->digits[0] == 0) { remainder->isNegative = false; } return quotient; } // 打印大整数 void printBigInt(const BigInt *num) { if (num->isNegative) { printf("-"); } int i = num->length - 1; for (i = num->length - 1; i >= 0; i--) { printf("%d", num->digits[i]); } } int main() { char str1[MAX_DIGITS + 2]; // +2 for sign and null terminator char str2[MAX_DIGITS + 2]; printf("Enter first big integer: "); scanf("%s", str1); printf("Enter second big integer: "); scanf("%s", str2); BigInt a, b; initBigIntFromString(&a, str1); initBigIntFromString(&b, str2); printf("\nA = "); printBigInt(&a); printf("\nB = "); printBigInt(&b); printf("\n"); // 加法 BigInt sum = addBigInt(&a, &b); printf("\nA + B = "); printBigInt(&sum); // 减法 BigInt diff = subBigInt(&a, &b); printf("\nA - B = "); printBigInt(&diff); // 乘法 BigInt product = mulBigInt(&a, &b); printf("\nA * B = "); printBigInt(&product); // 除法 if (!(b.length == 1 && b.digits[0] == 0)) { BigInt remainder; BigInt quotient = divBigInt(&a, &b, &remainder); printf("\nA / B = "); printBigInt(&quotient); printf(" ... "); printBigInt(&remainder); } else { printf("\nCannot divide by zero"); } printf("\n"); return 0; }好了,这是我最终要用的代码,我是一个C语言零基础的人,请详细的为我讲解这段代码,以便我应付我刁钻的老师,帮助我顺利通过答辩。首先我想问为何要比较两个大整数的绝对值大小,
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值