zoukankan      html  css  js  c++  java
  • 1049 数列的片段和 (20 分)

    TIM截图20190228113145.jpg

    需要注意的一点:

    当B的长度小于A的长度时,需要的B的前面补零。

    
    #include <iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    int main()
    {
    	string str1, str2;
    	cin >> str1 >> str2;
    	int len1 = str1.length();
    	int len2 = str2.length();
    	char str1_f[105], str2_f[105];
    	int str3_f[105];
    	//将两个字符串倒置
    	//reverse(begin,end)会将区间[begin,end)内的元素全部逆序
    	//reverse(str1.begin().str1.end());
    	for (int i = 1;i <= len1;i++) {
    		str1_f[i] = str1[len1 - i];
    	}
    	for (int i = 1;i <= len2;i++) {
    		str2_f[i] = str2[len2 - i];
    	}
    	//B的长度小于等于A的长度时,
    	if (len2 <= len1) {
    		for (int i = 1;i <= len2;i++) {
    			if (i % 2 == 0) {//偶数位时
    				int b = str2_f[i] - str1_f[i];
    				if (b < 0) {
    					b += 10;
    				}
    				str3_f[i] = b;
    			}
    			else {//奇数位时
    				int b = str2_f[i] - '0' + str1_f[i] - '0';
    				b = b % 13;
    				str3_f[i] = b;
    			}
    		}
    		//需要在B的前面补零
    		for (int j = len2 + 1;j <= len1;j++) {
    			if (j % 2 == 0) {//偶数位时
    				int b = '0' - str1_f[j];
    				if (b < 0) {
    					b += 10;
    				}
    				str3_f[j] = b;
    			}
    			else {//奇数位时
    				int b = str1_f[j] - '0';
    				str3_f[j] = b;
    			}
    		}
    		for (int j = len1;j > 0;j--) {
    			if (str3_f[j] < 10) {
    				cout << str3_f[j];
    			}
    			else {
    				if (str3_f[j] == 10) {
    					cout << 'J';
    				}
    				if (str3_f[j] == 11) {
    					cout << 'Q';
    				}
    				if (str3_f[j] == 12) {
    					cout << 'K';
    				}
    			}
    		}
    
    	}
    	//B的长度大于A的长度时
    	else {//len2大于len1时
    		for (int i = 1;i <= len1;i++) {
    			if (i % 2 == 0) {//偶数位时
    				int b = str2_f[i] - str1_f[i];
    				if (b < 0) {
    					b += 10;
    				}
    				str3_f[i] = b;
    			}
    			else {//奇数位时
    				int b = str2_f[i] - '0' + str1_f[i] - '0';
    				b = b % 13;
    				str3_f[i] = b;
    			}
    		}
    		for (int i = len1 + 1;i <= len2;i++) {
    			str3_f[i] = str2_f[i]-'0';
    		}
    		for (int j = len2;j > 0;j--) {
    			if (str3_f[j] < 10) {
    				cout << str3_f[j];
    			}
    			else {
    				if (str3_f[j] == 10) {
    					cout << 'J';
    				}
    				if (str3_f[j] == 11) {
    					cout << 'Q';
    				}
    				if (str3_f[j] == 12) {
    					cout << 'K';
    				}
    			}
    		}
    	}
    	
    	return 0;
    }
    
    
  • 相关阅读:
    linux 删除乱码文件
    snprintf用法
    面试时经常问到的非技术性问题
    vector查找元素
    new 和delete
    python安装
    UIPickerView详解
    设置文本框左边显示的View
    字符串的分割??
    VC++异常捕获??
  • 原文地址:https://www.cnblogs.com/chance-zou/p/10449359.html
Copyright © 2011-2022 走看看