码迷,mamicode.com
首页 > 其他好文 > 详细

11-1048.数字加密

时间:2017-09-01 22:13:08      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:重写   space   直接   float   stream   turn   out   span   pen   

              1048. 数字加密(20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

本题要求实现一种数字加密方法。首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取余——这里用J代表10、Q代表11、K代表12;对偶数位,用B的数字减去A的数字,若结果为负数,则再加10。这里令个位为第1位。

输入格式:

输入在一行中依次给出A和B,均为不超过100位的正整数,其间以空格分隔。

输出格式:

在一行中输出加密后的结果。

输入样例:
1234567 368782971
输出样例:
3695Q8118
思路:直接按题意写,不倒字符窜,可以看出从左0计数与从右边1开始计数,长度为奇数时相同,偶数时相反,于是分类写了;太麻烦了,看了别人的代码又重新写了一遍。
自己的:
#include <iostream>
#include <cstring>
using namespace std;

int main(){
  char a[105];
  char b[105];
  char c[105];
  int lena, lenb, lenc = 0;
  cin >> a >> b;
  lena = strlen(a);
  lenb = strlen(b);
  int x = lena > lenb ? lena : lenb;      //最多位数 
  c[x] = ‘\0‘;
  if(x % 2 == 0){                         //最高位为偶数 
    while(lena > 0 && lenb > 0){
      if(x % 2 == 0){
        c[x - 1] = (a[lena - 1] - ‘0‘ + b[lenb - 1] - ‘0‘) % 13 + ‘0‘;
        if(c[x - 1] == 10 + ‘0‘)
          c[x - 1] = ‘J‘;
        if(c[x - 1] == 11 + ‘0‘)
          c[x - 1] = ‘Q‘;
        if(c[x - 1] == 12 + ‘0‘)
          c[x - 1] = ‘K‘;
      }
      else{
        c[x - 1] = b[lenb - 1] - a[lena - 1];
        if(c[x - 1] < 0){
          c[x - 1] += 10;
        }
        c[x - 1] += ‘0‘;
      }        
      x--; lena--; lenb--;
    }
    while(lena > 0){
      if(x % 2 == 0){
        c[x - 1] = (a[lena - 1] - ‘0‘ + 0) % 13 + ‘0‘;
        if(c[x - 1] == 10 + ‘0‘)
          c[x - 1] = ‘J‘;
        if(c[x - 1] == 11 + ‘0‘)
          c[x - 1] = ‘Q‘;
        if(c[x - 1] == 12 + ‘0‘)
          c[x - 1] = ‘K‘;
      }
      else{
        c[x - 1] = ‘0‘- a[lena - 1];
        if(c[x - 1] < 0){
          c[x - 1] += 10;
        }
        c[x - 1] += ‘0‘;
      }        
      x--; lena--;
    }
    while(lenb > 0){
      if(x % 2 == 0){
        c[x - 1] = (0 + b[lenb - 1] - ‘0‘) % 13 + ‘0‘;
        if(c[x - 1] == 10 + ‘0‘)
          c[x - 1] = ‘J‘;
        if(c[x - 1] == 11 + ‘0‘)
          c[x - 1] = ‘Q‘;
        if(c[x - 1] == 12 + ‘0‘)
          c[x - 1] = ‘K‘;
      }
      else{
        c[x - 1] = b[lenb - 1];
      }        
      x--; lenb--;
    }
  }
  else{         //最高位为奇数 
    while(lena > 0 && lenb > 0){
      if(x % 2 != 0){
        c[x - 1] = (a[lena - 1] - ‘0‘ + b[lenb - 1] - ‘0‘) % 13 + ‘0‘;
        if(c[x - 1] == 10 + ‘0‘)
          c[x - 1] = ‘J‘;
        if(c[x - 1] == 11 + ‘0‘)
          c[x - 1] = ‘Q‘;
        if(c[x - 1] == 12 + ‘0‘)
          c[x - 1] = ‘K‘;
      }
      else{
        c[x - 1] = b[lenb - 1] - a[lena - 1];
        if(c[x - 1] < 0){
          c[x - 1] += 10;
        }
        c[x - 1] += ‘0‘;
      }
//      cout << c[x - 1] << endl;        
      x--; lena--; lenb--;
    }
    while(lena > 0){
      if(x % 2 != 0){
        c[x - 1] = (a[lena - 1] - ‘0‘ + 0) % 13 + ‘0‘;
        if(c[x - 1] == 10 + ‘0‘)
          c[x - 1] = ‘J‘;
        if(c[x - 1] == 11 + ‘0‘)
          c[x - 1] = ‘Q‘;
        if(c[x - 1] == 12 + ‘0‘)
          c[x - 1] = ‘K‘;
      }
      else{
        c[x - 1] = ‘0‘- a[lena - 1];
        if(c[x - 1] < 0){
          c[x - 1] += 10;
        }
        c[x - 1] += ‘0‘;
      }        
      x--; lena--;
    }
    while(lenb > 0){
      if(x % 2 != 0){
        c[x - 1] = (b[lenb - 1] - ‘0‘) % 13 + ‘0‘;
      }
      else{
        c[x - 1] = b[lenb - 1];
        if(c[x - 1] < ‘0‘){
          c[x - 1] += 10;
        }
      }        
      x--; lenb--;
    }
  }
//  cout << strlen(c) << "clen" << endl;
  cout << c;
  return 0;
} 


 重写的:

#include <cstring>
#include <iostream>
using namespace std;

int main(){
	string a, b, c;
	cin >> a >> b;
	for(int i = 0; i <= (int)a.length()/2 - 1; i++)
		swap(a[i], a[a.length() - i - 1]);
//	int lenb = b.length();
	for(int i = 0; i <= (int)b.length()/2 - 1; i++){    //string类的length()或者size()函数返回的是
											  			//unsigned integer(无符号数)类型。而用在for循环时,
											  			//正常不会出错,但作为判断条件时,
  										      			//当s.length()等于0时,s.length()-1 不等于 -1
		swap(b[i], b[b.length() - i - 1]);
//		cout << b.length() / 2 - 1 << endl;
	}
	if(a.length() > b.length())
		b.append(a.length() - b.length(), ‘0‘);
	else
		a.append(b.length() - a.length(), ‘0‘);
	char str[13] = {‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘J‘, ‘Q‘, ‘K‘};
	for(int i = 0; i < a.length(); i++){
		if(i % 2 == 0){
			c += str[(b[i] - ‘0‘ + a[i] - ‘0‘) % 13];
		}
		else{
			int k = b[i] - a[i];
			if(k < 0)
				k += 10;
			c += str[k];
		}
	}
	for(int i = c.length() - 1; i >= 0; i--)
		cout << c[i];
	return 0;
} 

 


11-1048.数字加密

标签:重写   space   直接   float   stream   turn   out   span   pen   

原文地址:http://www.cnblogs.com/zhumengdexiaobai/p/7465052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!