機試

//01揹包問題
#include<iostream>
using namespace std;
const int maxn = 100; //物品數最大值
const int maxv = 1000;//容量最大值
int main()
{
    int n;//n件物品
    int V;//b揹包容量
    int dp[maxn][maxv] ;//dp[i][v]表明前i件物品剛好裝入容量爲v的揹包的最大價值
    int w[maxn];//物品重量
    int c[maxn];//物品價值
    int v;
    while(cin>>n>>V)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>w[i];
        }
        for(int i=1;i<=n;i++)
        {
            cin>>c[i];
        }
        //初始化邊界
        for(v=0;v<=V;v++)//放入0件物品的價值爲0
            dp[0][v] = 0;
            
        for(int i=1;i<=n;i++)
        {
            for(v=1;v<=V;v++)
            {
                if(v<w[i])  //第i件物品放不進去
                    dp[i][v] = dp[i-1][v];
                else
                    dp[i][v] = max(dp[i-1][v],dp[i-1][v-w[i]]+c[i]);
            }
        }
        cout<<dp[n][V]<<endl;
    }
    return 0;
}
//折點計算
#include<iostream>
using namespace std;
int main()
{
    int n;
    int a[1001];
    bool flag[1001];//flag等於true表明上升,flag = false 表明降低
    int count;
    while(cin>>n)
    {
        for(int i = 0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i = 1;i<n;i++)
        {
            if(a[i]>a[i-1])
                flag[i] = true;
            else
                flag[i] = false;
        }
        count = 0;
        for(int i = 1;i<n-1;i++)
        {
            if(flag[i] != flag[i+1])
                count++;
        }
        cout<<count<<endl;
    }
    return 0;
}
//最小差值
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int a[1001];
    set<int> cha;
    int temp;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
            cin>>a[i];
        sort(a,a+n);   //排序
        for(int i=1;i<n;i++)
        {
            temp = a[i] - a[i-1];  //求差值
            cha.insert(temp);    //存到set數組中
        }
        set<int>::iterator it = cha.begin();
        cout<<*it<<endl;
    }
    return 0;
}
//相似退圈遊戲
#include<iostream>
using namespace std;
struct node{
    int number;
    struct node * next;
};
node* createLink(int n)
{
    node * L =  new node();
    L->number = 1;
    L->next = NULL;
    node * p =  new node();
    p = L;
    for(int i=2;i<=n;i++)
    {
        node * r = new node();
        r->number = i;
        r->next = NULL;
        p->next = r;
        p = r;
    }
    p ->next = L;
    return L;
}
void deleteNode(node * p)//要刪除q節點
{
    node * r =  new node();
    r = p->next;
    while(r->next != p)
    {
        r=r->next;
    }
    r->next = p->next;
    delete p;
}
int play(node * L,int n,int k)
{
    int num = n;
    int count = 1;
    node * p = new node();
    node * q = new node();
    p = L;
    while(num != 1)
    {
        if(count % k == 0 || count % 10 == k)
        {
            q = p->next;
            deleteNode(p);
            p = q;
            num--;
        }
        else
            p = p->next;
        
        count++;
    }
    return p->number;
}
int main()
{
    int n;
    int k;
    while(cin>>n>>k)
    {
        node * L =  new node();
        L = createLink(n);
        cout<<play(L,n,k)<<endl;
    }
    return  0;
}
//碰撞的小球
#include<iostream>
using namespace std;
struct Ball{
    int location; //位置
    int direction;  //運動方向  0是左,1是右
}ball[101];
int main()
{
    int n;//小球個數
    int L;//線段長度
    int t;//計算t秒以後
    int k;
    while(cin>>n>>L>>t)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>ball[i].location;
            ball[i].direction = 1;   //初始均向右運動
        }
        k = 0;//記錄時間
        while(k<t)
        {
            k++;
            for(int i=1;i<=n;i++)
            {
                if(ball[i].direction == 1)
                    ball[i].location++;
                else
                    ball[i].location--;
                if(ball[i].location == 0)   //走到最左端改變方向
                    ball[i].direction = 1;
                if(ball[i].location == L)    //走到最右端改變方向
                    ball[i].direction = 0;
                //兩球相撞,改變方向
                for(int j=1;j<i;j++)
                {
                    if(ball[i].location == ball[j].location)
                    {
                        if(ball[i].direction == 1 )
                        {
                            ball[i].direction = 0;
                            ball[j].direction = 1;
                        }
                        else
                        {
                            ball[i].direction = 1;
                            ball[j].direction = 0;
                        }
                    }
                }
            }//for
            
        }//while(k<=t)
        for(int i=1;i<=n;i++)
        {
            cout<<ball[i].location<<" ";
        }
        cout<<endl;
    }
    return 0;
}
//買菜
#include<iostream>
#include<map>
using namespace std;
int main()
{
    int n;
    map<int,int> mp;
    int a,b;
    int i,j;
    int count;
    while(cin>>n)
    {
        count = 0;
        for(i=0;i<2*n;i++)
        {
            cin>>a>>b;
            for(j = a;j<b;j++)
            {
                mp[j]++;
            }
        }
        for(map<int,int>::iterator it = mp.begin();it!=mp.end();it++)
        {
            if(it->second == 2)
                count++;
        }
        cout<<count<<endl;
    }
    return 0;
}
//小明上學
#include<iostream>
#include<map>
using namespace std;
int main()
{
    int r,y,g;
    int n;
    map<int,int> mp;
    int a,b;
    int sum ;
    while(cin>>r>>y>>g)
    {
        sum = 0;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a>>b;
            sum += b;
            if(a==2)
                sum += r;
            if(a == 3)
                sum -= b;
        }
        cout<<sum<<endl;
    }
    return 0;
}
//小明放學
#include<iostream>
using namespace std;
int main()
{
    int r,y,g;
    int n;
    long long sum;
    int a,b;
    int x;
    while(cin>>r>>y>>g)
    {
        sum = 0;
        cin>>n;
        for(int i =0;i<n;i++)
        {
            cin>>a>>b;
            x = (sum - b)%(r+y+g);
            if(a == 0)
            {
                sum += b;
            }
            else if(a == 1)//紅燈
            {
                if(x<0)        //仍是紅燈,
                    sum += b - sum;
                else if(x<=g)     //綠燈
                    sum +=0;
                else if(x>g&&x<=g+y)  //
                    sum +=(g+y) - x + r;
                else
                    sum += (r+y+g) - x;   //紅燈
            }
            else if(a == 2)//黃燈
            {
                if(x<0)            //仍是黃燈
                    sum += b - sum + r;
                else if(x<=r)     //紅燈
                    sum +=r - x;
                else if(x>r&&x<=r+g)  //綠燈
                    sum += 0;
                else
                    sum += (r+y+g) - x + r;   //黃燈
            }
            else//綠燈
            {
                if(x<0)              //仍是綠燈
                    sum += 0;
                else if(x<=y)     //
                    sum +=y - x + r;
                else if(x>y&&x<=y+r)  //紅燈
                    sum += r+y - x;
                else
                    sum += 0;   //綠燈
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}
/*給出一個01字符串(長度不超過100),求其每個子串出現的次數。*/
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
    string str;
    map<string , int> mapstr;//試用map,自己已經用字典序排序
    string substring;
    while(cin>>str)
    {
        int len = str.length();
        mapstr.clear();      //清空mapstr
        for(int i=0;i<len;i++)
        {
            substring="";
            /*單個0或1*/
            substring+=str[i];
            if(mapstr.find(substring)!=mapstr.end())
            {
                mapstr[substring]++;
            }
            else{
                mapstr[substring] = 1;
            }
            /*從當前位置日後找其子串*/
            for(int j=i+1;j<len;j++)
            {
                substring+=str[j];
                if(mapstr.find(substring)!=mapstr.end())
                {
                    mapstr[substring]++;//匹配
                }
                else{
                    mapstr[substring] = 1;
                }
            }
        }
        //輸出
        for(map<string,int>::iterator it=mapstr.begin();it!=mapstr.end();it++)
        {
            if(it->second>1)
            {
                cout<<it->first<<" "<<it->second<<endl;
            }
            
        }
    }
    return 0;
}
//質因數個數
#include<iostream>
#include<math.h>
using namespace std;
const int maxn =100000;
bool isPrime(int n)
{
    if(n==1)
        return false;
    int sqr = sqrt(1.0*n);
    for(int i=2;i<=sqr;i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int prime[maxn];//存放素數
int pnum = 0;
void findPrime()
{
    for(int i=2;i<maxn;i++)
        if(isPrime(i))
        {
            prime[pnum] = i;
            pnum++;
        }
}
//求質因數方法一:求出必定區間內全部的質數,而後判斷
/*
int countAppro(int n)
{
    int count = 0;//記錄個數
    int sqr = sqrt(1.0*n);
    for(int i=0;i<pnum && prime[i]<=sqr;i++)
    {
        if(n%prime[i]==0)
        {
            count++;
            n/=prime[i];
            while(n%prime[i]==0)
            {
                count++;
                n/=prime[i];
            }
        }
        if(n==1)
            break;
    }
    if(n!=1)
        count++;
    return count;
}*/
//直接判斷
int countAppro(int n)
{
    int count = 0;//記錄個數
    int sqr = sqrt(1.0*n);
    for(int i=2;i<=sqr;i++)
    {
        if(n%i==0&&isPrime(i))
        {
            count++;
            n/=i;
            while(n%i==0)
            {
                count++;
                n/=i;
            }
        }
        if(n==1)
            break;
    }
    if(n!=1)
        count++;
    return count;
}
int main()
{
    //findPrime();
    int n;
    while(cin>>n)
    {
        cout<<countAppro(n)<<endl;
    }
    return 0;
}
//上幾真題
2008年
A
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int a[1001] ={};
    bool flag;
    int temp = 0;
    while(cin>>n&&n!=0)
    {
        flag = true;
        for(int i = 0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        temp=a[1] - a[0];
        for(int i = 1;i<n;i++)
        {
            if(a[i] - a[i-1] != temp)
            {
                flag = false;
                break;
            }
        }
        if(flag == true)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}
B
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main()
{
    int n;
    int sum;
    int temp;
    while(cin>>n && n != 0)
    {
        sum = 0;
        temp = n;
        while(temp)
        {
            sum += pow(temp%10,3.0);
            temp /= 10;
        }
        if(sum == n)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}
C
#include<iostream>
using namespace std;
/*統計每一個點變回原樣的次數,其中最大值即爲所求*/
int main()
{
    int count,a,b,ta,tb;
    int index;//記錄矩陣每一個點變化的最大值
    int N;
    while(cin>>N)
    {
        index = 0;//初始化爲0
        
        if(N==0)
            break;
        if(N<=2||N>10)
            continue;

        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                ta = i;
                tb = j;
                count = 0 ;
                do{
                    a = (ta+tb) % N;
                    b = (ta + 2*tb) % N;//須要藉助a,b做爲中轉的緣由是,這一步須要用到原來的ta值而不是變化後的ta值
                    ta = a;
                    tb = b;
                    count++;
                    if(ta == i&&tb==j)//若座標點通過若干次轉化爲原來的
                    {
                        index = count>index?count:index;
                        break;
                    }
                }while(count<=N*N/2);
            }
        }
        cout<<index<<endl;
    }
}
/*以下方法行不通,想經過比較兩個矩陣來實現,可是矩陣會變化,*/
/*int N;
bool equal(int a[][100],int b[][100])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(a[i][j] != b[i][j])
                return false;
        }
    }
    return true;
}
int main()
{
    
    int n;
    int a[100][100] = {0};
    int b[100][100] = {0};
    int count;
    int temp;
    while(cin>>N)
    {
        n = 1;
        
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
            {
                a[i][j] = n;
                n++;
            }
        int ix = 0;
        int jx = 0;
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
            {
                temp = a[i][j];
                ix = (i+1) % N;
                jx = (i + 2*j) % N;
                b[ix][jx] = temp;
            }
        count = 1;
        while(!equal(a,b))
        {
            count++;
            for(int i=0;i<N;i++)
                for(int j=0;j<N;j++)
                {
                    temp = b[i][j];
                    ix = (i+1) % N;
                    jx = (i + 2*j) % N;
                    b[ix][jx] = temp;
                }
        }
        cout<<count<<endl;
    }
    return 0;
}*/
D
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
vector <int> yueshu;
int count = 0;
//素數判斷
bool isPrime(int n)
{
    for(int i =2;i<=sqrt((double)n);i++)
    {
        if(n%i == 0)
        {
            return false;
        }
    }
    return true;
}
void Approximates(int n)//求質因數
{
    int temp = n;
    int i = 2;
    //正整數的因數分解可將正整數表示爲一連串的質因子相乘,質因子如重複能夠用指數表示。
    while(temp!=1)
    {
        if(temp%i == 0&&isPrime(i))
        {
            yueshu.push_back(i);
            count++;
            temp /= i;//每次取得一個因數,就除以這個因數
            i--;
        }
        i++;
    }
}
//求一個整數各位數字之和
int wei_Add(int n)
{
    int temp = n;
    int sum = 0;
    while(temp)
    {
        sum += temp%10;
        temp/=10;
    }
    return sum;
}
int main()
{
    int n;
    int yueshu_sum;
    while(cin>>n&&n!=0)
    {
        //初始化工做
        yueshu_sum = 0;//約數各個位數之和
        count = 0;//質因數個數
        yueshu.clear();
        //求質因數
        Approximates( n);
        //求每一個質因數的各位數字和
        for(int i=0;i<count;i++)
        {
            yueshu_sum += wei_Add(yueshu[i]); 
        }
        //比較
        if(wei_Add(n) == yueshu_sum)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}
2009
A
#include<iostream>
#include<math.h>
using namespace std;
//採用此方法運行時間太長
/*bool isWanshu(int n)
{
    int sum = 1;
    for(int i = 2;i<=sqrt(n*1.0);i++)
    {
        if(n%i==0)
        {
            sum+=i;
            sum += n/i;
        }
    }
    if(sum == n && n!=1)
        return true;
    else
        return false;
}*/
//應先將1-100000之間的完數計算出來,存到數組中
int wanshu[10] = {0};
int index = 0;
void findWanshu()
{
    int sum;

    for(int n=2;n<100000;n++)
    {
        sum = 1;                       //1必定是因子
        for(int i = 2;i<=sqrt(n*1.0);i++)//只需計算到根號n以前
        {
            if(n%i==0)
            {
                sum+=i;
                sum += n/i;
             }
        }
        if(sum==n)             //是完數則存入數組
        {
            wanshu[index] = n;
            index++;
        }
    }
}
int main()
{
    findWanshu();
    int a = 0;
    int b = 0;
    while(cin>>a>>b)
    {
        for(int j = 0;j<index;j++)
        {
            if(wanshu[j]>=a && wanshu[j]<=b)
                cout<<wanshu[j]<<endl;
        }
    }
    return 0;
}
B
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int m;
    
    while(cin>>m)
    {
        int a[11][11] = {0};
        int b[30]= {0};
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>a[i][j];
            }
        }
        int x = 0;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(i == j)
                    b[0] += a[i][j];//主對角線
                //矩陣副對角線有i+j = m-1
                if(i+j == m-1)
                    b[2*m+1] += a[i][j];
                if(j==0)
                    x++;//每次換行就加1
                b[x] += a[i][j];//每一行的和
            }
        }
        for(int j=0;j<m;j++)
        {
            for(int i=0;i<m;i++)
            {
                if(i==0)
                    x++;//每次換列就加1
                b[x] += a[i][j];//
            }
        }
        sort(b,b+2*m+2,cmp);
        for(int i = 0;i<2*m+1;i++)
            cout<<b[i]<<" ";
        cout<<b[2*m+1]<<endl;
    }
    return 0;
}
/*
運行時間:3ms

佔用內存:368k
*/
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
//學長寫法
        /*
        //求每一行的和
        for(int i=0;i<m;i++)
        {
            sum = 0;
            for(int j=0;j<m;j++)
            {
                sum+=a[i][j];
            }
            num[n] = sum;
            n++;
        }
        //求主對角線
        for(i =0,j = 0;i<m;i++,j++)
            sum += a[i][j];
        //求副對角線
        for(i =0,j = m-1;i<m;i++,j--)
            sum += a[i][j];
        */
C
/*

對於本題而言,下邊使用了快速分解定理:
一、即對於一個合數總存在有質數因子,因此咱們在分解合數的時候不須要判斷被整除的因子是否爲質數。
二、理解是一個難點,對於一個合數,咱們將小於等於這個合數平方根的質因子整除完以後,若
餘下的書大於1,說明本合數存在而且僅存在一個大於這個合數的平方根的質因子*/
#include<iostream>
#include<vector>
#include<string>
#include<math.h>
using namespace std;
const int maxn = 100000;
int max_yinzi(int n)
{
    int max =0;
    for(int i=2;i<=sqrt(1.0*n);i++)//從2到根號n求其質因數
    {
        while(n%i==0)              //由於一個質因數可能會乘屢次,因此應該用while,而不是if
        {
            max = i;               //讓max指向當前最大值
            n /= i;                //除以質因數
        }
        if(n==1)
            break;
    }
    if(n!=1)                       //若是n不等於1,說明存在一個大於根號n的質因數,有且僅有一個,即等於當前的n
        max = n;
    return max;
}
int main()
{
    int n;
    vector<string> str;
    char ch[101];
    string temp;
    int sum;
    double count;
    while(cin>>n)
    {
        str.clear();//清空
        getchar();  //用在gets()以前
        for(int i=0;i<n;i++)
        {    
            gets(ch);   //考慮到輸入可能有空格
            str.push_back(ch);
        }
        for(int i=0;i<n;i++)//對str中每個字符串進行分析
        {
            sum = 0;
            count = 0;
            temp = str[i];
            for(int j = temp.length()-1;j>=0;j--)  //從後往前,
            {
                if(temp[j]>='0'&&temp[j]<='9')
                {
                    sum += (temp[j] -'0')*pow(10.0,count);
                    count++;
                }
            }
            cout<<max_yinzi(sum)<<endl;
        }
    }
    return 0;
}
D
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
struct node{
    char data;
    struct node *lchild,*rchild;
    node(char ch) {
        data = ch;
        lchild = rchild = NULL;
    }
}*BiTree;
node* createTree(string str_pre,string str_in)
{
    int len = str_pre.length();
    if(len<=0) return NULL;
    node* T = new node(str_pre[0]);
    int val = str_in.find(str_pre[0]);
    T->lchild = createTree(str_pre.substr(1,val),str_in.substr(0,val));
    T->rchild = createTree(str_pre.substr(val+1,len-val-1),str_in.substr(val+1,len-val-1));
    return T;
}
void postOrder(node * T)
{
    if(T!=NULL)
    {
        postOrder(T->lchild);
        postOrder(T->rchild);
        cout<<T->data;
    }
}
int main()
{
    string str_pre;//先序
    string str_in;//中序
    cin>>str_pre;
    cin>>str_in;
    node * T = createTree(str_pre,str_in);
    postOrder(T);
    return 0;
}
E
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
    //變量定義
    int n;
    string str;
    bool flag = true;//標誌位,判斷
    
    char ch;


    cin>>n;  //個數
    while(n--)
    {
        stack<char> S;//初始化一個棧,存儲符號
        cin>>str;
        //對字符串進行操做
        for(int i =0;i<str.length();i++)
        {
            //當爲左括號是就入棧
            if(str[i] == '('||str[i] == '['||str[i] == '{')
                S.push(str[i]);
            //當爲右括號是分狀況討論
            if(str[i] == ')')
            {
                if(S.empty())//若棧爲空則不匹配
                {
                    flag = false;
                    break;
                }
                ch = S.top();//在獲取top以前必須判斷棧是否爲空
                if(ch == '(')
                {
                    flag = true;
                    S.pop();//匹配則出棧
                }
                else
                {
                    flag = false;
                    break;
                }
            }
            if(str[i] == ']')
            {
                if(S.empty())
                {
                    flag = false;
                    break;
                }
                ch = S.top();
                if(ch == '[')
                {
                    flag = true;
                    S.pop();
                }
                else
                {
                    flag = false;
                    break;
                }
            }
            if(str[i] == '}')
            {
                if(S.empty())
                {
                    flag = false;
                    break;
                }
                ch = S.top();
                if(ch == '{')
                {
                    flag = true;
                    S.pop();
                }
                else
                {
                    flag = false;
                    break;
                }
            }
        }
        if(flag == true&&S.empty())//判斷棧空目的:"()("也爲不匹配
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}
2011
A
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[1001]={0};
    int b[1001]={0};
    int i=0;
    int temp;
    int sum;
    //輸入
    while(1)
    {
        cin>>a[i];
        if(a[i]==0)//輸入0結束
            break;
        i++;//計數,共i個數
    }
    //對輸入的每一個數求各個數字之和
    for(int j=0;j<i;j++)
    {
        temp = a[j];
        sum = 0;
        while(temp)
        {
            sum += temp%10;
            temp /= 10;
        }
        b[j] = sum;//結果存到b數組中
    }
    sort(b,b+i);//排序
    for(int j=0;j<i-1;j++)
        cout<<b[j]<<" ";
    cout<<b[i-1]<<endl;
    return 0;
}
B
#include<iostream>
using namespace std;
struct Ma{
    int x;
    int y;
    int num;
}an[100];
int main()
{
    int m,n;//行數、列數
    int a[101][101]={0};
    int min_hang;
    int temp;
    bool flag;
    int x;
    while(cin>>m>>n)
    {
        //初始化
        
        
        //輸入矩陣
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>a[i][j];
            }
        }
        //初始化
        min_hang = a[0][0];
        temp = 0;
        x = 0;//記錄馬鞍點個數
        
        for(int i=0;i<m;i++)
        {
            //找每一行最小值
            for(int j=0;j<n;j++)
            {
                if(min_hang>a[i][j])
                {
                    min_hang = a[i][j];
                    temp = j;//記錄所在列數
                }
            }
            
            //判斷是否是所在列的最大值,temp記錄每一行最小值的列
            for(int k=0;k<m;k++)
            {
                if(min_hang<a[k][temp])
                {
                        flag = false;
                        break;
                }
            }
            //若是是馬鞍點就把他加入到an數組中
            if(flag)
            {
                an[x].x = i;
                an[x].y = temp;
                an[x].num = min_hang;
                x++;
            }
            //恢復標誌位
            min_hang = a[i+1][0];
            flag = true;
        }
        for(int i=0;i<x;i++)
            cout<<an[i].x<<" "<<an[i].y<<" "<<an[i].num<<endl;
    }
    return 0;
}
C
//字符串壓縮
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main()
{
    string str;
    string str2;
    int sum;
    int count;
    while(cin>>str)
    {
        sum = 0;
        count = 0;
        str2 = "";
        for(int i=str.length()-1;i>=0;i--)
        {
            if(str[i]>='0'&&str[i]<='9')
            {
                sum += (str[i]-'0') * pow(10.0,count);
                count++;
            }
            else{
                if(sum==0)
                    str2+=str[i];
                else{
                    for(int j=0;j<sum;j++)
                        str2+=str[i];
                }
                sum = 0;
                count=0;
            }
        }
        for(int i=str2.length()-1;i>=0;i--)
            cout<<str2[i];
        cout<<endl;
    }
    return 0;
}
D
//求哈夫曼樹帶權路徑長度
//算法思想參考算法筆記344-345頁
#include<iostream>
#include<queue>
#include<functional>  //greater
using namespace std;
int main()
{
    int n;
    int w;
    int x,y;
    int WPL = 0;
    priority_queue<int,vector<int>,greater<int> > q;//定義一個優先級隊列,從小到大
    while(cin>>n)
    {
        //清空優先級隊列
        while(!q.empty())
            q.pop();
        while(n--)
        {
            cin>>w;
            q.push(w);
        }
        while(q.size()>1)
        {
            x = q.top();//取第一小元素
            q.pop();
            y = q.top();//取第二小元素
            q.pop();
            q.push(x+y);//二者相加後加入優先級隊列
            WPL += x + y;//爲何直接相加就能獲得最短帶權路徑長度?由於每一次相加後又壓入了隊列中,因此每次相加把以前的權值也加上了
        }
        cout<<WPL<<endl;
    }
    return 0;
}
2013
A
#include<iostream>
using namespace std;
int F(int n)
{
    if(n==0)
        return 7;
    else if(n==1)
        return 11;
    else
        return F(n-1)+F(n-2);
}
int main()
{
    int N;
    int n;
    cin>>N;
    while(N--)
    {
        cin>>n;
        if(F(n)%3==0)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;

    }
    return 0;
}
B
#include<iostream>
#include<map>
using namespace std;
map<int,int> number;
int main()
{
    int index;
    for(int i=0;i<20;i++)
    {
        cin>>index;
        number[index]++;
    }
    for(map<int,int>::iterator it=number.begin();it!=number.end();it++)
    {
        cout<<it->first<<":"<<it->second<<endl;
    }
    return 0;
}
C
D
#include<iostream>
#include<math.h>
#include<stack>
#include<string>
using namespace std;
//二進制轉換十進制
int Binary(int n)
{
    int sum = 0;
    int count =0;
    while(n)
    {
        sum += (n %10) * pow(2.0,count);
        n /= 10;
        count++;
    }
    return sum;
}
//十進制轉二進制
string Decimal(int n)
{
    stack<int> S;
    string str = "";
    int count = 0;
    while(n)
    {
        S.push(n%2);
        n /= 2;
    }
    while(!S.empty())
    {
        str += S.top() + '0';
        S.pop();
    }
    return str;
}
int main()
{
    int n;//
    int m;//進制
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        if(m == 2)
        {
            cout<<Binary(n)<<endl;
        }
        else
        {
            cout<<Decimal(n)<<endl;
        }
    }
    return 0;
}
2015
A
//求一串數中大於1的素數
#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
using namespace std;
//判斷是不是素數
bool isPrime(int n)
{
    for(int i=2;i<=sqrt((double)n);i++)
        if(n%i==0)
            return false;
    return true;
}
int main()
{
    char ch[200];
    int num[200]={0};
    int result[200] = {0};
    string str;
    int count;
    int sum;
    int j;
    int index=0;
    while(gets(ch))
    {//輸入,接受每一行的數據
        if(ch[0]=='0')  //輸入0返回
            break;
        str = ch; 
        sum = 0;
        count = 0;
        j = 0;

        for(int i=str.length()-1;i>=0;i--) //從後往前遍歷,把每個數提取出來,存放到num數組中
        {
            if(str[i]>='0'&&str[i]<='9')   //數字
            {
                sum += (str[i] - '0')*pow(10.0,count);
                count++;
            }
            else{                         //空格
                num[j] = sum;
                j++;
                count = 0;
                sum = 0;
            }
            if(i==0)
                num[j] = sum;
        }
        sum = 0;//素數和
        for(int i=0;i<=j;i++)
        {
            if(isPrime(num[i])&&num[i]!=1)
                sum+=num[i];
        }
        result[index] = sum;//記錄每一組數的結果
        index++;         //
    }
    for(int i=0;i<index;i++)
        cout<<result[i]<<endl;
    return 0;
}
B
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    string str2 = "";
    int count;
    while(cin>>str)
    {
        count = 1;
        str2 = "";
        for(int i= 0;i<str.length();i++)
        {
            if(str[i] == str[i+1])
            {
                count++;
            }
            else{
                
                if(count==1)
                    str2 += str[i];
                else
                {
                    str2 += count +'0';
                    str2 += str[i];
                    
                }
                count = 1;
            }
        }
        cout<<str2<<endl;
    }
    return 0;
}
C
#include<iostream>
using namespace std;
int main()
{
    int row,col,start;
    char maze[11][11]={''};
    while(cin>>row>>col>>start)
    {
        if(row==0&&col==0&&start==0)
            return 0;
        for(int i=1;i<=row;i++)
            for(int j=1;i<=col;j++)
                cin>>maze[i][j];
        int i = 1;
        int j = start;
        int count = 0;
        while(i>=1&&i<=row&&j>=1&&j<=col)
        {
            if(maze[i][j]=='N')
                i = i-1;
            else if(maze[i][j]=='W')
                j -= 1;
            else if(maze[i][j]=='S')
                i -= 1;
            else
                j += 1;
            count++;
        }
        cout<<count<<endl;
    }
    return 0;
}#include<iostream>
using namespace std;
int main()
{
    int row,col,start;
    char maze[11][11]={''};
    while(cin>>row>>col>>start)
    {
        if(row==0&&col==0&&start==0)
            return 0;
        for(int i=1;i<=row;i++)
            for(int j=1;i<=col;j++)
                cin>>maze[i][j];
        int i = 1;
        int j = start;
        int count = 0;
        while(i>=1&&i<=row&&j>=1&&j<=col)
        {
            if(maze[i][j]=='N')
                i = i-1;
            else if(maze[i][j]=='W')
                j -= 1;
            else if(maze[i][j]=='S')
                i -= 1;
            else
                j += 1;
            count++;
        }
        cout<<count<<endl;
    }
    return 0;
}
D
#include<iostream>
#include<fstream>  //引用文件輸入輸出庫
#include<string>
#include<algorithm>
using namespace std;
struct Score{
    string ID;
    int question_num;
    int score;

    friend bool operator <(Score a,Score b)  //重載小於號,定義排序規則
    {
        if(a.question_num != b.question_num)
            return a.question_num>b.question_num;
        else
            return a.score<b.score;
    }
}Stu_score[100];
int main()
{
    ifstream infile;  //從文件輸入,讀取文件內容
    ofstream outfile;  //輸出到文件
    int i = 0;
    infile.open("Score.txt",ios::in);//打開文件
    outfile.open("Order.txt");
    if(!infile)                       //打開失敗
    {
        cout<<"open failed"<<endl;
        exit(1);
    }
    while(!infile.eof())
    {
        infile>>Stu_score[i].ID>>Stu_score[i].question_num>>Stu_score[i].score;//文件流輸入結構體中
        i++;
    }
    sort(Stu_score,Stu_score+i);//排序
    for(int j=0;j<i;j++)
        outfile<<j+1<<" "<<Stu_score[j].ID<<" "<<Stu_score[j].question_num<<" "<<Stu_score[j].score<<endl;
    infile.close();
    outfile.close();
    return 0;
}
2017
A
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int a[100] = {0};
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        if(n%2 == 0&&a[n/2-1] == a[n/2])
        {
            cout<<a[n/2-1]<<endl;
        }
        else if(n%2 != 0 && a[n/2-1] != a[n/2] &&a[n/2+1] != a[n/2])
        {
            cout<<a[n/2]<<endl;
        }
        else
            cout<<-1<<endl;
    }
    return 0;
}
B
#include<iostream>
#include<vector>
#include<string>
#include<math.h>
using namespace std;
vector<string> strs;
//樸素的串匹配算法
bool String_match(string c,string str,int option)//str是子串
{
    int i =0;
    int j = 0;
    while(i<c.length()&&j<str.length()) //注意循環條件
    {
        if(option == 1)               //1表示對大小寫敏感
        {
            if(c[i] == str[j])      //若是當前字符匹配,就往下走
            {
                i++;
                j++;
            }
            else                     //不然回溯
            {
                i = i-j+1;
                j = 0;
            }
        }
        else                                            //對大小寫不敏感
        {
            if(c[i]==str[j] || abs(c[i]-str[j]) == 32)      //若是當前字符匹配或者是大小寫關係,小寫字母比大寫字母要大32
            {
                i++;
                j++;
            }
            else                     //不然回溯
            {
                i = i-j+1;
                j = 0;
            }
        }
    }
    if(j>=str.length())   //子串遍歷完說明匹配成功
        return true;
    else
        return false;
}
int main()
{
    string str;
    int option;//大小寫敏感選項
    int n;//字符串個數
    string temp;
    
    
    while(cin>>str)
    {
        strs.clear();
        cin>>option;//0對大小寫不敏感,1對大小寫敏感
        cin>>n;//組數
        for(int i=0;i<n;i++)
        {
            cin>>temp;
            strs.push_back(temp);
        }
        if(option == 1)
        {
            for(int i=0;i<n;i++)
            {
                temp = strs[i];
                if(String_match(temp,str,1))
                    cout<<temp<<endl;
            }
        }
        else
        {
            for(int i=0;i<n;i++)
            {
                temp = strs[i];
                if(String_match(temp,str,0))
                    cout<<temp<<endl;
            }
        }
    }
    return 0;
}
#include<iostream>
using namespace std;

//KMP算法
void getnext(int next[],char t[])
{
    int i=0;
    int j=-1;
    next[0] = -1;
    while(t[i]!='\0')
    {
        if(j==-1||t[i] == t[j])
        {
            next[++i] = ++j;
        }
        else
            j = next[j];
    }
}
int kmp(int next[],char s[],char t[])
{
    int i=0,j = 0;
    int len1 = strlen(s);
    int len2 = strlen(t);
    while(i<len1 && j<len2)
    {
        if(j==-1||s[i] == t[j])
            ++i,++j;
        else
            j = next[j];
    }
    if(j>=len2)
        return i-len2+1;
    else
        return -1;
}
const int MAXN = 100;
int next[MAXN+1];
char s[MAXN+1],s2[MAXN+1],t[MAXN+1];
int main()
{
    int option,m;
    cin>>t>>option;
    if(option == 0)//大小寫不敏感
    {
        for(int i=0;t[i];i++)
        {
            t[i] = tolower(t[i]);
        }
    }
    getnext(next,t);
    cin>>m;
    while(m--)
    {
        cin>>s;
        strcpy(s2,s);
        if(option == 0)
        {
            for(int i=0;s2[i];i++)
                s2[i] = tolower(s2[i]);
            
        }
        if(kmp(next,s2,t)!=-1)
            cout<<s<<endl;
    }
    return 0;
}
C
//存在問題
#include<iostream>
#include<map>
#include<string>
using namespace std;
map<int,int>  m;
struct work_time{
    int order;  //記錄順序
    string name;
    int t1_hour;
    int t1_minutes;
    int t2_hour;
    int t2_minutes;
    int total_minutes;
}wt[100];

int main()
{
    int n;
    int sum_time;
    
    while(cin>>n)//數據數
    {
        m.clear();
        for(int i=0;i<n;i++)
        {
            sum_time = 0;
            wt[i].order = i+1;
            cin>>wt[i].name;
            scanf("%2d%*c%2d %2d%*c%2d",&wt[i].t1_hour,&wt[i].t1_minutes,&wt[i].t2_hour,&wt[i].t2_minutes);
            //scanf("%2d%*c%2d %2d%*c%2d",&t1_hour,&t1_minutes,&t2_hour,&t2_minutes);
            sum_time = (wt[i].t2_hour * 60 +wt[i].t2_minutes) - (wt[i].t1_hour * 60 +wt[i].t1_minutes);
            m[wt[i].order] += sum_time;
            wt[i].total_minutes = m[wt[i].order];
        }
        for(int i=0;i<n;i++ )
        {
            cout<<wt[i].name<<" "<<wt[i].total_minutes<<endl;
        }
        /*for(map<string,int>::iterator it=m.begin();it!=m.end();it++)
        {
            cout<<it->first<<" "<<it->second<<endl;
        }*/
        
    }
    return 0;
}
D
#include<iostream>
#include<map>
using namespace std;
const int row =  20;
const int col = 5;
map<int,int> mp;//第幾排還有幾個座位的映射
int main()
{
    int n;//購票指令數
    int temp;
    int start;
    int end;
    int a[100]={0};
    bool fenpei;
    while(cin>>n)
    {
        for(int i=0;i<20;i++)//初始化每一排都有五個座位
            mp[i] = col;
        for(int i=0;i<n;i++) //輸入每條指令要買的票數
        {
            cin>>a[i];
        }
        for(int i=0;i<n;i++) 
        {
            temp = a[i];
            //能夠安排在同一排編號相鄰的位置
            for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
            {
                if(it->second>=temp)//找到一排能夠放下temp我的的排
                {
                    start = it->first * col + (col - it->second)+1;//計算其開始位置和結束位置
                    end = start + temp -1;
                    for(int j=start;j<end;j++)   //輸出
                        cout<<j<<" ";
                    cout<<end<<endl;
                    //若是該排已經分配完,就刪除,不然座位數減去分配掉的座位數
                    if (it->second == temp)
                        mp.erase(it);
                    else
                        it->second = it->second - temp;
                    fenpei = true;    //分配成功
                    break;    //退出
                }
            }
            if(fenpei == false)   //沒有相鄰的座位能夠安排
            {
                while(temp>0)   // 每次判斷是否安排完畢 //
                {
                    for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
                    {
                        if(it->second >= temp)   //當本排座位數大於當前須要的座位數
                        {
                            start = it->first * col + (col - it->second)+1;
                            end = start + temp -1;
                            for(int j=start;j<=end;j++)
                                cout<<j<<" ";
                            cout<<endl;
                            temp = 0;
                            if (it->second == temp)
                                mp.erase(it);
                            else
                                it->second -= temp;
                            break;
                        }
                        else
                        {
                            start = it->first * col + 1 + (col - it->second);
                            end = start + it->second - 1;
                            for (int j = start; j <= end; j++) {
                                cout << j<<" ";
                                //nofirstflag = true;
                            }
                            temp -= it->second;             //減去分配的座位數
                            // 對於票數已經分配完畢的排,刪除
                            mp.erase(it);
                        }//else
                    }//for
                }
            }//if(fenpei == false)
        }
    }
    return 0;
}
2018
A
#include<iostream>
using namespace std;
int main()
{
    int n;
    int sum = 0;//總分
    int count = 0;//跳躍到中心的次數
    while(cin>>n)
    {
        if(n==0)
            break;
        if(n==1)
        {
            sum += 1;
            count = 0;
        }
        else if(n == 2)
        {
            count++;
            sum += count * 2;
        }
    }
    cout<<sum<<endl;
    return 0;
}
B
#include<iostream>
#include<string>
#include<vector>
#include<string.h>
#include<math.h>
using namespace std;
bool fun(string str,string S)
{
    int i = 0;
    int j=0;
    while(i<str.length()&&j<S.length())
    {
        if(str[i] == S[j])
        {
            ++i;
            ++j;
        }
        else
        {
            i = i - j +1;
            j = 0;
        }
        
    }
    if(j>=S.length())
        return true;
    else
        return false;
}
bool fun2(string str,string S)
{
    int i = 0;
    int j=0;
    while(i<str.length()&&j<S.length())
    {
        if(str[i] == S[j] || abs(str[i] - S[j]) == 32)
        {
            ++i;
            ++j;
        }
        else
        {
            i = i - j +1;
            j = 0;
        }
        
    }
    if(j>=S.length())
        return true;
    else
        return false;
}
int main()
{
    char S[20];
    int option = 0;
    int n;
    vector<string> str;
    string a;
    string temp;
    while(cin>>S>>option>>n)
    {
        for(int i = 0;i<n;i++)
        {
            cin>>a;
            str.push_back(a);
        }
        if(option == 1)
        {
            for(int i = 0;i<n;i++)
            {
                if(fun(str[i],S))
                    cout<<str[i]<<endl;
            }
        }
        else
        {
            for(int i = 0;i<n;i++)
            {
                if(fun2(str[i],S))
                    cout<<str[i]<<endl;
            }
        }
    }
    
    return 0;
}
/*
Hello
1
5
HelloWorld
HiHiHelloWorld
GrepIGreat
HELLO
HELLOIsnotHello
*/
B(2)
#include<iostream>
#include<string>
#include<vector>
#include<string.h>
#include<math.h>
using namespace std;
int main()
{
    char S[20];
    int option = 0;
    int n;
    char str[101];
    char temp[101];
    vector<string> ch;
    string a;
    while(cin>>S>>option>>n)
    {
        ch.clear();
        for(int i = 0;i<n;i++)
        {
            cin>>str;
            if(option == 1)
            {
                if(strstr(str,S))
                {
                    a = str;
                    ch.push_back(a);
                    //cout<<str<<endl;
                }
            }
            else
            {
                for(int j = 0;j<strlen(S);j++)
                    S[j] = tolower(S[j]);
                for(int j = 0;j<strlen(str);j++)
                    temp[j] = tolower(str[j]);
                if(strstr(temp,S))
                {
                    a = str;
                    ch.push_back(a);
                    //cout<<str<<endl;
                }
            }
        }
    }
    
    return 0;
}
/*
Hello
0
5
HelloWorld
HiHiHelloWorld
GrepIGreat
HELLO
HELLOIsnotHello
*/
C
#include<iostream>
using namespace std;
struct square{
    int x1;
    int y1;
    int x2;
    int y2;
}S[101];
int main()
{
    int n;
    int a[101][101]={0};
    int count;
    while(cin>>n)
    {
        count = 0;
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            cin>>S[i].x1>>S[i].y1>>S[i].x2>>S[i].y2;
            for(int j=S[i].x1;j<S[i].x2;j++)
            {
                for(int k = S[i].y1;k<S[i].y2;k++)
                {
                    a[j][k] = 1;
                }
            }
        }
        for(int j=0;j<101;j++)
        {
            for(int k = 0;k<101;k++)
            {
                if(a[j][k] == 1)
                    count++;
            }
        }
        cout<<count<<endl;
    }
    return 0;
}
D
#include<iostream>
#include<algorithm>
using namespace std;
struct record{
    int year;
    int month;
    int day;
    int score;
}rec[101];
bool cmp(record a,record b)
{
    if(a.score != b.score)
        return a.score>b.score;
    else if(a.year != b.year)
        return a.year<b.year;
    else if(a.month != b.month)
        return a.month < b.month;
    else
        return a.day < b.day;
}
int  main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%4d%*c%d%*c%d %d",&rec[i].year,&rec[i].month,&rec[i].day,&rec[i].score);
        }
        sort(rec,rec+n,cmp);
        for(int i=0;i<n;i++)
        {
            printf("%4d/%d/%d %d\n",rec[i].year,rec[i].month,rec[i].day,rec[i].score);
        }
    }
    return 0;
}
相關文章
相關標籤/搜索