机试

//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、即对于一个合数总存在有质数因子,所以我们在分解合数的时候不需要判断被整除的因子是否为质数。
2、理解是一个难点,对于一个合数,我们将小于等于这个合数平方根的质因子整除完之后,若
余下的书大于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]!='')
    {
        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
",rec[i].year,rec[i].month,rec[i].day,rec[i].score);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ttzz/p/10622243.html