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;
}