2020年12月10日 星期四

【Wicked】popular 歌詞筆記

makeover: (個人形象的)改頭換面

take over: 接管

follow my lead: 跟著我

poise: 姿態(不確定歌詞裡的意思)

flounce: (為了吸引人注意)大步走動

fix my hair: 整理頭髮

cohort: 有共通點的(通常指年齡)一群人

awfully:非常、十分

dialysis: (醫學上對患者的)透析

pal: 朋友

dreary: 沉悶的、令人沮喪的

unprepossessing: 無趣乏味的

on their own behalf: 他們自己的行為上

shrewd: 精明的

clandestinely: 暗中的


2020年12月9日 星期三

【Wicked】for good 歌詞筆記

歌詞的兩段類比好美!

Like a comet pulled from orbit

As it passes the sun

Like a stream that meets a boulder

Halfway through the wood


Like a ship blown from its mooring

By a wind off the sea

Like a seed dropped by a sky bird

In a distant wood


for good: 玩了雙關,for good(=better),也是永遠

it's up to you: 靠你自己

boulder: 巨石;卵石

part: 分別;離開

handprint: 手印

mooring: 停泊處

clear the air: 消除隔閡

2020年12月8日 星期二

【Wicked】Defying Gravity 歌詞筆記

超級推Willemijn Verkaik唱的版本,太好聽了!!!

https://soundcloud.com/nikolay-hsu/defying-gravity/s-UnSwZ



fly off the handle: 發火

grovel: 卑躬屈膝

second-guess: 猜測、預言

grandeur: 宏偉壯麗

in tandem: 同心協力

renown: 名聲

2020年10月7日 星期三

invert infix to postfix or prefix

 infix to postfix

infix to prefix



#include <iostream>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

bool isOperator(char c)
{
	if (c == '+' || c == '/' || c == '-' || c == '*') return true;
	return false;
}
int priority(char c)
{
	if (c == '+' || c == '-') return 1;
	if (c == '*' || c == '/') return 2;
}
string to_postfix(string s)
{
	string postfix = "";
	stack<char> st;
	s += ")";
	st.push('(');
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] == '(') st.push(s[i]);
		else if (isOperator(s[i]))
		{
			while (st.top()!='('&&priority(st.top()) >= priority(s[i]))
			{
				postfix += st.top();
				st.pop();
			}
			st.push(s[i]);
		}
		else if (s[i] == ')')
		{
			while (st.top() != '(')
			{
				postfix += st.top();
				st.pop();
			}
			st.pop();
		}
		else
		{
			postfix += s[i];
		}
	}
	while (!st.empty())
	{
		postfix += st.top();
		st.pop();
	}
	return postfix;
}
string to_prefix(string s)
{
	reverse(s.begin(), s.end());
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] == '(') s[i] = ')';
		else if (s[i] == ')') s[i] = '(';
	}
	s = to_postfix(s);
	reverse(s.begin(), s.end());
	return s;
}
int main()
{
	string s;
	getline(cin, s);
	string postfix = to_postfix(s);
	cout << postfix << endl;
	cout << to_prefix(s);
	return 0;
}

2020年9月10日 星期四

a224: 明明愛明明

 解題心得:

會錯的可能有兩個地方:

1.如果全都不是字母,輸出yes

2.如果字母都是偶數,也輸出yes (奇數次<=1都可算對)

程式碼:

#include <iostream>
#include <map>
using namespace std;

int main()
{
    string s;
    while(cin>>s)
    {
        map<char,int> mp;
        map<char,int>::iterator it;
        int odd_count=0;
        for(int i=0;i<s.size();i++)
        {
            if(isalpha(s[i]))
            {
                s[i]=tolower(s[i]);
                mp[s[i]]++;
            }
        }
        for(it=mp.begin();it!=mp.end();it++)
        {
            if(it->second%2) odd_count++;
        }
        if(odd_count>1) cout<<"no..."<<endl;
        else cout<<"yes !"<<endl;
    }
    return 0;
}

2020年9月8日 星期二

a308: NOIP2011 2.统计单词数

解題心得:
用string的find()快速找到子字串的開頭位置,如果沒有則回傳string::npos。
不過這樣找也有可能會找到錯誤的,像是找單字:to
如果有一個單字叫otto或tto之類有包含to兩字母的話,也會誤以為找到。
因此則改成找單字且前後都有空格的話才算是找到,也因此句子裡每一個單字前後也都要有空白,所以才有那兩行奇怪的操作。

程式碼:
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    int count=0;
    string s,word,tmp;
    getline(cin,word);
    getline(cin,s);
    for(int i=0;i<word.size();i++)
        word[i]=tolower(word[i]);
    for(int i=0;i<s.size();i++){
        s[i]=tolower(s[i]);
    }
    stringstream ss(s);
    while(ss>>tmp)
    {
        if(tmp==word)
            count++;
    }
    word=' '+word+' ';
    s=' '+s+' ';
    if(s.find(word)!=string::npos) cout<<count<<" "<<s.find(word)<<endl;
    else cout<<-1<<endl;
    return 0;
}

2020年9月7日 星期一

inheritance

base(parent) class v.s. derived(child) class
-inherit 變數與函式
-create 新的變數與函式
- is-a 的關係

無法繼承
-建構子與解構子
-friend(爸爸的朋友未必是兒子的朋友)
-static
-Overloading assignment operators and copy constructors


public inheritance
-can't access the private members of the base class
-member functions of the derived class can access public and protected members of the base class
最常見的繼承,子類別可以跟平常一樣使用父類別

private inheritance
-can't access the private members of the base class
-member functions of the derived class can access public and protected members of the base class
An object of the derived class cannot access any member (including public, protected and private) of the base class

protected inheritance
–member functions of the derived class still can access public and protected members of the base class directly
–cannot access private members of the base class

小結:
在base class裡的public跟protected members的存取權會隨著繼承方式是哪種而變成哪種,至於private members不管用哪種方式繼承都不可被存取。

2020年9月5日 星期六

11222 - Only I did it!

解題心得:
寫的不是很精簡QQ

程式碼:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int t,n,temp;
    cin>>t;
    for(int r=1;r<=t;r++)
    {
        cout<<"Case #"<<r<<":"<<endl;
        vector<int> v[3],vn[3];
        vector<int>::iterator it1,it2;
        for(int i=0;i<3;i++)
        {
            cin>>n;
            for(int j=0;j<n;j++)
            {
                cin>>temp;
                v[i].push_back(temp);
            }
        }
        int record[3]={0};
        for(int i=0;i<v[0].size();i++)
        {
            it1=find(v[1].begin(),v[1].end(),v[0][i]);
            it2=find(v[2].begin(),v[2].end(),v[0][i]);
           if(it1==v[1].end()&&it2==v[2].end()) vn[0].push_back(v[0][i]),record[0]++;
        }
        for(int i=0;i<v[1].size();i++)
        {
            it1=find(v[0].begin(),v[0].end(),v[1][i]);
            it2=find(v[2].begin(),v[2].end(),v[1][i]);
           if(it1==v[0].end()&&it2==v[2].end()) vn[1].push_back(v[1][i]),record[1]++;
        }
        for(int i=0;i<v[2].size();i++)
        {
            it1=find(v[0].begin(),v[0].end(),v[2][i]);
            it2=find(v[1].begin(),v[1].end(),v[2][i]);
           if(it1==v[0].end()&&it2==v[1].end()) vn[2].push_back(v[2][i]),record[2]++;
        }
        int Max=max(record[0],record[1]);
        Max=max(Max,record[2]);
        for(int i=0;i<3;i++)
        {
            if(Max==record[i])
            {
                sort(vn[i].begin(),vn[i].end());
                cout<<i+1<<" "<<record[i];
                for(int j=0;j<vn[i].size();j++)
                    cout<<" "<<vn[i][j];
                cout<<endl;
            }
        }
    }
    return 0;
}

2020年9月4日 星期五

10878 - Decode the tape

解題心得:
二進位轉十進位。

程式碼:
#include <iostream>
using namespace std;

int main()
{
    string s;
    while(getline(cin,s))
    {
        if(s!="___________")
        {
            int value=0;
            for(int i=0;i<s.size();i++)
            {
                if(s[i]=='o') value=value*2+1;
                else if(s[i]==' ') value*=2;
            }
            cout<<char(value);
        }
    }
    return 0;
}

2020年9月3日 星期四

streams

istream

get()
-prototype:
    istream& get(char &);
-example:
    char a;
    cin.get(a);
說明:
從串流裡拿一個字元出來,放到a裡。

-prototype:
    int get();
-example:
    cout<<"press any key to continue"<<endl;
    int c=cin.get();
說明:
從串流裡拿一個字元出來,以interger型態存到c裡。

-prototype:
    istream& get(char *str, int len,char c = '\n');
說明:
從串流中拿出指定長度的字串,放到指標指到的裡。


getline()
-prototype:
    istream& getline(char *str,int len, char c='\n');
說明:
跟get()的第三種用法相同。


ignore()
-prototype:
    istream& ignore(int length = 1,char c = '\n');
說明:
從串流裡拿默認長度為1的字串忽略。


ostream

setf()
cout.setf(ios::showpos|ios::dec|ios::showpoint);

width()
cout.width(5);

precision()
cout.precision(4);

雖然也有:
cout<<setw(5)<<87<<endl;

cout<<fixed<<setprecision(7)<<endl;
之類的寫法,不過要另外引用<iomanip>才能使用,但上面的則是ostream裡的函式,當標頭檔寫了<iostream>時就已在內,無須另外引入。


File I/O

要引用<fstream>。
可以依據需求,使用ifstream(讀檔)或ofstream(寫檔),或者用fstream且告知讀或寫檔。

例:
ifstream File("data.txt"); // ifstream File; File.open("data.txt");

例:
ofstream File("data.txt");

例:
fstream File;
File.open("data.txt",ios::in); // 或 ios::out, ios::app

在讀寫檔前,要檢查是否正確開檔:
if(File.good())

if(!File())

10789 - Prime Frequency

程式碼:
#include <iostream>
#include <map>
using namespace std;

bool isPrime(int n)
{
    if(n==1) return false;
    for(int i=2;i<=n/2;i++)
        if(n%i==0) return false;
    return true;
}
int main()
{
    string  s;
    int t;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        int empty=1;
        cin>>s;
        cout<<"Case "<<i<<": ";
        map<char,int> mp;
        map<char,int>::iterator it;
        for(int j=0;j<s.size();j++)
            mp[s[j]]++;
        for(it=mp.begin();it!=mp.end();it++)
        {
            if(isPrime(it->second)) 
            {
                cout<<it->first;
                empty=0;
            }
        }
        if(empty) cout<<"empty";
        cout<<endl;
    }
    return 0;
}

2020年9月2日 星期三

10226 - Hardwood Species

解題心得:
處理資料輸入卡住好久。
用map的好處是很方便計次,也不需要額外排序。


程式碼:
#include <iostream>
#include <map>
#include <iomanip>
using namespace std;

int main()
{
    int n;
    string s;
    cin>>n;
    getchar();
    getchar();
    while(n--)
    {
        map<string,int> mp;
        map<string,int>::iterator it;
        int total=0;
        while(getline(cin,s)&&s!="")
        {
            mp[s]++;
            total++;
        }
        for(it=mp.begin();it!=mp.end();it++)
        {
            cout<<it->first<<" "<<fixed<<setprecision(4)<<(double)it->second/total*100<<endl;
        }
        if(n!=0)
            cout<<endl;
    }
    return 0;
}

2020年8月25日 星期二

b976: 5.最終任務->a.尋找提示

程式碼:
#include <iostream>
#include <cmath>
using namespace std;
struct Point
{
    int x,y;  
};
double distance(Point a,Point b)
{
    return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
int main()
{
    int n,m,a,b;
    cin>>n>>m;
    Point p[1001],ask;
    for(int i=0;i<n;i++)
        cin>>p[i].x>>p[i].y;
    while(m--)
    {
        cin>>ask.x>>ask.y;
        double dist=distance(p[0],ask);
        int flag=0;
        for(int i=0;i<n;i++)
        {
            if(dist>distance(p[i],ask))
            {
                flag=i;
                dist=distance(p[i],ask);
            }
        }
        cout<<flag+1<<endl;
    }
    
}

b978: 7.最終任務->c.重組提示

程式碼:
#include <iostream>
using namespace std;

int main()
{
    string s,ss;
    int command;
    while(cin>>s)
    {
        ss=s;
        for(int i=0;i<s.size();i++)
        {
            cin>>command;
            ss[command-1]=s[i];
        }
        cout<<ss<<endl;
    }
}

c508: 去蟲

解題心得:
map反過來輸入的寫法:
for(map<int,int>::reverse_iterator rit=m.begin();rit!=m.rend();rit++)
    cout<<rit->first<<endl;

程式碼:
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    int n,arr[100001];
    map<int,int> m;
    map<int,int>::reverse_iterator it;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>arr[i];
    for(int i=0;i<n;i++)
        m[arr[i]]++;
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    for(it=m.rbegin();it!=m.rend();it++)
        cout<<it->first<<" ";
}

2020年8月24日 星期一

10905 - Children's Game

程式碼:
#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(string a,string b)
{
    return (a+b)>(b+a);
}
int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0) break;
        string l[51];
        for(int i=0;i<n;i++)
            cin>>l[i];
        sort(l,l+n,cmp);
        for(int i=0;i<n;i++)
            cout<<l[i];
        cout<<endl;
    }
    return 0;
}

d550: 物件排序

解題心得:
可以用內建sort,但要稍微優化IO。
vector二維初始化方式。

程式碼:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n,m,tmp;
    while(cin>>n>>m)
    {
        vector<vector<int>> l;
        for(int i=0;i<n;i++)
        {
            vector<int> ll;
            for(int j=0;j<m;j++)
            {
                cin>>tmp;
                ll.push_back(tmp);
            }
            l.push_back(ll);
        }
        sort(l.begin(),l.end());
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                cout<<l[i][j]<<" ";
            cout<<'\n';
        }
    }
    return 0;
}

2020年8月23日 星期日

b265: Q11286 - Conformity

程式碼:
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n,tmp,index[5];
    while(cin>>n)
    {
        if(n==0) break;
        map<string,int> m;
        map<string,int>::iterator it;
        while(n--)
        {
            string temp;
            for(int i=0;i<5;i++)
                cin>>index[i];
            sort(index,index+5);
            for(int i=0;i<5;i++)
                temp+=to_string(index[i]);
            it=m.find(temp);
            if(it!=m.end()) (it->second)++;
            else m[temp]=1;
        }
        int max=0,ans=0;
        for(it=m.begin();it!=m.end();it++)
            if(it->second>max) max=it->second;
        for(it=m.begin();it!=m.end();it++)
            if(it->second==max) ans+=max;
        cout<<ans<<'\n';
    }
    
    return 0;
}

d517: 文字抄寫 I

程式碼:
#include <iostream>
#include <map>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    string temp;
    while(cin>>n)
    {
        int len=1;
        map<string,int> m;
        map<string,int>::iterator it;
        while(n--)
        {
            cin>>temp;
            it=m.find(temp);
            if(it!=m.end())
                cout<<"Old! "<<it->second<<'\n';
            else
            {
                cout<<"New! "<<len<<'\n';
                m[temp]=len++;
            }
        }
    }
    
    return 0;
}

2020年8月20日 星期四

d502: 第三題:產品包裝

解題心得:
邊長=4的每個自己一箱,沒有剩餘空間。
邊長=3的每個自己一箱,每個剩下4*4*4-3*3*3 = 37 的空間
邊長=2的一箱可裝8個,若無法裝滿則剩下64-8*(b%8) 的空間
邊長=1的則是見縫插針,如果上面剩的空間夠就不用多一箱,不夠就多一箱。

程式碼:
#include <iostream>
using namespace std;


int main()
{
    int a,b,c,d;
    while(cin>>a>>b>>c>>d)
    {
        int ans=0,space=0;
        ans+=d;
        ans+=c;
        space+=37*c;
        ans+=(b%8==0?b/8:b/8+1);
        space+=64-8*(b%8);
        if(a>space) ans++;
        cout<<ans<<endl;
    }
    return 0;
}

c776: 106北二1.六邊形屋瓦

解題心得:
國高中數學題變形。
先觀察n=1時,a1=6, d=4
而n每增加1,a1就增加5,因此a1 = 6+(n-1)*5 = 5n+1
至於公差d,則是每次增加3,因此d = 4+(n-1)*3 = 3*n+1

所求由等比公式得知為: a1+(m-1)*d = (5n+1)+(m-1)(3n+1)

程式碼:
#include <iostream>
using namespace std;


int main()
{
    int n,m;
    cin>>n>>m;
    cout<<(5*n+1)+(m-1)*(3*n+1)<<endl;
    return 0;
}

2020年8月18日 星期二

operator overloading

自定義物件的加減乘除等操作。

operator+

例如:
const Complex operator+(const Complex &obj); //member function

使用const+參考是為了效率。

還可以寫成friend function:
friend Comples operator+(Complex &o1,Complex &o2);

記得在外部定義即可:
Complex operator+(Complex &o1,Complex &o2)
{
    return Complex(o1.real+o2.real,o1.imag+o2.imag); // 回傳沒有名字的物件
}

(note:
由上面的例子可得知,constructor本身並非void function,它會回傳沒有名字的物件。)


上面討論的都是 c3=c1+c2,也就是兩個參數都是object的時候。
如果是c3=c1+x呢?(x為任意int)
Complex Complex::operator+(int& x); //裡面傳的參數資料型態改一下

如果是c3=x+c2呢?
原本上面兩個狀況都是由c1這個物件呼叫operator+這個函式,有點像c1.operator+(c2)的感覺,所以才用member function。
現在則沒有物件能呼叫了,所以才用外部定義的friend function。
Complex operator+(int& x,Complex& o);

(note:
c3=(c1+c2) // c3 和 (c1+c2) 是不同物件。(c1+c2)是一個沒有名字的物件,它的值會被拿去給c3)


operator++

有prefix跟postfix兩種,為了區別於是寫法如下,只差在有沒有再傳參數進去。
Point& operator++() // prefix
{
    count++;
    return *this;
}
(note: 回傳值為參考)
Point operator++(int n) // postfix
{
    Point tmp=*this;
    this->count++;
    return tmp;
}
(note: postfix 的 operator++回傳值不是參考!!否則會出現錯誤訊息:"warning: reference to local variable ‘result’ returned [-Wreturn-local-addr] ",因為若回傳tmp參考,tmp本身會再動作結束後消失造成錯誤!!)


可以簡化來看:
operator++()
operator++(int)
值得注意的是為何postfix operator需要額外的傳int進去呢?這是因為C++判斷function的時候,是由它們的signature,也就是參數數量、型別與順序,而非回傳值來看。
參考:overloading postfix and prefix operators
(the function overloading can be achieved by different data type and different number of argument list, but it cannot be different return type.)

operator>> & <<

friend istream& operator>>(istream& in,Complex &c)
{
    in>>c.real>>c.imag;
    return in;
}
friend ostream& operator<<(ostream& out,Complex &c)
{
    out<<c.real<<" "<<c.imag<<endl;
   return out;
}
(note:return in跟return out是為了連續輸入/輸出)

2020年8月16日 星期日

a870: 10. List Maker

解題心得:
vector find: find(vec.begin(),vec.end(),x);
vector insert: vec.insert(it,x); // or vec.insert(vec.begin()+n,x);
vector erase: vec.erase(it) // or vec.erase(vec.begin()+n);

程式碼:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    string command,x,n;
    vector<string> l;
    while(cin>>command)
    {
        if(command=="SHOW") break;
        if(command=="ADD")
        {
            cin>>x;
            l.push_back(x);
        }
        else if(command=="INSERT")
        {
            cin>>x>>n;
            vector<string>::iterator it=find(l.begin(),l.end(),n);
            l.insert(it,x);
        }
        else if(command=="REMOVE")
        {
            cin>>x;
            for(int i=0;i<l.size();i++)
            {
                if(l[i]==x)
                {
                    l.erase(l.begin()+i);
                    break;
                }
            }
        }
    }
    for(int i=0;i<l.size();i++)
        cout<<l[i]<<" ";
    return 0;
}

a787: 9. Mirror to the Stars

程式碼:
#include <iostream>
using namespace std;

int main()
{
    string name,command;
    int width,height;
    char picture[37][25];
    while(cin>>name>>width>>height>>command)
    {
        for(int i=0;i<height;i++)
            for(int j=0;j<width;j++)
                cin>>picture[i][j];
        cout<<name<<endl;
        for(int l=0;l<command.size();l++)
        {
            char copy[37][25];
            for(int i=0;i<height;i++)
                for(int j=0;j<width;j++)
                    copy[i][j]=picture[i][j];
            if(command[l]=='R')
            {
                for(int i=0;i<height;i++)
                    for(int j=0;j<width;j++)
                        picture[i][j]=copy[i][width-1-j];
            }
            else
            {
                for(int i=0;i<height;i++)
                    for(int j=0;j<width;j++)
                        picture[i][j]=copy[height-1-i][j];
            }
        }
        for(int i=0;i<height;i++)
        {
            for(int j=0;j<width;j++)
                cout<<picture[i][j];
            cout<<endl;
        }
    }
    return 0;
}

2020年8月14日 星期五

12918 - Lucky Thief

程式碼:
#include <iostream>
using namespace std;


int main()
{
    int t,n,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        long long int up=m-1,down=m-n;
        
        cout<<(up+down)*n/2<<endl;
    }
    return 0;
}

11498 - Division of Nlogonia

程式碼:
#include <iostream>
using namespace std;


int main()
{
    int k,n,m,x,y;
    while(cin>>k)
    {
        if(k==0) break;
        cin>>n>>m;
        for(int i=0;i<k;i++)
        {
            cin>>x>>y;
            if(x==n||y==m) cout<<"divisa"<<endl;
            else if(x>n&&y>m) cout<<"NE"<<endl;
            else if(x>n&&y<m) cout<<"SE"<<endl;
            else if(x<n&&y>m) cout<<"NO"<<endl;
            else cout<<"SO"<<endl;
        }
    }
    return 0;
}

263 - Number Chains

程式碼:
#include <iostream>
#include <vector>
using namespace std;


int main()
{
    
    string s;
    while(cin>>s)
    {
        if(s=="0") break;
        cout<<"Original number was "<<s<<endl;
        string bigToSmall=s,smallToBig=s;
        int chain=0;
        vector<string> list;
        while(1)
        {
            
            chain++;
            bigToSmall=s,smallToBig=s;
            for(int i=0;i<bigToSmall.size();i++)
            {
                for(int j=0;j<bigToSmall.size();j++)
                {
                    if(bigToSmall[i]>bigToSmall[j]) swap(bigToSmall[i],bigToSmall[j]);
                    if(smallToBig[i]<smallToBig[j]) swap(smallToBig[i],smallToBig[j]);
                }
                    
            }
            int big=stoi(bigToSmall),small=stoi(smallToBig),repeat=0;
            s=to_string(big-small);
            cout<<big<<" - "<<small<<" = "<<s<<endl;
            for(int i=0;i<list.size();i++)
            {
                if(s==list[i])
                {
                    repeat=1;
                    break;
                }
            }
            if(repeat) break;
            list.push_back(s);
        }
        cout<<"Chain length "<<chain<<endl<<endl;
    }
    return 0;
}

friend

若某個函式是某class的friend,那個函式就可以存取到private的資料。
它會被宣告在class外面,就像一般的函式一樣。不過在class裡面會多加一個friend在前面。
寫在private或public部分沒有影響,畢竟它不是member function,只是friend。

class CPoint
{
private:
    int x,y;
    friend CPoint offset(CPoint &pt,int diff);
public:
    CPoint(){x=0,y=0;}
    CPoint(int a,int b){x=a,y=b;}
    void Print(){cout<<x<<" "<<y<<endl;}
};
CPoint offset(CPoint &pt,int diff)
{
    pt.x+=diff;pt.y+=diff;
    return pt;
}
int main()
{
    CPoint p1(3,4);
    p1.Print();
    offset(p1,10);
    p1.Print();
    return 0;
}

friend除了用在function上,還能用在class。
如果:
class A
{
    friend class B;
    // other code....
};
那class B裡的所有member function都能取用class A的private資料。
但friend的關係並非雙向!

2020年8月13日 星期四

【Hamilton】The World Was Wide Enough 歌詞筆記

sign on:擔任
number two:副手
terrain:地形
marksman:神槍手
rigor:嚴格的
methodically:有條不紊的
fiddle:撥弄
seconds:副手
send in:派出
set the record straight:澄清是非
take aim:瞄準
wise up:領悟
usher:迎來
wailing:哀號、哭泣
obliterate:抹去不留痕跡

10093 - An Easy Problem!

解題心得:
解題推導參考網路。

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    while(getline(cin,s))
    {
        int sum=0,temp,max=1;
        for(int i=0;i<s.size();i++)
        {
            if('0'<=s[i]&&s[i]<='9') temp=s[i]-'0';
            else if('A'<=s[i]&&s[i]<='Z') temp=s[i]-'A'+10;
            else if('a'<=s[i]&&s[i]<='z') temp=s[i]-'a'+36;
            else continue;
            
            if(temp>max) max=temp;
            sum+=temp;
        }
        for(int i=max;i<=62;i++)
        {
            if(sum%i==0)
            {
                cout<<(i+1)<<endl;
                break;
            }
            else if(i==62) cout<<"such number is impossible!"<<endl;
        }
    }
    return 0;
}

const member function && const static function

class 的 object 也能像 int, double 等資料型態一樣,被宣告成 const,這代表該object的資料不能被任意更動。
因此只有不會更動到資料的函式,也就是const member function能被這種const object呼叫。

宣告const member function的語法就是在()與{}間加上const:

class ID{
private:
    string name;
public:
    ID(string n){name=n;}
    string getName() const {return name;}
};

這樣一來,如果要在getName()這個函式修改值,像是把name="unknown"的話,就會出現編譯錯誤的訊息。

另外,由於const object不能使用一般的 member function,但 const object 跟 non-const object 都能使用 const member function,在寫某些不會更動值的函式像是accessor時,可以直接加個const。

參考:
https://www.geeksforgeeks.org/const-member-functions-c/


_______________________

static 通常不會單獨出現,而是配合著 const 。
宣告也是在外面,不過是 const + data type 一起。
class CScore { 
protected:
    //declare a static constant
    static const int Max;
};
const int CScore::Max = 100; //outside class

2020年8月12日 星期三

static data members & static function

class X {
private:
    static int count;
};
int X::count = 0;

static 的資料屬於所有 class object共享,就好像 class 裡的 member function一樣。又因為在定義 class 時還沒有對資料賦值,因此初始化要寫在 class 的定義外面。

class CNum {
public:
    CNum(int a) { x = a; y += x; }
    static void fun(CNum m) {
        cout << m.x << "vs." << y << endl; }
private:
    int x;
    static int y;
};
int CNum::y = 0;

int main()
{
    CNum O1(4), O2(7);
    CNum::fun(O1);
    CNum::fun(O2);
}
既然所有的object都共享同一份static 變數,要怎麼存取它呢?
可以用static的member function來存取它,如上面code。
在同一個 class 裡的member function其實本來就是共享的,加上static是為了處理 static 的資料。

12019 - Doom's Day Algorithm

程式碼:
#include <iostream>
using namespace std;


int main()
{
    int cases,index[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    string week[7]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
    cin>>cases;
    while(cases--)
    {
        int m,d,total=0;
        cin>>m>>d;
        for(int i=0;i<m-1;i++)
            total+=index[i];
        total+=d;
        cout<<week[(total-1)%7]<<endl;
    }
}

11349 - Symmetric Matrix

程式碼:
#include <iostream>
using namespace std;


int main()
{
    char ignore;
    int t;
    cin>>t;
    for(int rounds=1;rounds<=t;rounds++)
    {
        cout<<"Test #"<<rounds<<": ";
        int n,flag=0;
        long long int matrix[101][101];
        cin>>ignore>>ignore>>n;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                cin>>matrix[i][j];
        
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(matrix[i][j]!=matrix[n-1-i][n-1-j]||matrix[i][j]<0)
                    flag=1;
            }
        }
        if(flag) cout<<"Non-symmetric."<<endl;
        else cout<<"Symmetric."<<endl;
    }
}

2020年8月11日 星期二

11321 - Sort! Sort!! and Sort!!!

解題心得:
1.使用內建sort()
2.負數mod出來也會是負數
3.比較時有四種狀況:不同餘、同餘皆奇、同餘皆偶、同餘一奇一偶

程式碼:
#include <iostream>
#include <algorithm>
using namespace std;

int n,m;
bool cmp(int a,int b)
{
    if(a%m!=b%m) return a%m<b%m;
    if(abs(a%2)==1&&abs(b%2)==1) return a>b;
    if(abs(a%2)==0&&abs(b%2)==0) return a<b;
    return abs(a%2);
}
int main()
{
    int l[10001];
    while(cin>>n>>m)
    {
        cout<<n<<" "<<m<<endl;
        if(n==0&&m==0) break;
        for(int i=0;i<n;i++)
            cin>>l[i];
        sort(l,l+n,cmp);
        
        for(int i=0;i<n;i++)
            cout<<l[i]<<endl;
    }
    return 0;
}

function pointer

#include <iostream>
using namespace std;

double f()
{
    return 8.7;
}
int main()
{
    double (*pF)();
    pF=f; // or pF=&f
    double result=(*pF)();
    cout<<result<<endl;
    return 0;
}

執行結果:8.7

2020年8月10日 星期一

# 讀報 What Footage of the Beirut Explosion Tells Us About the Blast

https://www.nytimes.com/2020/08/05/video/beirut-explosion-footage.html

a plume of smoke:羽狀煙霧
displace:迫使...離開常居地
rip through:快速猛烈的穿透地方或建築物
envelop:環繞、包圍
confiscate from:沒收、充公
stockpile:儲存
welding:焊接
military grade:軍用級
silo:穀倉
gaping:(洞或開口)大的
Container Terminal:貨櫃中心
capsize:覆沒
detonation:引爆
mosque:清真寺

118 - Mutant Flatworld Explorers

解題心得:
用陣列紀錄如果超出範圍的前一個座標,如果那個座標曾被標記過,代表這次可以忽略。

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int w,h,x,y,map[51][51]={0};
    char direction;
    cin>>w>>h;
    while(cin>>x>>y>>direction)
    {
        int lost=0;
        string command;
        cin>>command;
        for(int i=0;i<command.size();i++)
        {
            if(command[i]=='L')
            {
                switch(direction)
                {
                    case 'N':
                        direction='W';break;
                    case 'E':
                        direction='N';break;
                    case 'S':
                        direction='E';break;
                    case 'W':
                        direction='S';break;
                }
                    
            }
            else if(command[i]=='R')
            {
                switch(direction)
                {
                    case 'N':
                        direction='E';break;
                    case 'E':
                        direction='S';break;
                    case 'S':
                        direction='W';break;
                    case 'W':
                        direction='N';break;
                }
            }
            else if(command[i]=='F')
            {
                int nextX=x,nextY=y;
                if(direction=='N') nextY++;
                else if(direction=='E') nextX++;
                else if(direction=='S') nextY--;
                else if(direction=='W') nextX--;
                if(nextX>w||nextX<0||nextY>h||nextY<0)
                {
                    if(map[x][y]==1)
                        continue;
                    else
                    {
                        lost=1;
                        map[x][y]=1;
                        break;
                    }
                }
                x=nextX,y=nextY;
            }
        }
        cout<<x<<" "<<y<<" "<<direction<<(lost?" LOST":"")<<endl;
    }
    return 0;
}

2020年8月8日 星期六

299 - Train Swapping

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int l,train[51],times=0;
        cin>>l;
        for(int i=0;i<l;i++)
            cin>>train[i];
        for(int i=0;i<l;i++)
        {
            for(int j=i;j<l;j++)
            {
                if(train[i]>train[j])
                {
                    swap(train[i],train[j]);
                    times++;
                }
            }
        }
        cout<<"Optimal train swapping takes "<<times<<" swaps."<<endl;
    }
    return 0;
}

2020年8月7日 星期五

10409 - Die Game

解題心得:
可以自己手做一個小骰子模擬。

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int num;
    while(cin>>num)
    {
        if(num==0) break;
        int top=1,n=2,w=3,bottom=7-top,s=7-n,e=7-w;
        while(num--)
        {
            string str;
            cin>>str;
            if(str=="north")
            {
                int temp=n;
                n=top;
                top=s;
                s=bottom;
                bottom=temp;
            }
            else if(str=="south")
            {
                int temp=s;
                s=top;
                top=n;
                n=bottom;
                bottom=temp;
            }
            else if(str=="east")
            {
                int temp=e;
                e=top;
                top=w;
                w=bottom;
                bottom=temp;
            }
            else if(str=="west")
            {
                int temp=w;
                w=top;
                top=e;
                e=bottom;
                bottom=temp;
            }
            
        }
        cout<<top<<endl;
    }
    return 0;
}

10193 - All You Need Is Love

解題心得:
1.二進位轉十進位(& string to int)
2.gcd

程式碼:
#include <iostream>
#include <string>
using namespace std;
int to_dec(string s)
{
    int expo=1,sum=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        sum+=(s[i]-'0')*expo;
        expo*=2;
    }
    return sum;
}
int gcd(int a,int b)
{
    while((a%=b)!=0&&(b%=a)!=0);
    return a+b;
}
int main()
{
    int n;
    cin>>n;
    for(int cases=1;cases<=n;cases++)
    {
        string s1,s2;
        int n1,n2;
        cin>>s1>>s2;
        n1=to_dec(s1),n2=to_dec(s2);
        cout<<"Pair #"<<cases<<": ";
        if(gcd(n1,n2)!=1) cout<<"All you need is love!"<<endl;
        else cout<<"Love is not all you need!"<<endl;
    }
    return 0;
}

2020年8月6日 星期四

copy constructor & copy assignment constructor & default constructor

copy constructor
A::A(const A& a)
{
    // code
}

使用時機:有指標的時候

使用方法:
A a;
a.setValue(.....); // 初始化a
A b(a);

注意:跟Copy Assignment Operator不一樣!!
copy constructor是一種constructor,顧名思義是在初始化的時候用的。而copy assignment operator則是跟 = 有關。


copy assignment constructor
A& A::operator=(const A& a)
{
    //code
    return *this;
}
note:回傳參考!!
參考連結:
http://jyleef.blogspot.com/2012/04/copy-constructor.html
[C++筆記] 拷貝建構式(Copy Constructor) & 複製指派運算子(Copy Assignment Operator)


default constructor
如果沒有宣告constructor的話,會自動生成一個沒有任何參數的constructor,像這樣:
A::A(){;}
但如果有宣告constructor的話,就不會自動生成。

11005 - Cheapest Base

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int cases;
    while(cin>>cases)
    {
        for(int rounds=1;rounds<=cases;rounds++)
        {
            cout<<"Case "<<rounds<<":"<<endl;
            int index[36];
            for(int i=0;i<36;i++)
                cin>>index[i];
            int num;
            cin>>num;
            while(num--)
            {
                int n,min_cost=1000000000,min_base=100;
                cin>>n;
                cout<<"Cheapest base(s) for number "<<n<<":";
                for(int i=2;i<=36;i++)
                {
                    int cost=0,number=n;
                    while(number>0)
                    {
                        cost+=index[number%i];
                        number/=i;
                    }
                    if(cost<min_cost) min_cost=cost,min_base=i;
                }
                for(int i=2;i<=36;i++)
                {
                    int cost=0,number=n;
                    while(number>0)
                    {
                        cost+=index[number%i];
                        number/=i;
                    }
                    if(cost==min_cost) cout<<" "<<i;
                }
                cout<<endl;
            }
            if(rounds!=cases) cout<<endl;
        }

    }
    return 0;
}

2020年8月5日 星期三

【Hamilton】Farmer Refuted 歌詞筆記

Refuted:駁斥
hear ye:官方發言前的開場詞,尤其在法庭上
heed:注意(警告或建議)
rabble:烏合之眾、暴民
at heart:在心裡
bloodshed:殺戮
astray:誤入歧途
unravel:破壞、拆散
have-nots:富人與窮人
straight face:故意板起的臉孔
thee:你
mange:癬
divisive:引起分歧的
indecisive:優柔寡斷的
niceties:美好、精美

11063 - B2-Sequence

程式碼:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n,arr[101],times=1;
    while(cin>>n)
    {
        int flag=1;
        for(int i=0;i<n;i++)
            cin>>arr[i];
        for(int i=0;i<n-1;i++)
        {
            if(arr[i]>0&&arr[i]<arr[i+1]);
            else flag=0;
        }
        vector<int> sq;
        for(int i=0;i<n;i++)
        {
            for(int j=i;j<n;j++)
            {
                
                if(find(sq.begin(),sq.end(),arr[i]+arr[j])!=sq.end())
                    flag=0;
                else
                    sq.push_back(arr[i]+arr[j]);
            }
        }
        cout<<"Case #"<<times++<<": ";
        if(flag==0) cout<<"It is not a B2-Sequence."<<endl;
        else cout<<"It is a B2-Sequence."<<endl;
        cout<<endl;
    }
    return 0;
}

10057 - A mid-summer night's dream.

解題心得:
好像是高中數學?

程式碼:
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n,arr[1000001];
    while(cin>>n)
    {
        int A,two=0,three=0;
        for(int i=0;i<n;i++)
            cin>>arr[i];
        sort(arr,arr+n);
        if(n%2) // odd
        {
            A=arr[n/2];
            for(int i=0;i<n;i++)
            {
                if(arr[i]==A) two++;
            }
            three=1;
        }
        else // even
        {
            A=arr[n/2-1];
            for(int i=0;i<n;i++)
            {
                if(arr[i]==arr[n/2]||arr[i]==arr[n/2-1])
                    two++;
            }
            three=arr[n/2]-arr[n/2-1]+1;
        }
        cout<<A<<" "<<two<<" "<<three<<endl;
    }
    return 0;
}

2020年8月4日 星期二

948 - Fibonaccimal Base

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int f[40],n,num;
    f[0]=f[1]=1;
    for(int i=2;i<40;i++)
        f[i]=f[i-1]+f[i-2];
    cin>>n;
    while(n--)
    {
        int start=0;
        cin>>num;
        cout<<num<<" = ";
        for(int i=39;i>=1;i--)
        {
            if(num/f[i]) cout<<"1",start=1;
            else if(start) cout<<"0";
            num%=f[i];
        }
        cout<<" (fib)"<<endl;
    }
    return 0;
}

10189 - Minesweeper

解題心得:
注意最後一筆測資後不需換行。

程式碼:

[解一]
#include <iostream>
using namespace std;

int main()
{
    int n,m,times=1;
    while(cin>>n>>m)
    {
        if(n==0&&m==0) break; 
        if(times!=1) cout<<endl;
        char map[105][105];
        for(int i=0;i<100;i++)
            for(int j=0;j<100;j++)
                map[i][j]='0';
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>map[i][j];
        cout<<"Field #"<<times++<<":"<<endl;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(map[i][j]=='*') cout<<map[i][j];
                else
                {
                    int count=0;
                    for(int x=i-1;x<=i+1;x++)
                    {
                        for(int y=j-1;y<=j+1;y++)
                        {
                            if(map[x][y]=='*') count++;
                        }
                    }
                    cout<<count;
                }
            }
            cout<<endl;
        }
    }
    return 0;
}
[解二]
#include <iostream>
using namespace std;

int main()
{
	int n, m, counter = 1;
	int dx[8] = { -1,-1,-1,0,0,1,1,1 }, dy[8] = { -1,0,1,-1,1,-1,0,1 };
	while (cin >> n >> m && n && m)
	{
		if (counter != 1) cout << endl;
		char arr[101][101];
		for (int i = 0; i < 101; i++)
			for (int j = 0; j < 101; j++)
				arr[i][j] = '0';
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				cin >> arr[i][j];
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				if (arr[i][j] != '*')
				{
					int total = 0;
					for (int k = 0; k < 8; k++)
					{
						if (arr[i + dx[k]][j + dy[k]] == '*')
							total++;
					}
					arr[i][j] = total + '0';
				}
			}
		}
		cout << "Field #" << counter++ << ":" << endl;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
				cout << arr[i][j];
			cout << endl;
		}
	}
	return 0;
}

2020年8月3日 星期一

【Hamilton】Aaron Burr, Sir 歌詞筆記

I'm at your service:願為您效勞
sort of:有點
out of sorts:心情不佳
bursar:(學校、大學裡的)財務主管
dying wish:遺願
bargained for:預料(常與more than連用)
buy you a drink:請你喝一杯
run their mouths off:太多話
pint:品脫
Sam Adams:啤酒名
redcoats:英國軍人
chicka:語助詞
cop:警察
lock up:鎖好
corsets:束腹
brew:啤酒
prodigy:神童
verse:韻文
Good luck with that:phrase that people say when you're going to try something that they think will be hard or impossible.
imminent:即將來臨

d141: Linearity

解題心得:
看兩條線是否相等,就是列等式看一不一樣。
因為有除以零或者兩點相同的可能,所以把兩邊等式交叉相乘,直接看乘積是否相等。

程式碼:
#include<iostream>
using namespace std;
typedef struct
{
    int x,y;
}Point;
int main()
{
 int cases;
 char ignore;
 cin>>cases;
 while(cases--)
 {
     Point p1,p2,p3;
     cin>>p1.x>>ignore>>p1.y>>p2.x>>ignore>>p2.y>>p3.x>>ignore>>p3.y;
     double m1,m2;
     
     if((p2.y-p1.y)*(p3.x-p2.x)==(p2.x-p1.x)*(p3.y-p2.y))
         cout<<"collinear"<<endl;
     else cout<<"not collinear"<<endl;
 }
 return 0;
}

10268 - 498-bis

解題心得:
記得再寫一次這題。

程式碼:
#include<iostream>
using namespace std;
int a[1000000];
int derivative(int x,int max)
{
 long long sum=0,exp=1;
 int i;
 for(i=max-1;i>=0;i--)
 {
  sum+=a[i]*exp*(max-i);
  exp*=x;
 }
 return sum;
}
int main()
{
 int x,n;
 while(cin>>x)
 {
  for(n=0;;n++)
  {
   cin>>a[n];
   if(getchar()=='\n')
    break;
  }
  cout<<derivative(x,n)<<endl;
 }
 return 0;
}

2020年8月2日 星期日

簡易翻牌遊戲

使用說明:
總共36格,內為A~R x2 隨機排列的表格。
索引從0開始,35結束。
依指示輸入想翻開的卡牌,若該次兩張牌相同,則成功找出;反之則蓋回去。
沒有防呆,不能輸入0~35外的數字、已被翻開的卡牌索引、每次不得輸入兩相同索引。
(也許還有其他錯誤?)

程式碼:
#include <iostream>
using namespace std;

int main()
{
    srand(time(NULL));
    int index[18]={0},counter=0;
    char map[6][6]={'*'},alpha[6][6];
    for(int i=0;i<6;i++)
        for(int j=0;j<6;j++)
            map[i][j]='*',alpha[i][j]='*';
    for(int i=0;i<6;i++)
    {
        for(int j=0;j<6;j++)
        {
            int letter=rand()%18;
            while(index[letter]>=2)
                letter=rand()%18;
            alpha[i][j]=char(letter+'A');
            index[letter]++;
        }
    }
    
    for(int i=0;i<6;i++)
    {
        for(int j=0;j<6;j++)
            cout<<alpha[i][j]<<" ";
        cout<<endl;
    }
    while(counter<36)
    {
        int cardIndex1,cardIndex2;
        cout<<"Please enter card index: ";
        cin>>cardIndex1;
        for(int i=0;i<6;i++)
        {
            for(int j=0;j<6;j++)
            {
                if(cardIndex1/6==i&&cardIndex1%6==j)
                    cout<<alpha[i][j]<<" ";
                else
                    cout<<map[i][j]<<" ";
            }
            cout<<endl;
        }
        cout<<"Please enter card index: ";
        cin>>cardIndex2;
        for(int i=0;i<6;i++)
        {
            for(int j=0;j<6;j++)
            {
                if(cardIndex1/6==i&&cardIndex1%6==j)
                    cout<<alpha[i][j]<<" ";
                else if(cardIndex2/6==i&&cardIndex2%6==j)
                    cout<<alpha[i][j]<<" ";
                else
                    cout<<map[i][j]<<" ";
            }
            cout<<endl;
        }
        if(alpha[cardIndex1/6][cardIndex1%6]==alpha[cardIndex2/6][cardIndex2%6])
        {
            cout<<"Good Job!"<<endl;
            counter+=2;
            map[cardIndex1/6][cardIndex1%6]=alpha[cardIndex1/6][cardIndex1%6];
            map[cardIndex2/6][cardIndex2%6]=alpha[cardIndex2/6][cardIndex2%6];
        }
        else cout<<"Try Again!"<<endl;
    }
    cout<<"Congratulation!!"<<endl;
}

2020年8月1日 星期六

10908 - Largest Square

解題心得:
細節要注意。

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int t,m,n,q,r,c;
    cin>>t;
    while(t--)
    {
        char map[100][100]={'0'};
        cin>>m>>n>>q;
        cout<<m<<" "<<n<<" "<<q<<endl;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                cin>>map[i][j];
        while(q--)
        {
            cin>>r>>c;
            int length=0,out=0;
            char target=map[r][c];
            while(1)
            {
                int i,j;
                for(i=r-length;i<=r+length;i++)
                {
                    for(j=c-length;j<=c+length;j++)
                    {
                        if(map[i][j]!=target)
                        {
                            out=1;
                            break;
                        }
                        else if(i<0||j<0||i>=m||j>=n)
                        {
                            out=1;
                            break;
                        }
                    }
                    if(out) break;
                }
                if(out)
                {
                    length--;
                    break;
                }
                length++;
            }
            cout<<length*2+1<<endl;
        }

    }
    return 0;
}

2020年7月31日 星期五

10242 - Fourth Point !!

程式碼:
#include <iostream>
#include <iomanip>
using namespace std;
typedef struct
{
    double x,y;
}Point;
int main()
{
    Point p1,p2,p3,p4;
    cout<<fixed<<setprecision(3);
    while(cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y>>p4.x>>p4.y)
    {
        if(p1.x==p3.x&&p1.y==p3.y) swap(p1,p2);
        else if(p1.x==p4.x&&p1.y==p4.y) swap(p1,p2),swap(p3,p4);
        else if(p2.x==p4.x&&p2.y==p4.y) swap(p3,p4);
        Point p5;
        p5.x=p1.x+p4.x-p2.x;
        p5.y=p1.y+p4.y-p2.y;
        cout<<p5.x<<" "<<p5.y<<endl;
    }
    return 0;
}

2020年7月30日 星期四

【Hamilton】You'll Be Back 歌詞筆記

hurl:丟
go by:經過
estrangement:疏遠;隔閡
time will tell:時間會證明一切
serve ...... well:對人有所幫助、助益
through it all:經歷一切
when push comes to shove:一旦不得已時
change the subject:改變話題
subject:話題、臣民
until my dying day:一直到死

c660: 墨西哥波浪舞

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    getline(cin,s);
    for(int i=0;i<s.size();i++)
    {
        if(s[i]==' ')
        {
            //cout<<s<<endl;
            continue;
        }
        for(int j=0;j<s.size();j++)
        {
            if(s[j]==' ') cout<<s[j];
            else if(i==j) // capital
            {
                if('a'<=s[j]&&s[j]<='z') cout<<char(s[j]-'a'+'A');
                else cout<<s[j];
            }
            else //non-capital
            {
                if('a'<=s[j]&&s[j]<='z') cout<<s[j];
                else cout<<char(s[j]-'A'+'a');
            }
        }
        cout<<endl;
    }
    return 0;
}

10056 - What is the Probability ?

程式碼:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    double s,n,p,k,q;
    cin>>s;
    cout<<fixed<<setprecision(4);
    while(s--)
    {
        double r,a0;
        cin>>n>>p>>k;
        q=1-p;
        if(pow(q,n)==1) 
            cout<<"0.0000"<<endl;
        else
            cout<<p*pow(q,k-1)/(1-pow(q,n))<<endl;
    }
    return 0;
}

10038 - Jolly Jumpers

程式碼:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n,tmp;
    while(cin>>n)
    {
        vector<int> sq;
        vector<int> diff;
        for(int i=0;i<n;i++)
        {
            cin>>tmp;
            sq.push_back(tmp);
        }
        for(int i=0;i<sq.size()-1;i++)
            diff.push_back(abs(sq[i]-sq[i+1]));
        sort(diff.begin(),diff.end());
        int i;
        for(i=0;i<n-1;i++)
        {
            if(i+1!=diff[i]) break;
        }
        if(i==n-1) cout<<"Jolly"<<endl;
        else cout<<"Not jolly"<<endl;
        
    }
    return 0;
}

2020年7月29日 星期三

10041 - Vito's Family

程式碼:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int r,cases,n,mid;
    cin>>cases;
    while(cases--)
    {
        cin>>r;
        vector<int> nightbor;
        for(int i=0;i<r;i++)
        {
            cin>>n;
            nightbor.push_back(n);
        }
        sort(nightbor.begin(),nightbor.end());
        mid=r/2;
        int sum=0;
        for(int i=0;i<nightbor.size();i++)
        {
            sum+=abs(nightbor[i]-nightbor[mid]);
        }
        cout<<sum<<endl;
    }
    return 0;
}

10190 - Divide, But Not Quite Conquer!

程式碼:
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        vector<int> sq;
        if(n<=1||m<=1)
        {
            cout<<"Boring!"<<endl;
            continue;
        }
        while(n%m==0)
        {
            sq.push_back(n);
            n/=m;
        }
        if(n!=1) cout<<"Boring!"<<endl;
        else
        {
            for(int i=0;i<sq.size();i++)
            {
                if(i!=sq.size()-1)
                    cout<<sq[i]<<" ";
                else
                    cout<<sq[i]<<" 1"<<endl;
            }
        }
    }
    return 0;
}

2020年7月28日 星期二

【Hamilton】The Room Where It Happens 歌詞筆記

Secretary:部長
good old:有名的人
renamed ... after:把...重新命名為...
whatever it takes:不惜代價
hate the sin, love the sinner:(甘地名言)
Virginians:維吉尼亞人
diametrically opposed:完全相反的
Bros:兄弟
unprecedented:史無前例的
pièce de résistance:最重要的事
how the sausage gets made:私底下做的事
doorstep:門口
disarray:混亂
nowhere else to turn:無處容身
join the fray:參與辯論
pieces:棋
grapple with sth:設法克服
quid pro quo:交換條件
see how it goes:看看事情會怎麼發展
In God we trust:(美國格言)我們信仰上帝
sell ...... down the river:出賣、背叛
skin in the game:(商業上)參與某事
outlive:活得比....長
fall for:相信
hold one’s nose:睜一隻眼閉一隻眼
save the day:扭轉危機
get a say:參與活動並影響或做出決策
trade away:賣掉、出賣

10221 - Satellites

解題心得:
弧長=2*pi*半徑*a/360
弦長=2*sin(a/2)*半徑

pi 可用2acos0得知
三角函數參數預設弧度,轉為角度要另外乘上pi/180

程式碼:
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

#define PI 2*acos(0)

int main()
{
    double s,a;
    string type;
    cout<<fixed<<setprecision(6);
    while(cin>>s>>a>>type)
    {
        if(a>180) a=360-a;
        if(type=="min") a/=60;
        cout<<2*PI*(s+6440)*(a/360.0)<<" "<<2*sin(a/2.0*PI/180)*(6440+s)<<endl;
    }
    return 0;
}

272 - TEX Quotes

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int count=0;
    string text;
    while(getline(cin,text))
    {
        for(int i=0;i<text.size();i++)
        {
            if(text[i]=='"')
            {
                if(count%2==0) cout<<"``";
                else cout<<"''";
                count++;
            }
            else
                cout<<text[i];
        }
        cout<<endl;
    }
    return 0;
}

2020年7月27日 星期一

10235 - Simply Emirp

程式碼:
#include <iostream>
#include <cmath>
using namespace std;

int isPrime(int n)
{
    for(int i=2;i<=n/2;i++)
    {
        if(n%i==0)
            return 0;
    }
    return 1;
}

int main()
{
    int n;
    while(cin>>n)
    {
        int reverse=0,copy=n;
        while(copy>0)
        {
            reverse=reverse*10+copy%10;
            copy/=10;
        }
        if(isPrime(n)&&isPrime(reverse)&&n!=reverse)
            cout<<n<<" is emirp."<<endl;
        else if(isPrime(n))
            cout<<n<<" is prime."<<endl;
        else
            cout<<n<<" is not prime."<<endl;
    }
    return 0;
}

10226 - Hardwood Species

程式碼:
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;


int main()
{
    int cases;
    cin>>cases;
    string tree,ignore;;
    getline(cin,ignore);
    cin.ignore();
    for(int i=0;i<cases;i++)
    {
        vector<string> species;
        vector<int> amounts;
        int total=0;
        while(getline(cin,tree))
        {
            if(tree.size()==0) break;
            total++;
            int l=0;
            for(l=0;l<species.size();l++)
            {
                if(species[l]==tree)
                {
                    amounts[l]++;
                    break;
                }
            }
            if(l==species.size())
            {
                species.push_back(tree);
                amounts.push_back(1);
            }
        }
        for(int j=0;j<species.size();j++)
        {
            for(int k=0;k<species.size();k++)
            {
                if(species[j]<species[k])
                {
                    swap(species[j],species[k]);
                    swap(amounts[j],amounts[k]);
                }
            }
        }
        for(int j=0;j<species.size();j++)
            cout<<species[j]<<" "<<fixed<<setprecision(4)<<amounts[j]/float(total)*100<<endl;
        if(i!=cases-1)
            cout<<endl;
    }
    return 0;
}

2020年7月25日 星期六

490 - Rotating Sentences

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int l=0,w=0;
    string s[100],temp;
    while(getline(cin,temp))
    {
        s[l++]=temp;
        if(temp.size()>w) w=temp.size();
    }
    for(int i=0;i<w;i++)
    {
        for(int j=l-1;j>=0;j--)
        {
            if(s[j].size()>i)
                cout<<s[j][i];
            else
                cout<<" ";
        }
        cout<<endl;
    }
    return 0;
}

11461 - Square Numbers

程式碼:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        if(a==0&&b==0) break;
        int sum=0;
        for(int i=a;i<=b;i++)
        {
            int sr=sqrt(i);
            if(sr*sr==i)
                sum++;
        }
        cout<<sum<<endl;
    }
    return 0;
}

2020年7月24日 星期五

10931 - Parity

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int l;
    while(cin>>l)
    {
        int parity=0;
        string binary;
        if(l==0) break;
        cout<<"The parity of ";
        while(l>0)
        {
            binary=(l%2==0?"0":"1")+binary;
            if(l%2) parity++;
            l/=2;
        }
        cout<<binary<<" is "<<parity<<" (mod 2)."<<endl;
    }
}

10252 - Common Permutation

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a,b;
    while(getline(cin,a))
    {
        getline(cin,b);
        int index1[26]={0},index2[26]={0};
        for(int i=0;i<a.size();i++)
            index1[a[i]-'a']++;
        for(int i=0;i<b.size();i++)
            index2[b[i]-'a']++;
        for(int i=0;i<26;i++)
        {
            if(index1[i]&&index2[i])
            {
                int len=index1[i]<index2[i]?index1[i]:index2[i];
                for(int j=0;j<len;j++)
                    cout<<char('a'+i);
            }
        }
        cout<<endl;
    }
}

10170 - The Hotel with Infinite Rooms

程式碼:
#include <iostream>
using namespace std;

int main()
{
    unsigned long long int s,d;
    while(cin>>s>>d)
    {
        unsigned long long int days=0;
        while(days<d)
        {
            days+=s;
            s++;
        }
        cout<<s-1<<endl;
    }
}

2020年7月23日 星期四

f148: 2. 定向越野 (Orienteering)

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int w,h,n,target=0;
    cin>>w>>h>>n;
    char map[10][10];
    for(int i=0;i<w;i++)
    {
        for(int j=0;j<h;j++)
        {
            cin>>map[i][j];
            if(map[i][j]!='0') target++;
        }
    }
    if(n>target) cout<<"Mission fail."<<endl;
    else
    {
        char object='a';
        for(int i=0;i<26;i++)
        {
            for(int j=0;j<w;j++)
            {
                for(int k=0;k<h&&n>0;k++)
                {
                    if(map[j][k]==char(object+i))
                    {
                        cout<<j<<" "<<k<<endl;
                        n--;
                        break;
                    }
                }
            }
        }
    }
}

10922 - 2 the 9s

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    while(cin>>s)
    {
        if(s=="0") break;
        cout<<s<<" is ";
        int times=1,sum=0;
        for(int i=0;i<s.size();i++)
            sum+=s[i]-'0';
        while(sum>=10)
        {
            times++;
            int copy=0;
            while(sum>0)
            {
                copy+=sum%10;
                sum/=10;
            }
            sum=copy;
        }
        if(sum!=9) cout<<"not a multiple of 9."<<endl;
        else cout<<"a multiple of 9 and has 9-degree "<<times<<".\n";
    }
}

11332 - Summing Digits

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0) break;
        while(n>=10)
            n=n%10+n/10;
        cout<<n<<endl;
    }
}

2020年7月22日 星期三

【Hamilton】My Shot 歌詞筆記

shot:機會、子彈
scrappy:好鬥
I got a lot of brains but no polish:我的才智尚未被開發、得以展現(?)
"I take "polish" a bit differently.

I think it means he is unrefined. Like he's smart but has no presence or ability to convey it yet. He needs polishing via formal education, to make the most of the intelligence he knows he possesses.

We all know those kids and adults who are clearly smart but don't quite put it together to make the most of their abilities. I think that's what he's saying here."--from here
brag:誇耀
holler:大聲喊叫
drop knowledge:To give information or share knowledge on a subject or just life in general(?)
diamond in the rough:未經琢磨的鑽石
coal:煤炭
unimpeachable:無可挑剔的
brandish:(興奮或威脅的)揮舞
famished:很餓的
fan the flames:搧風點火    ->fan this spark into a flame:讓星星之火燎原
spell out my name:拼出我的名字
we are meant to be:我們命中注定要在一起/一見如故
shit on:輕蔑、不尊重的對待
spending spree:揮霍
Enter me, he says in parentheses:劇本裡會在括號內標註人物出場
"It’s a play on a stage direction. A script will say enter: character and stage directions (non-dialogue) are written in parentheses."--from here
lay down my life:為....貢獻生命
ascendancy:權勢顯赫
take a shot:嘗試、乾杯(此處雙關?)
monarchy:君主制
anarchy:無政府狀態
panicky:驚慌的
tailor:裁縫
y'all: equal to "you all"
knucklehead:笨蛋、榆木腦袋、楞頭青
in loco parentis:(對別人的小孩)代盡父母的責任
in bondage:受奴役
do or die:孤注一擲
sally:突圍
stallion:種馬
battalion:(軍隊的)營
keep out of:別捲入
fraught:令人焦慮不安的
lancelot:一名圓桌武士的名字
hatch a plan:策劃一個決策
the pot calling the kettle black:五十步笑百步 ->歌詞是"blacker than the kettle callin' the pot"
pop a squat:坐下(?)
conventional wisdom:a generally accepted theory or belief.
manumission:奴隸解放
abolitionists:廢奴主義者
ammunition:彈藥
shoot off at the mouth:話講太多、不加思索
rooftop:屋頂
rise up:改變、反抗政府
living on your knees:屈膝苟活
flask:酒瓶
plenty:足夠
Scratch that:收回、忽略之前說的
take a stand:採取堅定立場
vengeance:復仇
defendant:被告人
in the face of:面對
casualties:傷亡

f147: 1. 點餐系統 (Ordering System)

程式碼:
#include <string>
#include <iostream>
using namespace std;

int main()
{
    int command,sum=0,order;
    string menu1[5]={"Medium Wac","WChicken Nugget","Geez Burger","ButtMilk Crispy Chicken","Plastic Toy"};
    string menu2[4]={"German Fries","Durian Slices","WcFurry","Chocolate Sunday"};
    int price1[5]={4,8,7,6,3},price2[4]={2,3,5,7};
    while(cin>>command)
    {
        if(command==0) break;
        else if(command==1)
        {
            cin>>order;
            cout<<menu1[order-1]<<" "<<price1[order-1]<<endl;
            sum+=price1[order-1];
        }
        else if(command==2)
        {
            cin>>order;
            cout<<menu2[order-1]<<" "<<price2[order-1]<<endl;
            sum+=price2[order-1];
        }
    }
    cout<<"Total: "<<sum<<endl;
    return 0;
}

a741: 10101 - Bangla Numbers

程式碼:
#include <iostream>
#include <iomanip>
using namespace std;
void bangla(unsigned long long int n)
{
    if(n==0) return;
    
    if(n/10000000)
    {
        bangla(n/10000000);
        cout<<" kuti";
        n%=10000000;
    }
    if(n/100000)
    {
        bangla(n/100000);
        cout<<" lakh";
        n%=100000;
    }
    if(n/1000)
    {
        bangla(n/1000);
        cout<<" hajar";
        n%=1000;
    }
    if(n/100)
    {
        bangla(n/100);
        cout<<" shata";
        n%=100;
    }
    if(n) cout<<" "<<n;
}
int main()
{
    int times=1;
    unsigned long long int n;
    
    while(cin>>n)
    {
        cout<<setw(4)<<times++<<".";
        if(n==0) cout<<" 0";
        bangla(n);
        cout<<endl;
    }
    return 0;
}

2020年7月20日 星期一

10019 - Funny Encryption Method

解題思路:
二進位出現的1與0就是十進位轉二進位的過程中,每次除以二的mod值,所以可以不用紀錄二進位轉換結果,一直除然後把有1的情況加起來就好了。

十六進位轉二進位也是一樣,先把十六進位換成十進位,然後一直除2。

程式碼:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int t,n;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int b1=0,copy,b2=0;
        cin>>n;
        copy=n;
        while(copy>0)
        {
            b1+=copy%2;
            copy/=2;
        }
        
        copy=n;
        int times=0,x2=0;
        while(copy>0)
        {
            x2+=pow(16,times)*(copy%10);
            times++;
            copy/=10;
        }
        copy=x2;
        while(copy>0)
        {
            b2+=copy%2;
            copy/=2;
        }
        cout<<b1<<" "<<b2<<endl;
    }
    return 0;
}

11150 - Cola

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        cout<<(int)(n*1.5)<<endl;
    }
    return 0;
}

2020年7月19日 星期日

10415 - Eb Alto Saxophone Player

解題思路:
這題我的寫法很不漂亮XD
每一個按法的對十個指頭來說,只有按與不按的差別,就直接把每個音調的按法記錄起來。
核心思想就是比較這次跟下次的按法,如果要「按」,就把次數加1。最後在把當前按法換成下次換法。

感覺可以包裝成函式?

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int t;
    cin>>t;
    getchar();
    string c="0111001111",d="0111001110",e="0111001100",f="0111001000",g="0111000000",
    a="0110000000",b="0100000000",C="0010000000",D="1111001110",E="1111001100",F="1111001000",
    G="1111000000",A="1110000000",B="1100000000";
    for(int i=0;i<t;i++)
    {
        int count[10]={0};
        string s,finger="0000000000";
        getline(cin,s);
        if(s[0]=='c') finger=c;
        else if(s[0]=='d') finger=d;
        else if(s[0]=='e') finger=e;
        else if(s[0]=='f') finger=f;
        else if(s[0]=='g') finger=g;
        else if(s[0]=='a') finger=a;
        else if(s[0]=='b') finger=b;
        else if(s[0]=='C') finger=C;
        else if(s[0]=='D') finger=D;
        else if(s[0]=='E') finger=E;
        else if(s[0]=='F') finger=F;
        else if(s[0]=='G') finger=G;
        else if(s[0]=='A') finger=A;
        else if(s[0]=='B') finger=B;
        for(int j=0;j<10;j++)
        {
            if(finger[j]=='1') count[j]++;
        }
        for(int j=1;j<s.size();j++)
        {
            string next;
            if(s[j]=='c') next=c;
            else if(s[j]=='d') next=d;
            else if(s[j]=='e') next=e;
            else if(s[j]=='f') next=f;
            else if(s[j]=='g') next=g;
            else if(s[j]=='a') next=a;
            else if(s[j]=='b') next=b;
            else if(s[j]=='C') next=C;
            else if(s[j]=='D') next=D;
            else if(s[j]=='E') next=E;
            else if(s[j]=='F') next=F;
            else if(s[j]=='G') next=G;
            else if(s[j]=='A') next=A;
            else if(s[j]=='B') next=B;
            for(int k=0;k<10;k++)
            {
                if(finger[k]=='0'&&next[k]=='1') count[k]++;
            }
            finger=next;
        }
        for(int i=0;i<10;i++)
        {
            if(i!=9)
                cout<<count[i]<<" ";       
            else
                cout<<count[i]<<endl;
        }
    }
    return 0;
}

10050 - Hartals

程式碼:
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n,p,h,ans=0;
        cin>>n>>p;
        vector<int> party;
        for(int j=0;j<p;j++)
        {
            cin>>h;
            party.push_back(h);
        }
        for(int j=1;j<=n;j++)
        {
            for(int k=0;k<party.size();k++)
            {
                if(j%party[k]==0&&j%7!=6&&j%7!=0)
                {
                    ans++;
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

2020年7月18日 星期六

11417 - GCD

程式碼:
#include <iostream>

using namespace std;

int GCD(int a,int b)
{
    while((a%=b)!=0&&(b%=a)!=0);
    return a+b;
}
int main()
{
    int N;
    while(cin>>N&&N!=0)
    {
        int G=0;
        for(int i=1;i<N;i++)
        {
            for(int j=i+1;j<=N;j++)
            {
                G+=GCD(i,j);
            }            
        }
        cout<<G<<endl;
    }
    
    return 0;
}

10642 Can You Solve It?

解題思路:
線與線的間隔為等差級數。
注意「1ll」這種寫法。
程式碼:
#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            int x1,y1,x2,y2;
            cin>>x1>>y1>>x2>>y2;
            long long int step1=(x1+y1)*(x1+y1+1ll)/2+x1;
            long int step2=(x2+y2)*(x2+y2+1ll)/2+x2;
            cout<<"Case "<<i<<": "<<abs(step1-step2)<<endl;            
        }
    }
    return 0;
}

2020年7月17日 星期五

10222 - Decode the Mad man

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    while(getline(cin,s))
    {
        string index="`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
        for(int i=0;i<s.size();i++)
        {
            if(s[i]==' '||s[i]=='\n') cout<<s[i];
            else
            {
                for(int j=0;j<index.size();j++)
                {
                    if(s[i]==index[j])
                        cout<<index[j-2];
                }
            }
        }
        cout<<endl;
    }
    return 0;
}

d681: BinaryCount

程式碼:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string s;
    
    while(getline(cin,s))
    {
        stringstream ss;
        string one,two,operation;
        ss<<s;
        ss>>one;
        cout<<one;
        while(ss>>operation>>two)
        {
            if(operation=="or") cout<<"||";
            else cout<<"&&";
            cout<<two;
            
            if(operation=="or")
            {
                for(int i=0;i<5;i++)
                {
                    if(one[i]=='1'||two[i]=='1') one[i]='1';
                    else one[i]='0';
                }
            }
            if(operation=="and")
            {
                for(int i=0;i<5;i++)
                {
                    if(one[i]=='1'&&two[i]=='1') one[i]='1';
                    else one[i]='0';
                }
            }
        }
        cout<<" = "<<one<<endl;
    }
    return 0;
}

12250 - Language Detection

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int cases=1;
    string s;
    while(cin>>s)
    {
        if(s=="#") break;
        cout<<"Case "<<cases++<<": ";
        if(s=="HELLO") cout<<"ENGLISH"<<endl;
        else if(s=="HOLA") cout<<"SPANISH"<<endl;
        else if(s=="HALLO") cout<<"GERMAN"<<endl;
        else if(s=="BONJOUR") cout<<"FRENCH"<<endl;
        else if(s=="CIAO") cout<<"ITALIAN"<<endl;
        else if(s=="ZDRAVSTVUJTE") cout<<"RUSSIAN"<<endl;
        else cout<<"UNKNOWN"<<endl;
    }
    return 0;
}

10929 - You can say 11

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string n;
    while(cin>>n)
    {
        if(n=="0") break;
        int even=0,odd=0;
        for(int i=0;i<n.size();i++)
        {
            if(i%2) odd+=n[i]-'0';
            else even+=n[i]-'0';
        }
        if(abs(even-odd)%11==0) cout<<n<<" is a multiple of 11."<<endl;
        else cout<<n<<" is not a multiple of 11."<<endl;
    }
    return 0;
}

11437 - Triangle Fun

解題思路:
所求小三角形面積為大三角形的1/7,推導可以看其他人寫的。
注意面積要為正。
注意四捨五入。

程式碼:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            double ax,ay,bx,by,cx,cy;
            cin>>ax>>ay>>bx>>by>>cx>>cy;
            double area=fabs(ax*by-ay*bx+bx*cy-by*cx+cx*ay-cy*ax)/14;
            area>=0?area:-1*area;
            cout<<(int)(area+0.5)<<endl;
        }
    }
    return 0;
}

2020年7月16日 星期四

e997: 升旗典禮抽背課文

程式碼:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
    int n;
    string name,s;
    stringstream ss;
    vector<string> l;
    getline(cin,s);
    cin>>n;
    ss<<s;
    while(ss>>name)
        l.push_back(name);
    cout<<l[l.size()-n]<<endl;
    return 0;
}

d471-0與1的遊戲

解題思路:
模擬二進位制。
n bits 會有 2^n種可能。
每次都加1,然後從尾到頭檢查是否要進位。

程式碼:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        string s;
        for(int i=0;i<n;i++)
            s+="0";
        for(int i=0;i<pow(2,n);i++)
        {
            cout<<s<<endl;
            s[s.size()-1]++;
            for(int j=s.size()-1;j>=0;j--)
            {
                if(s[j]>'1')
                {
                    s[j-1]++;
                    s[j]='0';
                }
            }
        }
    }
    return 0;
}

10783 - Odd Sum

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int t;
    while(cin>>t)
    {
        for(int i=1;i<=t;i++)
        {
            int a,b,sum=0;
            cin>>a>>b;
            for(int j=a;j<=b;j++)
            {
                if(j%2) sum+=j;
            }
            cout<<"Case "<<i<<": "<<sum<<endl;
        }
    }
    return 0;
}

10812 - Beat the Spread!

解題思路:
照著題目寫的去解就好了。
不可能的情況有:任一隊伍分數小於零、分數不為整數
程式碼:
#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            int s,d,team1,team2;
            cin>>s>>d;
            team1=(s+d)/2;
            team2=(s-d)/2;
            if(team1<0||team2<0||(s+d)%2||abs((s-d)%2))
                cout<<"impossible"<<endl;
            else
            {
                if(team1<team2) swap(team1,team2);
                cout<<team1<<" "<<team2<<endl;
            }
        }
    }
    return 0;
}

10420 - List of Conquests

解題思路:
輸入只要能讀到第一個單字,後面用getline讀進來就可以忽略了。
然後就看現在讀到的國家有沒有記錄過,有就把次數加1,沒有就加進去然後次數為1。
輸出照字典序。

程式碼:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n;
    string s,ignore;
    vector<string> country;
    vector<int> times;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>s;
            cin.ignore();
            getline(cin,ignore);
            if(find(country.begin(),country.end(),s)==country.end()) 
                country.push_back(s),times.push_back(1);
            else
            {
                int l=0;
                for(auto it:country)
                {
                    if(it==s)
                        break;
                    l++;
                }
                times[l]++;
            }
        }
        for(int i=0;i<country.size();i++)
        {
            for(int j=0;j<country.size();j++)
            {
                if(country[i]<country[j])
                {
                    swap(country[i],country[j]);
                    swap(times[i],times[j]);
                }
            }
        }
        for(int i=0;i<country.size();i++)
            cout<<country[i]<<" "<<times[i]<<endl;
    }
    return 0;
}

2020年7月15日 星期三

【Hamilton】Satisfied 歌詞筆記

that's what I'm talking about:幹的好!
give it up for sb:掌聲歡迎
the maid of honor:伴娘
rewind:倒帶
tripping over themselves:大獻殷勤
dreamlike:如夢一般的
pang:痛苦
dang:該死的
aflame:燃燒著的、激動的
strike....as....:讓人覺得
forget oneself:失態
catch:玄機(不確定)
flirt:輕佻
fidget:坐立不安
look askance:不滿地看
fly by the seat of your pants:未經縝密的思考和計畫即採取行動
social climb:提升社會階級
insidious:陰險的
be after:追求
nice going:(嘲諷)幹的好!
size sth/sb up:下判斷

同場加贈:https://www.bilibili.com/video/BV1kx411W7CQ?p=2
(聽了會很helpless)

10071 - Back to High School Physics

解題思路:
先回想國高中物理:

v-t圖直線下所圍面積,即為位移,而題目所求位移為著色區域。
梯形面積公式: (上底+下底)*高/2
在這裡上底可視為2t對應到的速度,下底可視為初速度,高為2t。
又因為為等加速度,且時間間隔固定(0~t, t~2t 都間隔t),根據中點公式:(上底+下底)/2=(初速度+2t對應的速度)/2=v。
所以本題可直接帶入公式:v*高,也就是v*2t

程式碼:
#include <iostream>
using namespace std;

int main()
{
    int v,t;
    while(cin>>v>>t)
    {
        cout<<2*v*t<<endl;
    }
    return 0;
}

10035 - Primary Arithmetic

解題思路:
用string解這題。
用較小數+較大數,先把較小數直接加上較大數,計算出進位次數。
然後再把該數字從頭到尾再檢查過一次要不要進位。
最後就是判斷輸出格式。

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a,b;
    while(cin>>a>>b)
    {
        if(a=="0"&&b=="0") break;
        int operation = 0;
        if(a.size()>b.size()) swap(a,b);
        for(int i=0;i<a.size();i++)
        {
            b[b.size()-i-1]=b[b.size()-i-1]+a[a.size()-i-1]-'0';
            if(b[b.size()-i-1]>'9')
            {
                b[b.size()-i-1]=(b[b.size()-i-1]-'0')%10+'0';
                b[b.size()-i-2]++;
                operation++;
            }
        }
        for(int i=b.size()-1;i>=0;i--)
        {
            if(b[i]>'9')
            {
                b[i]=(b[i]-'0')%10+'0';
                b[i-1]++;
                operation++;
            }
        }
        
        if(operation==0) cout<<"No carry operation."<<endl;
        else if(operation==1) cout<<"1 carry operation."<<endl;
        else cout<<operation<<" carry operations."<<endl;
    }
    return 0;
}

10055 - Hashmat the Brave Warrior

程式碼:
#include <iostream>
using namespace std;

int main()
{
    long long int a,b;
    while(cin>>a>>b)
    {
        cout<<abs(a-b)<<endl;
    }
    return 0;
}

2020年7月14日 星期二

10062 - Tell me the frequencies!

解題思路:
ascii code總共有128個,對應值為0~127,所以直接用長度為128的一維陣列紀錄出現次數即可。
再來是如何依照要求輸出。
因為每列最大長度為1000,所以單一值最大可能出現次數也是1000,那就從出現1次~1000次慢慢找,並且每次都從ascii最後面找(127~0)。
這樣做比較方便,不用煩惱怎麼排序,反正就次數由少到多、ascii值由大到小輸出。

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    while(getline(cin,s))
    {
        int count[128]={0};
        for(int i=0;i<s.size();i++)
            count[s[i]]++;
        for(int i=1;i<=1000;i++)
        {
            for(int j=127;j>=0;j--)
            {
                if(count[j]==i)
                    cout<<j<<" "<<i<<endl;
            }
        }
        cout<<endl;
    }
    return 0;
}

2020年7月13日 星期一

00100 - The 3n + 1 problem

#include <iostream>
using namespace std;

int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        int max_times=1;
        cout<<a<<" "<<b<<" ";
        if(a>b) swap(a,b);
        for(int i=a;i<=b;i++)
        {
            int times=1,n=i;
            while(n!=1)
            {
                times++;
                if(n%2) n=3*n+1;
                else n=n/2;
            }
            if(times>max_times) max_times=times;
        }
        cout<<max_times<<endl;
    }
    return 0;
}

2020年7月12日 星期日

【Hamilton】Non-Stop 歌詞筆記

jury:陪審團
bear with me:忍耐我一下、等一等
trial:訴訟
liberty:民主
Deliberation:審議
beyond a shadow of a doubt:不容置疑
counsel:法律顧問
sing along:一起唱
stalling:耽誤
public service:公職
mediocrities:平庸之才
Convention:會議
indelicate:無禮的
listless:無精打采的
proclamation:宣告
ammunition:彈藥
like it’s going out of style:很有熱情地很快的做
confer:談談
abrasive:粗暴、不友好
succinct:簡潔的
Hear me out:(激烈的辯論時)拜託聽我說!
back the wrong horse:下錯賭注;作錯決策
take a stand:(表達立場)站出來
keep close to my chest:守口如瓶
way/direction the wind blows:看看風向
sailing off:航行
turn of phrase:口才
join forces:聯合起來以達到共同目的
department of state:外交部

10008 - What's Cryptanalysis

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        cin.ignore();
        string text;
        char alphabet[26];
        int count[26]={0};
        for(int j=0;j<26;j++)
            alphabet[j]=(char)('A'+j);
        for(int i=0;i<n;i++)
        {
            getline(cin,text);
            for(int j=0;j<text.size();j++)
            {
                if(isalpha(text[j])&&islower(text[j]))
                    count[text[j]-'a']++;
                else if(isalpha(text[j])&&isupper(text[j]))
                    count[text[j]-'A']++;
            }
        }
        for(int i=0;i<26;i++)
        {
            for(int j=0;j<26;j++)
            {
                if(count[i]>count[j])
                {
                    swap(count[i],count[j]);
                    swap(alphabet[i],alphabet[j]);
                }
                else if(count[i]==count[j]&&alphabet[i]<alphabet[j])
                {
                    swap(count[i],count[j]);
                    swap(alphabet[i],alphabet[j]);
                }
            }
        }
        for(int i=0;i<26;i++)
        {
            if(count[i]>0)
                cout<<alphabet[i]<<" "<<count[i]<<endl;
        }
    }
    return 0;
}

2020年7月9日 星期四

2020年7月8日 星期三

【Hamilton】Alexander Hamilton 歌詞筆記

bastard:私生子
whore:婊子
Scotsman:蘇格蘭人
providence:天意
Impoverished:貧困的
squalor:骯髒的
founding father:開國元勳
self-starter:做事主動的人
charter:契約
kept his guard up:保持警惕
barter:以物易物
reigned:支配
dripping down the drain:付諸東流
temple:太陽穴
refrain:詞曲
testament:驗證
word gets around:消息迅速傳開
Took up a collection:募款
whence:何處
debt-ridden:債台高築
bed-ridden:臥床不起
fend for yourself:自謀生路
retreating:神隱
treatise:論文
astute:精明
destitute:貧困
restitution:補償
clerk:擔任店員
late mother:先母
sugar cane:甘蔗
rum:萊姆酒
bow:船首
wait in the wings:準備就緒
back down:認輸、退後
rep:名譽(reputation)

2020年7月7日 星期二

install ngspice


mkdir release
cd release
../configure --with-x --enable-xspice --disable-debug --enable-cider --with-readline=yes --enable-openmp
make
sudo make install

安裝過程中可能遇到錯誤:
1.error: no acceptable C compiler found in $PATH
yum install Gcc
使用./configure 錯誤configure: error: no acceptable C compiler found in $PATH
2.couldn't find gnu readline headers
yum install readline-devel

繪圖方面問題可能缺少的套件:
readline-devel
libXaw-devel
libX11-devel

2020年4月29日 星期三

0102-ComputeHMS

#include <iostream>
using namespace std;

int main()
{
    long long int A,hour,mintue,second;
    while(cin>>A)
    {
        second=A%60;
        A/=60;
        mintue=A%60;
        A/=60;
        hour=A;
        cout << hour << " hours " << mintue << " mintues and " << second << " seconds" << endl;
    }
    return 0;
}

0101-ComputSQRT

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double n,r,guess,pre_guess;
    while(cin>>n)
    {
        guess=n/2;
        do{
            r=n/guess;
            pre_guess=guess;
            guess=(guess+r)/2; 
        }while(pre_guess-guess>=0.01);
        cout << fixed << setprecision(2) << guess << endl;
    }
    return 0;
}

2020年4月6日 星期一

4.3-Greatest_Common_Divisor_Lab

程式碼:
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
 while ((a %= b) != 0 && (b %= a) != 0);
 return a + b;
}
int main() {
 int a, b;
 while (cin >> a >> b)
 {
  cout << gcd(a, b) << endl;
 }
    return 0;
}

4.2-BankAccount-Lab

程式碼:
main.cpp
#include <iostream>
#include "BankAccount.h"

using namespace std;

int main() {
 BankAccount bankAccount1(200), bankAccount2, bankAccount3(-100);
 cout << BankAccount::getAllMoneyInBank() << endl;
 bankAccount1.withdraw(100);
 cout << bankAccount1.getBalance() << endl;
 cout << BankAccount::getAllMoneyInBank() << endl;
 bankAccount2.save(50);
 cout << bankAccount2.getBalance() << endl;
 cout << BankAccount::getAllMoneyInBank() << endl;
 return 0;
}


BankAccount.h
#pragma once
class BankAccount {
private:
 int balance;
 static int total;
public:
 BankAccount()
 {
  balance = 0;
 }
 BankAccount(int input)
 {
  balance = input;
  total += input;
 }
 void withdraw(int output);
 void save(int input);
 int getBalance() { return balance; }
 static int getAllMoneyInBank() { return total; }
};

BankAccount.cpp
#include"BankAccount.h"

int BankAccount::total = 0;

void BankAccount::withdraw(int output)
{
 balance -= output;
 total -= output;
}
void BankAccount::save(int input)
{
 balance += input;
 total += input;
}

4.1-Collatz_Conjecture-Lab

解題心得:
典型的3N+1問題。

程式碼:
#include <iostream>
using namespace std;

int main() {
 int a, b;
 while (cin >> a >> b)
 {
  int maxCycleLength = 0, cycleLength = 0, n;
  cout << a << " " << b << " ";
  if (a > b)
   swap(a, b);
  for (int i = a; i <= b; i++)
  {
   n = i;
   cycleLength = 1;
   while (n != 1)
   {
    if (n % 2) // even
     n = 3 * n + 1;
    else //odd
     n = n / 2;
    cycleLength++;
   }
   maxCycleLength = cycleLength > maxCycleLength ? cycleLength : maxCycleLength;
  }
  cout << maxCycleLength << endl;
 }
    return 0;
}

4.2-Fraction

程式碼:
main.cpp
#include <iostream>
#include "Fraction.h"
using namespace std;

int main()
{
 Fraction f1, f2;
 f1.setNumerator(4);
 f1.setDenominator(2);
 f1.getDouble();
 f1.outputReducedFraction();

 f2.setNumerator(20);
 f2.setDenominator(60);
 f2.getDouble();
 f2.outputReducedFraction();
 system("pause");
 return 0;
}

Fraction.h
#ifndef Fraction_H
#define Fraction_H
#include <iostream>
#include <iomanip>
using namespace std;

class Fraction
{
private:
 int numerator;
 int denominator;
public:
 void setNumerator(int nu);
 void setDenominator(int de);
 void getDouble();
 void outputReducedFraction();

};

#endif //Fraction_H

Fraction.h
#include "Fraction.h"

int gcd(int a, int b)
{
 while ((a %= b) != 0 && (b %= a) != 0);
 return a + b;
}
void Fraction::setNumerator(int nu)
{
 numerator = nu;
}

void Fraction::setDenominator(int de)
{
 denominator = de;
}

void Fraction::getDouble()
{
 if (!(numerator % denominator)) // integer
  cout << numerator / denominator << endl;
 else
  cout << fixed << setprecision(6) << (double)numerator / denominator << endl;
}

void Fraction::outputReducedFraction()
{
 int greattestCommonDivisor = gcd(numerator, denominator);
 if (!(numerator % denominator)) // integer
  cout << numerator / denominator << endl;
 else
  cout << numerator / greattestCommonDivisor << "/" << denominator / greattestCommonDivisor << endl;
}

4.1-Class Point in plane

程式碼:
main.cpp
#include <iostream>
#include "Point.h"
using namespace std;

int main(void) {
 Point point;

 point.Set(0, 5);
 cout << point.RetrieveVertical() << " " << point.RetrieveHorizontal() << endl;

 point.Move(1, 2);
 cout << point.RetrieveVertical() << " " << point.RetrieveHorizontal() << endl;
 for (int i = 0; i < 4; i++) {
  point.Rotate();
  cout << point.RetrieveVertical() << " " << point.RetrieveHorizontal() << endl;
 }

 return 0;
}

Point.cpp
#include "Point.h"

void Point::Set(int x, int y)
{
 vertical = x;
 horizontal = y;
}

void Point::Move(int x, int y)
{
 vertical += x;
 horizontal += y;
}
void Point::Rotate()
{
 int temp = vertical;
 vertical = horizontal;
 horizontal = temp * (-1);
}


Point.h
#ifndef Point_H
#define Point_H
#include <iostream>
using namespace std;

class Point
{
private:
 int vertical;
 int horizontal;

public:
 Point()
 {
  vertical = 0;
  horizontal = 0;
 }

 void Set(int vertical, int horizontal);
 void Move(int x, int y);
 void Rotate();
 int RetrieveVertical() const { return vertical; }
 int RetrieveHorizontal() const { return horizontal; }
};

#endif //Point_H

2020年4月3日 星期五

operator overloading

三種方法:
Overloading unary operator.
Overloading binary operator.
Overloading binary operator using a friend function.
(Types of Operator Overloading in C++)

以複數運算為例:
(原創) 如何使用Operator Overloading? (C/C++)
文章中的複數加法用的是第二種方法,即用member function;複數乘法則是第三種方法,即用 friend function.

complex1 + complex2 比較像是 complex1.+(complex2) ,也就是說complex1 呼叫了加法的函式,把complex2當成參數傳進去。

complex1 * complex2 的概念比較像一般的function ,把兩個複數當成參數傳進去。

其他參考文章:
https://stackoverflow.com/questions/13544364/over-loading-operator-must-take-either-zero-or-one-arguments

練習的code:
https://onlinegdb.com/SJSR5TEvI

2020年3月29日 星期日

3.1-CaesarCipher-Lab

解題心得:
為了修正輸入k值很小很小的問題我想了好久,之後直接用計算機找出26的n次方加上去 >__<
但更好的方法是甚麼呢?

程式碼:
#include <iostream>
#include <string>
using namespace std;

int main() {
 long long int k;
 string plaintext;
 while (getline(cin, plaintext))
 {
  cin >> k;
  cin.ignore();
  for (int i = 0; i < plaintext.size(); i++)
  {
   if ('a' <= plaintext[i] && plaintext[i] <= 'z')
   {
    plaintext[i] = (plaintext[i] - 'a' + k + 208827064576) % 26 + 'a';

   }
   else if (plaintext[i] != ' ')
    plaintext[i] += 'a';
  }
  cout << plaintext << endl;
 }
 
 

 
    return 0;
}

3.2-RowTranspositionCipher-Lab

解題心得:
記得stringstream使用要初始化.....抓好久這個bug.......

程式碼:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main() {
 string plaintext;
 stringstream ss;
 while (getline(cin, plaintext))
 {
  int temp, row = 0;
  vector<int> key;
  string s;
  ss.clear();
  ss.str("");
  getline(cin, s);
  ss << s;
  while (ss >> temp) // get each key
  {
   key.push_back(temp);
   row++;
  }
  char array[10000][10000] = { '\0' };
  for (int i = 0; i < plaintext.size(); i++) // replace the data
  {
   if (plaintext[i] == ' ')
    array[i % row][i / row] = '_';
   else
    array[i % row][i / row] = plaintext[i];
  }
  for (int i = 0; i < row; i++)
  {
   for (int j = 0;; j++)
   {
    if (array[key[i]][j] != '\0')
     cout << array[key[i]][j];
    else
     break;
   }
  }
  
  //for (int i = 0; i < row; i++) // for test
  //{
  // for (int j = 0; j < row; j++)
  //  cout << array[i][j];
  // cout << endl;
  //}
  cout << endl;
 }
 return 0;
}

3.3-PlayfairCipher-Lab

解題思路:
其實照著題目步驟一步步寫就好了,只要不要會錯意應該就沒有大問題。

程式碼:
#include <iostream>
#include <string>
using namespace std;
void findPosition(char c,char table[5][5],int &x,int &y)
{
 for (int i = 0; i < 5; i++)
 {
  for (int j = 0; j < 5; j++)
  {
   if (c == table[i][j])
   {
    x = i, y = j;
    return;
   }
  }
 }
}

int main() {
 string plaintext, key;
 
 while (cin >> plaintext >> key)
 {
  char table[5][5] = {'\0'};
  int alphabetIndex[26] = { 0 }; // check if alphabet is recorded in table
  alphabetIndex['j' - 'a'] = 1; // aviod error
  int currentRow = 0, currentColumn = 0, currentPoint = 0; // current recorded position
  string answer = "";
  for (int i = 0; i < key.size(); i++)
  {
   if (alphabetIndex[key[i] - 'a'] == 0) // if new
   {
    table[currentRow][currentColumn] = key[i];
    currentColumn++;
    if (currentColumn >= 5) // next line
    {
     currentColumn %= 5;
     currentRow++;
    }
   }
   if (key[i] == 'j' && alphabetIndex['i' - 'a'] == 0) // 'j' case
   {
    table[currentRow][currentColumn] = 'i';
    currentColumn++;
    if (currentColumn >= 5) // next line
    {
     currentColumn %= 5;
     currentRow++;
    }
    alphabetIndex['i' - 'a']++;
   }
   alphabetIndex[key[i] - 'a']++; // increase index record
  }
  for (int i = 0; i < 26; i++) //fill the table
  {
   if (alphabetIndex[i] == 0)
   {
    table[currentRow][currentColumn] = (i + 'a');
    currentColumn++;
    if (currentColumn >= 5) // next line
    {
     currentColumn %= 5;
     currentRow++;
    }
   }
  }
  while (currentPoint <= plaintext.size() - 1) // encode
  {
   int c1Row = 0, c1Column = 0, c2Row = 0, c2Column = 0;
   if (currentPoint == plaintext.size()-1)
   {
    findPosition(plaintext[currentPoint], table, c1Row, c1Column);
    findPosition('x', table, c2Row, c2Column);
    currentPoint++;
   }
   else if (plaintext[currentPoint] != plaintext[currentPoint + 1]) // they are different
   {
    findPosition(plaintext[currentPoint], table, c1Row, c1Column);
    findPosition(plaintext[currentPoint + 1], table, c2Row, c2Column);
    currentPoint += 2; // to the next point
   }
   else
   {
    findPosition(plaintext[currentPoint], table, c1Row, c1Column);
    findPosition('x', table, c2Row, c2Column);
    currentPoint += 1;
   }

   if (c1Row == c2Row)
   {
    answer += table[c1Row][(c1Column + 1) % 5];
    answer += table[c2Row][(c2Column + 1) % 5];
   }
   else if (c1Column == c2Column)
   {
    answer += table[(c1Row + 1) % 5][c1Column];
    answer += table[(c2Row + 1) % 5][c2Column];
   }
   else
   {
    answer += table[c1Row][c2Column];
    answer += table[c2Row][c1Column];
   }
  }
  cout << answer << endl;
  //for (int i = 0; i < 5; i++) // print table
  //{
  // for (int j = 0; j < 5; j++)
  //  cout << table[i][j];
  // cout << endl;
  //}
 }
    return 0;
}