資訊之芽 week 3 課後作業講解
By Koios
988 - Sproutle
題目概述
Wordle 是一款猜字遊戲
每次輸入一個猜測,答案會依據猜測的每個字給予結果
題目要我們模擬 Wordle 判斷每次猜測的結果的顏色標記
題目概述
- 綠色 - 字母出現在答案中且位置正確
- 黃色 - 字母出現在答案中但位置錯誤
- 灰色 - 字母沒出現在答案中
題目概述

輸入說明
第一行輸入答案字串,長度介在 \(4\)~\(10\)
第二行開始每行會是猜測字串,長度與答案字串相同
猜測字串輸入到跟答案字串相同時結束
題目保證猜測字串當中不會有重複出現的字母
輸出說明
對於每個猜測字串,輸出一行相對應顏色
- 綠色以 A 表示
- 黃色以 B 表示
- 灰色以 C 表示
解題思路
對猜測字串的每個字母\(c\)判斷
如果\(c\)放在正確位置,標記為綠色(A)
否則尋找答案當中是否存在\(c\) 有則標記黃色(B),否則標記灰色(C)
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char answer[15], guess[15], color[15];
cin>>answer;
size_t len = strlen(answer);
do{
cin>>guess;
for(int i=0 ; i<len ; i++){
if(guess[i] == answer[i]){
color[i] = 'A';
}
else{
bool found = false;
for(int j=0 ; j<len && !found ; j++){
if(guess[i] == answer[j]){
color[i] = 'B';
found = true;
}
}
if(!found){
color[i] = 'C';
}
}
}
color[len] = '\0';
cout<<color<<'\n';
}while(strcmp(guess, answer) != 0);
return 0;
}#include<iostream>
#include<cstring>
using namespace std;
int main(){
char answer[15], guess[15], color[15];
cin>>answer;
size_t len = strlen(answer);
do{
cin>>guess;
for(int i=0 ; i<len ; i++){
if(guess[i] == answer[i]){
color[i] = 'A';
}
else{
bool found = false;
for(int j=0 ; j<len && !found ; j++){
if(guess[i] == answer[j]){
color[i] = 'B';
found = true;
}
}
if(!found){
color[i] = 'C';
}
}
}
color[len] = '\0';
cout<<color<<'\n';
}while(strcmp(guess, answer) != 0);
return 0;
}#include<iostream>
#include<cstring>
using namespace std;
int main(){
char answer[15], guess[15], color[15];
cin>>answer;
size_t len = strlen(answer);
do{
cin>>guess;
for(int i=0 ; i<len ; i++){
if(guess[i] == answer[i]){
color[i] = 'A';
}
else{
bool found = false;
for(int j=0 ; j<len && !found ; j++){
if(guess[i] == answer[j]){
color[i] = 'B';
found = true;
}
}
if(!found){
color[i] = 'C';
}
}
}
color[len] = '\0';
cout<<color<<'\n';
}while(strcmp(guess, answer) != 0);
return 0;
}#include<iostream>
#include<cstring>
using namespace std;
int main(){
char answer[15], guess[15], color[15];
cin>>answer;
size_t len = strlen(answer);
do{
cin>>guess;
for(int i=0 ; i<len ; i++){
if(guess[i] == answer[i]){
color[i] = 'A';
}
else{
bool found = false;
for(int j=0 ; j<len && !found ; j++){
if(guess[i] == answer[j]){
color[i] = 'B';
found = true;
}
}
if(!found){
color[i] = 'C';
}
}
}
color[len] = '\0';
cout<<color<<'\n';
}while(strcmp(guess, answer) != 0);
return 0;
}#include<iostream>
#include<cstring>
using namespace std;
int main(){
char answer[15], guess[15], color[15];
cin>>answer;
size_t len = strlen(answer);
do{
cin>>guess;
for(int i=0 ; i<len ; i++){
if(guess[i] == answer[i]){
color[i] = 'A';
}
else{
bool found = false;
for(int j=0 ; j<len && !found ; j++){
if(guess[i] == answer[j]){
color[i] = 'B';
found = true;
}
}
if(!found){
color[i] = 'C';
}
}
}
color[len] = '\0';
cout<<color<<'\n';
}while(strcmp(guess, answer) != 0);
return 0;
}#include<iostream>
#include<cstring>
using namespace std;
int main(){
char answer[15], guess[15], color[15];
cin>>answer;
size_t len = strlen(answer);
do{
cin>>guess;
for(int i=0 ; i<len ; i++){
if(guess[i] == answer[i]){
color[i] = 'A';
}
else{
bool found = false;
for(int j=0 ; j<len && !found ; j++){
if(guess[i] == answer[j]){
color[i] = 'B';
found = true;
}
}
if(!found){
color[i] = 'C';
}
}
}
color[len] = '\0';
cout<<color<<'\n';
}while(strcmp(guess, answer) != 0);
return 0;
}953 - 維吉尼亞的統計學
題目概述
維吉尼亞密碼是一種加密方式,其中有
- 要加密的字串明文(Plain Text \(p\))
- 將加密明文的加密金鑰(Key \(k\))
- 加密後的字串密文(Cipher Text \(c\))
透過表格可以將 \(p\) 與 \(k\) 對應到 \(c\)
題目希望我們將\(c\)當中每個字母出現的次數輸出
題目概述

輸入說明
輸入總共兩行
- 第一行是明文(Plain Text)
- 第二行是加密金鑰(Key)
輸出說明
輸出一行(不換行),包含 \(26\) 個數字表示字母出現次數
解題思路

解題思路

解題思路
- 明文決定開始的字母
- 金鑰決定位移數量
實際上就是位移數量不定的凱薩加密
解題思路
令 \(c_i\) 表示密文第 \(i\) 個字元是第幾個字母
令 \(p_i\) 表示明文第 \(i\) 個字元是第幾個字母
令 \(k_i\) 表示金鑰第 \(i\) 個字元是第幾個字母
$$c_i = (p_i + k_i) \% 26$$
解題思路
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char PlainText[1005], Key[1005], CipherText[1005];
cin>>PlainText>>Key;
size_t len = strlen(PlainText);
int Counter[26];
for(int i=0 ; i<26 ; i++){
Counter[i] = 0;
}
for(int i=0 ; i<len ; i++){
CipherText[i] = 'a' + ((PlainText[i] - 'a') + (Key[i] - 'a')) % 26;
Counter[CipherText[i] - 'a']++;
}
for(int i=0 ; i<26 ; i++){
if(i > 0){
cout<<' ';
}
cout<<Counter[i];
}
return 0;
}解題思路
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char PlainText[1005], Key[1005], CipherText[1005];
cin>>PlainText>>Key;
size_t len = strlen(PlainText);
int Counter[26];
for(int i=0 ; i<26 ; i++){
Counter[i] = 0;
}
for(int i=0 ; i<len ; i++){
CipherText[i] = 'a' + ((PlainText[i] - 'a') + (Key[i] - 'a')) % 26;
Counter[CipherText[i] - 'a']++;
}
for(int i=0 ; i<26 ; i++){
if(i > 0){
cout<<' ';
}
cout<<Counter[i];
}
return 0;
}解題思路
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char PlainText[1005], Key[1005], CipherText[1005];
cin>>PlainText>>Key;
size_t len = strlen(PlainText);
int Counter[26];
for(int i=0 ; i<26 ; i++){
Counter[i] = 0;
}
for(int i=0 ; i<len ; i++){
CipherText[i] = 'a' + ((PlainText[i] - 'a') + (Key[i] - 'a')) % 26;
Counter[CipherText[i] - 'a']++;
}
for(int i=0 ; i<26 ; i++){
if(i > 0){
cout<<' ';
}
cout<<Counter[i];
}
return 0;
}解題思路
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char PlainText[1005], Key[1005], CipherText[1005];
cin>>PlainText>>Key;
size_t len = strlen(PlainText);
int Counter[26];
for(int i=0 ; i<26 ; i++){
Counter[i] = 0;
}
for(int i=0 ; i<len ; i++){
CipherText[i] = 'a' + ((PlainText[i] - 'a') + (Key[i] - 'a')) % 26;
Counter[CipherText[i] - 'a']++;
}
for(int i=0 ; i<26 ; i++){
if(i > 0){
cout<<' ';
}
cout<<Counter[i];
}
return 0;
}資訊之芽 week3 課後作業講解
By koios
資訊之芽 week3 課後作業講解
- 149