# HW 1 for Lab 1
## Problem 1
> Output:
> 
> [color=#907bf7]
```clike=
#include <stdio.h>
int main(void){
char faces[3][10] = {" ^ ^",
"(=-w-=)", " \" \""};
int i = 0;
for(i=0; i<3; i++){
printf("%s\n", faces[i]);
}
return 0;
}
```
## Problem 2
> Output:
> 
> [color=#907bf7]
```clike=
#include <stdio.h>
int main(void){
int i, j, x = 0;
//Input Tree Parameter
int height = 55; //better height = (9n+3)) //
//Input const
double ratio = 2.0/7.0; // tH/cH
//Tree Baseline
int cHeight = height/(1.0+ratio);
int wide = cHeight*2;
int tHeight = height - cHeight;
int core = (wide-2)/2;
// //Parameter Debugger
// printf("*cHeight:%d\n*tHeight:%d\n*wide:%d\n*core:%d\n", cHeight, tHeight,wide,core);
//
// //j col Debugger
// printf(" ");
// for(int z = 0; z<wide;z++){
// printf("%d", x);
// x++;
// if(x == 9){
// x = 0;
// }
// }
// printf("\n");
//Print Crown
for(i=0;i<cHeight;i++){
for(j=0;j<wide;j++){
if(j == (core-i)){
printf("/");
//odd layer
if(i%2 == 0 && i !=0){
int blank = (i-1)*2;
if(i == cHeight-1){
//bottom
for(int k =0;k<(blank+2);k++){
printf("-");
}
}else{
printf("-");
for(int k = 0; k<blank;k++){
printf(" ");
}
printf("-");
}
j = j + (i*2);
}
}else{
if(j == (core+1)+i){
printf("\\");
break;
}else{
printf(" ");
}
}
}
printf("\n");
}
//print trunk
for(i=0;i<tHeight;i++){
for(j=0;j<wide;j++){
if((j == core - 1) || (j == core + 2)){
printf("|");
if(i == tHeight-1 && j == core - 1){
printf("__");
j = j+2;
}
}else{
if(j > core + 2){
break;
}else{
printf(" ");
}
}
}
// if(i<tHeight-1){
// printf("\n");
// }
printf("\n");
}
return 0;
}
```
## Problem 3: 13979 - we stack boxes
Description
There are n boxes with length of l each.
Jake wanted to stack the boxes into a tall tall tower.
Help him calculate the height of the tower h.
> Input
> Two integers n and l seperated by a blank
> (1 < n, l < 1000)
>
> Output
> The height h. (No need to use '\n')
> [color=#907bf7]
```clike=
#include <stdio.h>
int main(void){
int n, l, h = 0;
scanf("%d %d", &n, &l);
h = n*l;
printf("%d", h);
return 0;
}
```
# HW2 for Lab 2
## Problem 1: 13577 - Anya loves to encrypt
To encrypt the string by changing lowercase to uppercase and reversing each alphabet.
That is,
'a' first becomes 'A' and then becomes 'Z'.
'b' first becomes 'B' and then becomes 'Y'.
'c' first becomes 'C' and then becomes 'X'.
...
'y' first becomes 'Y' and then becomes 'B'.
'z' first becomes 'Z' and then becomes 'A'.
> Input: apple
> Input contains only a line.
> A 5 character word.
> It is guarantee that the word contains only lower case alphabet.
>
> Output: ZKKOV
> The encrypted word.
> remember to print \n at the end of output.
> [color=#907bf7]
```clike=
#include <stdio.h>
int main(void){
// A-Z: DEC 65-90
// a-z: DEC 97-122
// enc = ori - (7+2*i); 0<=i<26
// i = -(97-input)
char ori[6];
scanf("%s", ori);
int i, ctr, enc =0;
while(ori[ctr] != '\0'){
i = -(97-ori[ctr]); //alphabet order position
enc = ori[ctr] - (7 + 2*i);
ori[ctr] = enc;
//printf("%c\n", ori[ctr]); //debugger
ctr++;
}
printf("%s\n", ori);
return 0;
}
```
## Problem 2: 13969 - Frieren and Magic
Given input X which describes the total days, convert X to the number of years and seconds.
Note :
1 year is 365 days
1 day is 24 hours
1 hour is 3600 seconds
> Input: 2000000000
>
> Input contains 1 integer X
> 0 < X < 231
>
> Output: 5479452.054795 5479452.000000 172800000000000
>
> Output 3 numbers, number of years in double datatype, years in float datatype, and seconds, separated by space.
> The year should be in 6th decimal place format.
> Don’t forget to print a new line '\n' after the end of the output.
> [color=#907bf7]
```clike=
#include <stdio.h>
int main(void){
long long int days;
scanf("%ld", &days);
double d_y = days/365.0;
float f_y= days/365.0;
long long int sec = days*24*3600;
printf("%.6lf %.6f %16ld\n", d_y, f_y, sec);
return 0;
}
```
if compile error: https://stackoverflow.com/questions/13590735/printf-long-long-int-in-c-with-gcc
## Problem 3: 13970 - Frieren and her Rizz
Given a 6-digit binary, help her to encrypt the password to a character.
The first digit is a signed bit, as per the rules :
If the signed bit is 1, make it lowercase
If the signed bit is 0, make it uppercase
The last 5 digits determine the number. Convert the number to a character, as:
Number 1 is ‘a’
Number 2 is ‘b’
Number 3 is ‘c’
and so on … until 26 as ‘z’
All of them are based on alphabetic order.


Explanation of sample input :
010010 split into 0 and 10010
The signed bit is 0, which means it’s uppercase
10010 is 18, which is R in alphabetical order.
> Input: 010010
> Input is 6 digit binary representation number (contain 1 or 0)
> You can safely asume that the number is in the range 1 <= X <= 26
>
> Output: R
> Output the encrypted character
> Don't forget to print the new line
> [color=#907bf7]
**Challenge**
> Mission 1: Can you do it without if-else?
> Mission 2: How will you solve it if the character is assigned in the reverse order? Maybe it'll appear in your lab next Monday!

```clike=
#include <stdio.h>
int main(void){
int bin; // 6 digits binary
scanf("%d", &bin);
// 1 => lowercase; 0 => uppercase
// A-Z: DEC 65-90
// minus 32 for lower case
// a-z: DEC 97-122
// ===========TESTED NO IF ELSE============
int cap;
int last;
int base = 1;
int ascii = 65; // start with A uppercase
int n_ascii;
int r_ascii;
cap = bin/100000;
ascii = ascii + cap*32; // lower or uppercase
// printf("1 lower => 32 : %d\n", ascii); // debug capitalization
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
int counter = 0;
int order = 0;
// Ref: Binary conversion algorithm: https://www.javatpoint.com/binary-to-decimal-number-in-c
while(bin>0 && counter < 5) //only getting the last 5 digit => 2^4
{
last = bin % 10; //for getting the last digit
order = order + (last*base); //adding the processed last digit with updated base
bin = bin/10; //forwarding the binary aka toss the last digit
base = base *2; //updating the base as soon as tossing the last digit
counter++;
// printf("%d\n", base); //debug: based update should be max 4
}
// printf("%d", ascii+(order-1)); //debug: ascii position
n_ascii = ascii + (order-1); //non-reverse
r_ascii = ascii + (26-order); //reverse
printf("%c\n", n_ascii);
printf("%c", r_ascii);
return 0;
}
```
## Notes on specifier

## 2022 [Lab2](https://acm.cs.nthu.edu.tw/contest/2551/): 13576 - Anya wants to study math
Given three integers G, B, and S:
1) G: The number of guns
2) B: The number of bullets per gun
3) S: The number of spy
Anya needs to calculate the total number of bullets and how many guns a spy can be distributed.
> [color=#907bf7] **I/O**:
> Input
> The input will contain 3 integers G, B, and S.
> (1<= G, B, S <= 100000)
>
> Output
> First line prints calculation result of total number of bullets, the result might exceed 2^32
> Second line prints calculation result of how many gun a spy can be distributed
> and rounded off to 2 decimal places
> remember to print \n at the end of output.
>
>> Sample Input
> 12 10 5
>
>> Sample output
> 120
> 2.40
```clike=
#include <stdio.h>
int main()
{
double G, B, S;
scanf("%lf %lf %lf", &G, &B, &S); //Gun Bullet/gun spy
long long int bullet = G * B;
long double dis = G / S;
printf("%lld\n", bullet);
printf("%.2Lf\n", dis);
return 0;
}
```
## 2022 Lab 2: 13577 - Anya loves to encrypt
'a' first becomes 'A' and then becomes 'Z'.
'b' first becomes 'B' and then becomes 'Y'.
'c' first becomes 'C' and then becomes 'X'.
...
'y' first becomes 'Y' and then becomes 'B'.
'z' first becomes 'Z' and then becomes 'A'.
> Input
> Input contains only a line.
> A 5 character word.
> It is guarantee that the word contains only lower case alphabet.
>
> Output
> The encrypted word.
> remember to print \n at the end of output.
> [color=#907bf7]
```clike=
#include <stdio.h>
int rev(int);
int main()
{
//only lowercase
char word[5];
scanf("%s", word);
int ctr = 0;
int cap = 0;
while(word[ctr] != '\0'){
//Uppercasing
cap = (word[ctr] - ('a'-'A'));
//Reversing
cap = rev(cap);
printf("%c", cap);
ctr++;
}
printf("\n");
}
int rev(int up){
int order = up - 'A';
return 'Z' - order;
}
```
# HW3 for Lab 3
## 12913 - Big number addition
**Description**
In the most of arithmetic addition case, we can simply use int or long long to store the two integers
and compute the answer by the operater '+' in C. If we have two big integers that can not be stored into the varibles mentioned below,
we should make use of arrays to complete the addition task.
Given two big number A, B, please compute A + B.
**Input**
First line contains an integer which denotes the length of A. The following one line contains the big integer A.
Third line contains an integer which denotes the length of B. The following one line contains the big integer B.
(1 ≤ number of digits of A, B ≤ 100, both A and B are intergers ≥ 0)
**Output**
An integer which denotes the answer of A + B.
> Sample [color=#907bf7]
> Input
> 25
> 1111111111111111111111111
> 25
> 1111111111111111111111111
>
> Output
> 2222222222222222222222222
Note that you don't need to print '\n' at the end of the output
*Strategy*
1. Exception for 99+1 => need to print the carry value
2. Need to print the value in integer inspite string => delete useless 0
3. Be careful or the digit placement
```clike=
#include <stdio.h>
void add(int, int, char[], char[]);
int main()
{
//Keeper is digit of the upper part
//Flwr is digit of the lower part
int d1, d2;
scanf("%d", &d1);
char num1[d1+1]; //array size is digit + 1 because string
scanf("%s", num1);
scanf("%d", &d2);
char num2[d2+1];
scanf("%s", num2);
int keeper, flwr;
if(d1>d2){
keeper = d1;
flwr = d2;
add(keeper, flwr, num1, num2);
} else{
keeper = d2;
flwr = d1;
add(keeper, flwr, num2, num1);
}
return 0;
}
void add(int keeper, int flwr, char main[], char sub[]){
int below;
int sum;
int carry = 0;
char result[keeper+1];
//Array Boundary
int end1 = keeper-1;
int end2 = flwr-1;
int i;
for(i=end1; i>=0; i--){
if(end2>=0){
below = sub[end2] - '0';
}else{
below = 0;
}
sum = (main[i] - '0') + below + carry;
result[i+1] = (sum % 10) + '0';
carry = sum / 10;
end2--;
}
if(carry){
result[0] = carry + '0';
}else{
result[0] = '0';
}
//printf("%s", result);
int flag = 1;
int index = 0;
while(index < keeper + 1){ //keeper + 1 is result size
if(flag && result[index] == '0' && index < keeper){ //not included last digit
}else{
printf("%d", result[index] - '0');
flag = 0;
}
index++;
}
}
```
**big number logic:** https://telegra.ph/Big-Integer-Addition-10-02
## 13996 - Squidward Musical
The notes that patrick writes contains a lot of palindromes and Spongebob suspects that this is the key to copying
Squidward's beautiful music but just to be sure, he wants to count how many palindromes are in the string.
Palindromes is a set of words that are read the same when written forwards and backwards,
however for this problem, palindromes that have 2 characters or less do not count.
For example the string elellal contains only 3 palindromes:
- ele
- lel
- lal
'll' does not count as a palindrome because it only has 2 characters.
Count how many palindromes are in a given string from patrick's notes.
Note: duplicate palindromes still count!
For example the string elele contains 4 palindromes:
- ele
- lel
- ele
- elele
> Input: elellal[color=#907bf7]
> A single string with length l (1 <= l <= 1000).
>
> Output: 3
> The number of Palindromes in the string followed by a newline character "\n".
```clike=
#include <stdio.h>
#include <string.h>
int counter = 0;
//Palindrome Checker
void palindrome(char *str){
int start = 0;
int end = strlen(str) - 1;
while(start < end){
if(str[start++] != str[end--]){
return;
}
}
counter++;
return;
}
int main(void){
char input[1001];
scanf("%s", input);
int end = strlen(input);
int i, j;
for(i = 0; i<end-2; i++){ //updating starting index of input
char temp[1001] = ""; //creating temp memory so we can get all the comb
for (j = i; j < end; j++){ //Updating the temp want to be checked
strncat(temp, &input[j], 1); //appending letter to the checker
if(strlen(temp)>= 3){ //fulfilling palindrome length rule
palindrome(temp); //Checking the current temp
}
}
}
printf("%d\n", counter);
return 0;
}
```
Palindrome logic: https://telegra.ph/Palindrome-Logic-10-02
> You can use num = printf(of array) to see the char length
# HW4 for Lab 4
## 14010 - Jangan Makan Indomieku
**Description**
TA Arithat is feeling upset because all of his k cups of Indomie were eaten during the test. To determine who is responsible, he has summoned n students to the classroom. As each student enters, they must follow a specific rule for knocking on the table:
When the first student arrives, they knock on the table once.
For subsequent students:
If the total number of students who have arrived so far is odd, the student must knock the table k times that of the previous student.
If the total number of students who have arrived so far is even, the student must knock the table k times more than the previous student.
After all students have knocked, the last student to arrive may knock on the door again using the same rule, and then everyone in the room will take turns knocking in reverse order of their initial entry.
For example, if there are 5 students named Alice, Bob, Charlie, David, and Eve, and k is 2, the knocking sequence would look like this:
(Alice enters, 1 student in total)
Alice: 1
(Bob enters, 2 students in total)
Alice: 1, Bob: 3, Alice: 1
(Charlie enters, 3 students in total)
Alice: 1, Bob: 2, Charlie: 4, Bob: 2, Alice: 1
(David enters, 4 students in total)
Alice: 1, Bob: 3, Charlie: 5, David: 7, Charlie: 5, Bob: 3, Alice: 1
(Eve enters, 5 students in total)
Alice: 1, Bob: 2, Charlie: 4, David: 8, Eve: 16, David: 8, Charlie: 4, Bob: 2, Alice: 1
As the first student, your task is to keep a record of how many times each student knocked on the table. You do not need to include their names in the record.
Input
All test case contains two positive integer n and k. (1 ≤ n ≤ 32, 1 ≤ k ≤ 10)
Output
The record of how many times every person has knocked in each round. The number of times a student knocks on the table is to be separated by a space. At the end of each round, you must output an endline character '\n'.
It is guaranteed that all numbers are less than 2^63 - 1
```clike=
/*
5 2
1
1 3 1
1 2 4 2 1
1 3 5 7 5 3 1
1 2 4 8 16 8 4 2 1
*/
#include <stdio.h>
int main()
{
int s, k;
long long int start;
int mem, m;
scanf("%d %d", &s, &k);
for(int i = 1; i<=s; i++){
start = 1;
mem = i + (i-1);
m = 1;
if(i%2 != 0){
//printf("[%d] Odd: ", i);
for(int j = 1; j<=i; j++, m++){
printf("%lld", start);
if(j != i && m<mem) printf(" ");
if(j != i) start = start * k;
}
for(int j = 1; j<i; j++, m++){
if(j == 1) printf(" ");
start = start / k;
printf("%lld", start);
if(i-j != 1) printf(" ");
}
}else{
//printf("[%d] Eve: ", i);
for(int j = 1; j<=i; j++){
printf("%lld ", start);
if(j != i) start = start + k;
}
for(int j = 1; j<i; j++){
start = start - k;
printf("%lld", start);
if(i-j != 1) printf(" ");
}
}
printf("\n");
}
return 0;
}
```
## 14007 - The Girl I Like Forgot Her Glasses
If you are here, you probably know what convolution is already.
So I'm going to make a summary of the program I need you to make.
I'm going to create a pair of glasses that takes in a grayscale image of width w and height h.
I want you to create an algorithm that turns it into a convoluted image of width w-2 and height h-2
using a 3-by-3 kernel that contains a single-precision floating value in each pixel.
Remember to round the numbers down to integers.
> Input
> In the first line, there are two positive integers, w and h,
> representing the width and height of the image. (3 ≤ w,h ≤ 300).
>
> Following are 9 single-precision floating point numbers distributed into a 3 x 3 grid,
> representing the numbers in the kernel.
>
> The remaining h lines consist of w integers each,
> representing the brightness value in a w x h grayscale picture.
> The brightness value in each pixel is a non-negative number b (0 ≤ b ≤ 255).
>
> Output
> The resulting convolved (w-2) x (h-2) image,
> consisting of numbers rounded to an integer with field width 4 (the format specifier is %4d),
> all followed by a space. At the end of each line, print an end-line character '\n'.
Input:
6 6 => w, h (3 ≤ w,h ≤ 300)
-1.0 0.0 1.0 => kernel (3x3)
-2.0 0.0 2.0
-1.0 0.0 1.0
7 6 5 5 6 7 => h line, with w int each; brightness
6 4 3 3 4 6
5 3 2 2 3 5
5 3 2 2 3 5
6 4 3 3 4 6
7 6 5 5 6 7
Output: => convolved (w-2) x (h-2)
-11 -4 4 11 => rounded to int; 4d followed by space
-12 -4 4 12 => \n
-12 -4 4 12
-11 -4 4 11
Matrix multiplication: h, w: l x m dot m x n = l x n
row exhausted, col exhausted
Matrix convolution is different from multiplication
Convolution = masking; first multiply each member, then sum all

```clike=
#include <stdio.h>
int w, h;
int convo(int y, int x, float kernel[3][3], int img[h][w], int h, int w);
int main()
{
scanf("%d %d", &w, &h);
//verified with printf .1f || Please use address of location to save
float kernel[3][3];
for(int i = 0; i<3; i++){
scanf("%f %f %f", &kernel[i][0], &kernel[i][1], &kernel[i][2]);
}
//Verified with printf %4d
//Array: [row][column] => h line w int
int img[h][w];
for(int i = 0; i<h; i++){
for(int j = 0; j<w; j++){
scanf("%d", &img[i][j]);
}
}
//Moving convolution
for(int i=0; i<h-2; i++){
for(int j = 0; j<w-2; j++){
printf("%4d ", convo(i, j, kernel, img, h, w));
}
printf("\n");
}
return 0;
}
int convo(int y, int x, float kernel[3][3], int img[h][w], int h, int w){
float result = 0;
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
result += img[i+y][j+x] * kernel[i][j];
}
}
return (int) result;
}
```
## Lab4 2022: 13260 - Nappa's Attack

```clike=
#include <stdio.h>
int main()
{
int last = 0;
scanf("%d", &last);
int arr[last][(last*2)-1];
for(int i = 0; i<last; i++){ //per row
arr[i][last-1] = (i+1);
for(int j = 1; j<last; j++){
if((i+1)-j<=0){
arr[i][(last-1)-j] = 0;
arr[i][(last-1)+j] = 0;
}else{
arr[i][(last-1)-j] = (i+1)-j;
arr[i][(last-1)+j] = (i+1)-j;
}
}
}
for(int i = 0; i<last; i++){
for(int j = 0; j<(last*2)-1; j++){
if(arr[i][j] == 0 && j<(last-1)){
printf(" ");
}else
if(arr[i][j] == 0 && j>(last-1)){
continue;
}else
printf("%d", arr[i][j]);
}
printf("\n");
}
return 0;
}
```
# Mid Term Practice
## Mid2022: 13291 - KONODIODA
Given an image (square matrix) A[N,N], if point P(X,Y) is the center of a star, the following condition will be satisfied:
(1) A[X][j]=255, for all 0<=j<N (The values in the Xth row are all 255)
(2) A[i][Y]=255, for all 0<=i<N (The values in the Yth column are all 255)
(3)
A[X+i][Y+i]=255, for all -N<=i<N if 0<=(X+i)<N and 0<=(Y+i)<N
A[X+i][Y-i]=255, for all -N<=i<N if 0<=(X+i)<N and 0<=(Y-i)<N
(The values of two diagonals from the centers are all 25
> **Input**
There are three parts of the input:
>
> (1) The number of testcases, T. 1<=T<=1000
>
> (2) The size of the square matrices, N. 1<=N<=2048
>
> (3) T N*N matrices separated by a newline character. 0<=The values in the matrices<=255.
>
> **Output:**
> The number of stars in each matrix.
> Note that you need to print “\n” in the end of each answer.



```clike=
/*
2
9
255 0 0 0 255 0 0 0 255
254 255 0 0 255 0 0 255 0
0 0 255 0 255 0 255 0 0
0 0 0 255 255 255 0 0 0
255 255 255 255 255 255 255 255 255
0 0 0 255 255 255 0 0 0
0 0 255 0 255 0 255 0 0
0 255 0 0 255 0 0 255 0
255 0 0 0 255 0 0 0 255
0 255 0 0 0 255 0 0 254
0 0 255 0 0 255 0 0 255
0 0 0 255 0 255 0 255 0
0 0 0 0 255 255 255 0 0
255 255 255 255 255 255 255 255 255
0 0 0 0 255 255 255 0 0
0 0 0 255 0 255 0 255 0
0 0 255 0 0 255 0 0 255
0 255 0 0 0 255 0 0 0
number of star
1
1
255 000 000 000 255 000 000 000 255
254 255 000 000 255 000 000 255 000
000 000 255 000 255 000 255 000 000
000 000 000 255 255 255 000 000 000
255 255 255 255 255 255 255 255 255
000 000 000 255 255 255 000 000 000
000 000 255 000 255 000 255 000 000
000 255 000 000 255 000 000 255 000
255 000 000 000 255 000 000 000 255
*/
#include <stdio.h>
//If array size is variable, cannot init;
int matrix[2049][2049]; //OJ = 2048
int main(void) {
int tests = 0;
int size = 0;
scanf("%d", &tests);
scanf("%d", &size);
int border = size - 1;
while(tests--) { //Iterate till Que End
// [Step 1]: Set Up Counter Parser
int row[2049]= {0};
int col[2049] = {0};
int downhill[4099] = {0};
int uphill[4099] = {0};
// [Step 2]: Set Up Matrix and Parsing
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
scanf("%d", &matrix[i][j]);
// [Step 2.a]: Parsing; If == 255; +1; if all true; sum=size
if(matrix[i][j] == 255){
row[i]+=1;
col[j]+=1;
downhill[i-j+border]+=1; //Left-Bottom = 4
uphill[i+j]+=1; //Right-Bottom = 4
}
}
}
unsigned long long int result = 0;
// [Step 3]: Checking whether for each point has valid axis
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
//For row and col, because square matrix, the sum of all member = size
//But for diagonal we need to find each diagonal member first
// [Step 3.a]: Finding diagonal member for each i,j
int member_down = 0;
int member_up = 0;
if(i-j > 0){ //Downhill Flip the limit
member_down = size - (i - j); // Below the downhill i-j>0 so - the size
}
else{
member_down = size + (i - j); //Above the downhill i-j<0 so + the size
}
if(i+j >= size){ //Uphill Flip the limit
member_up = 2*size - (i+j+1); //below uphill i+j>=size so - the max
}
else{
member_up = i+j+1; //Above uphill i+j<size so + all
}
// [Step 4: Validate all axis exist]
if(row[i] == size && col[j] == size && downhill[i-j+border] == member_down
&& uphill[i+j] == member_up){
result++;
}
}
}//Closure for each matrix
// [Step 5]: Printing Result
printf("%lld\n", result);
}//Closure after que finish
return 0;
}
```