Hw3 Review 

Arvin Liu @ Sprout 2020

2333 - 訊號處理

Problem Description

Keypoint

1. 判斷是不是小寫 /大寫 ...
2. 計算有多少個英文字

Key 1. 判斷是不是小寫...

Sol 1. 直接硬刻

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是大寫英文"; }

Key 2. 計算有多少個字

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 - 圈圈...畫圈圈...畫圈圈就不會緊張了...

Problem Description

Keypoint

0. 判斷指令 -> strcmp

1. up/down % 26
2. left/right % len
3. How 2 Rotate String?

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

Key 3. How 2 Rotate?

Sol 1. 直接硬刻

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)
            add += a % 26;
        else if( strcmp(ins,"down") == 0)
            add += 26 - a % 26;

        start = start % len;
        add = add % 26;
        for(int i=start,j=0; j<len; i++,j++){
            cout << (char) ((S[i%len]-'a'+add)%26+'a');
        }
        cout << endl;
    }
}
Made with Slides.com