Hw3 Review

Arvin Liu @ Sprout 2020

2333 - 訊號處理

Sol 2. 呼叫函數

``````#include <cctype>

if(islower(c)) { cout << "c是小寫英文"; }
if(isupper(c)) { cout << "c是大寫英文"; }``````
``````if('a' <= c && c <= 'z') { cout << "c是小寫英文"; }
if('A' <= c && c <= 'Z') { cout << "c是大寫英文"; }``````

A

B

Z

.......

-'A'

0

1

25

.......

char c =

int x =

count[x]++

Solution Code

``````#include <iostream>
int main(){
char s;
int a[26]={0}, x;
std::cin >> x;
while(std::cin>>s){
if('a' <= s && s <= 'z')
a[s-'a']++, std::cout << s;
if('A' <= s && s <= 'Z')
a[s-'A']++, std::cout << (char)(s-'A'+'a');
}
std::cout << std::endl;
for(int i=0; i<26; i++)
std::cout << a[i] << " \n"[i==25];
}``````

462 - 圈圈...畫圈圈...畫圈圈就不會緊張了...

Key 1. up/down

a

z

b

+1

-1

97+25

97

97+1

25

0

1

-1

+1

•  -27 % 26 = -1
• -5 % 26 = -1
• ((x % 26) + 26) % 26

Sol 2. %%%

``````// 因為%會一個循環，所以利用這個特性做Rotate
for(int i=start, j=0; j<len; i++, j++)
cout << S[i % len];``````
``````// 指定位置到後半部
for(int i=start; i<len; i++) cout << S[i];
// 前半部
for(int i=0; i<start; i++) cout << S[i];``````

Solution Code

``````#include <iostream>
#include <cstring>
using namespace std;
char S[20010];
int main(){
cin >> S;
int N, start = 0, add = 0, a, len = strlen(S);
cin >> N;
while(N--){
char ins [10];
cin >> ins >> a;
if( strcmp(ins,"left") == 0)
start += a % len;
else if( strcmp(ins,"right") == 0)
start += len - a % len;
else if( strcmp(ins,"up") == 0)