B1048 非法的间接寻址错误

本文详细介绍了字符串反转的算法实现,通过具体代码示例展示了如何使用C++进行字符串的反转操作,并结合输入字符串A和B的处理过程,生成新的字符串结果。文章重点讲解了在反转过程中需要注意的细节,如避免等于号的使用,以及如何根据不同条件进行字符的转换。

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

总结:
反转两次;
反转时,i=0时,i<len/2,切记没有等于号;

 #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<algorithm>
    const int maxn = 110;
    char A[maxn], B[maxn], ans[maxn] = { 0 };
    using namespace std;
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    void reverse1(char s[])//反转字符串
    {
    	int len = strlen(s);
    	for (int i = 0; i < len; i++)
    	{
    		reverse(s[i], s[len - i - 1]);
    	}
    }
    int main(int argc, char** argv) {
    	cin >> A;
    	cin >> B;
    	int lenA = strlen(A);
    	int lenB = strlen(B);
    	int len = lenA > lenB ? lenA : lenB;
    	for (int i = 0; i < len; i++)
    	{
    		int numA = i < lenA ? A[i] - '0' : 0;
    		int numB = i < lenB ? B[i] - '0' : 0;
    		if (i % 2 == 0)
    		{
    			int temp = (numB + numA) % 13;
    			if (temp == 10)
    				ans[i] = 'J';
    			else if (temp == 11)
    				ans[i] = 'Q';
    			else if (temp == 12)
    				ans[i] = 'K';
    			else
    				ans[i] = temp + '0';
    			
    		}
    		else//当前位数为奇数;
    		{
    			int temp = (numB - numA) >= 0 ? (numB - numA) : (numB - numA + 10);
    			ans[i] = temp + '0';
    		}
    	}
    	reverse1(ans);
    	puts(ans);
    	return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值