题目描述如下:
我的解题思路就是遍历给定的字符串,加密就加上“偏移量(秘钥)”,解密就减掉“偏移量”;秘钥先出队,用完再入队(循环使用)。我同学的编码的思路和我相似,但是他的能过系统的测试用例我的却不能。系统里面不能看见测试用例,只能看见最终输出结果的差异:
由于所学有限,实在找不出原因,请大家不吝赐教。
我自己的写法:
string CaesarEncry(const string& input, AQueue<int>& key, int cmd) {
string result = "";
for (int i = 0;i < input.length();i++) {
char c = input[i];
int shift = key.dequeue();
if (cmd == 1) {
cmd = 1;
} else {
cmd = -1;
}
c = c + (cmd * shift);
result += c;
key.enqueue(shift);
}
return result;
}
我同学的写法:
string CaesarEncry2(const string& input, AQueue<int>& key, int cmd) {
string result="";
for (size_t i = 0; i < input.length(); ++i) {
char currentChar = input[i];
int shift = key.frontValue(); // Get the current shift from the queue
key.enqueue(key.dequeue()); // Rotate the key
if (cmd == 1) { // Encrypt
result += static_cast<char>(currentChar + shift);
} else if (cmd == 2) { // Decrypt
result += static_cast<char>(currentChar - shift);
}
}
return result;
}
以下题目所给的参考代码:
#include<string>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <bitset>
using namespace std;
using std::cout;
using std::endl;
using std::string;
using std::ostream;
const int defaultSize = 10; // Default size
// Assert: If "val" is false, print a message and terminate
// the program
void Assert(bool val, string s) {
if (!val) { // Assertion failed -- close the program
cout << "Assertion Failed: " << s << endl;
exit(-1);
}
}
// Abstract queue class
template <typename E> class Queue {
private:
void operator =(const Queue&) {} // Protect assignment
Queue(const Queue&) {} // Protect copy constructor
public:
Queue() {} // Default
virtual ~Queue() {} // Base destructor
// Reinitialize the queue. The user is responsible for
// reclaiming the storage used by the queue elements.
virtual void clear() = 0;
// Place an element at the rear of the queue.
// it: The element being enqueued.
virtual void enqueue(const E&) = 0;
// Remove and return element at the front of the queue.
// Return: The element at the front of the queue.
virtual E dequeue() = 0;
// Return: A copy of the front element.
virtual const E& frontValue() const = 0;
// Return: The number of elements in the queue.
virtual int length() const = 0;
};
// This is the declaration for AStack.
// Array-based queue implementation
template <typename E> class AQueue : public Queue<E> {
private:
int maxSize; // Maximum size of queue
int front; // Index of front element
int rear; // Index of rear element
E* listArray; // Array holding queue elements
public:
AQueue(int size = defaultSize) { // Constructor
// Make list array one position larger for empty slot
maxSize = size + 1;
rear = 0; front = 1;
listArray = new E[maxSize];
}
~AQueue() { delete[] listArray; } // Destructor
void clear() { rear = 0; front = 1; } // Reinitialize
void enqueue(const E& it) { // Put "it" in queue
rear = (rear + 1) % maxSize;
listArray[rear] = it;
}
E dequeue() { // Take element out
E it = listArray[front];
front = (front + 1) % maxSize;
return it;
}
const E& frontValue() const { // Get front value
Assert(length() != 0, "Queue is empty");
return listArray[front];
}
virtual int length() const // Return length
{
return ((rear + maxSize) - front + 1) % maxSize;
}
};
// 补全这个函数
string CaesarEncry(const string& input, AQueue<int>& key, int cmd) {
string result = "";
return result;
}
int main() {
AQueue<int> qkey(16);
int cmd = 0, num;
string strkey, str;
string item;
//inputs
getline(cin, strkey);
getline(cin, str);
cin >> cmd;
stringstream str_num(strkey);
while (getline(str_num, item, ' ')) {
num = stoi(item);
qkey.enqueue(num);
}
return 0;
}