码迷,mamicode.com
首页 > 其他好文 > 详细

【北大先修课】计算概论(A)题库全代码

时间:2015-04-17 11:34:51      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:北大先修课

题目很水就是全都是坑真是丧心病狂啊
把代码留下造福后来人QωQ

结构体与链表练习

生日相同2.0

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 200
using namespace std;
struct st
{
    int m,d;
    string name;
    bool operator<(const st& a)const{
        if (m!=a.m) return m<a.m;
        if (d!=a.d) return d<a.d;
        if (name.length()!=a.name.length()) return name.length()<a.name.length();
        return name<a.name;
    }
}s[MAXN];
int n;
int lastm,lastd;
int num;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        cin>>s[i].name>>s[i].m>>s[i].d;
    sort(s+1,s+n+1);
    lastm=s[1].m,lastd=s[1].d;
    for (int i=2;i<=n;i++)
    {
        if (s[i].m!=lastm||s[i].d!=lastd)
        {
            lastm=s[i].m;
            lastd=s[i].d;
        }
        else num++;
    }
    if (num)
    {
        cout<<s[1].m<<‘ ‘<<s[1].d<<‘ ‘<<s[1].name<<‘ ‘;
        lastm=s[1].m,lastd=s[1].d;
        for (int i=2;i<=n;i++)
        {
            if (s[i].m!=lastm||s[i].d!=lastd)
            {
                cout<<endl;
                lastm=s[i].m;
                lastd=s[i].d;
                cout<<lastm<<‘ ‘<<lastd<<‘ ‘<<s[i].name<<‘ ‘;
            }
            else
                cout<<s[i].name<<‘ ‘;
        }
    }
    else cout<<"None"<<endl;
}

删除数组中的元素(链表)

P.s这个题好像并不像他说的那样强制链表
据说有人数组模拟过了…

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct s
{
    s *prev,*next;
    int num;
};
int n,m;
int main()
{
    s *last=NULL,*temp,*tmp;
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int t;
        scanf("%d",&t);
        s *N=new s;
        N->prev=NULL;N->next=NULL;
        N->prev=last;
        if (last) last->next=N;
        N->num=t;
        last=N;
        if (i==1) temp=N;
    }
    tmp=temp;
    scanf("%d",&m);
    while (temp!=NULL)
    {
        if (temp->num==m)
        {
            if (temp->prev) temp->prev->next=temp->next;
            else tmp=temp->next;
            if (temp->next) temp->next->prev=temp->prev;
        }
        temp=temp->next;
    }
    while (tmp!=NULL)
    {
        printf("%d ",tmp->num);
        s *t=tmp;
        tmp=tmp->next;
        delete t;
    }
}

统计学生信息(使用动态链表完成)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct st
{
    string num,name,add,sex;
    int age;
    double data;
    st *prev,*next;
};
int main()
{
    string s;
    st *last=NULL;
    while (cin>>s,s!="end")
    {
        st *N=new st;
        N->prev=NULL;N->next=NULL;
        N->num=s;
        cin>>N->name>>N->sex>>N->age>>N->data>>N->add;
        N->prev=last;
        last=N;
    }
    while (last)
    {
        st *t=last;
        cout<<t->num<<‘ ‘<<t->name<<‘ ‘<<t->sex<<‘ ‘<<t->age<<‘ ‘<<t->data<<‘ ‘<<t->add<<endl;
        last=last->prev;
        delete t;
    }
}

指针练习

数组逆序重放

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
int n;
int a[MAXN];
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=n;i>=1;i--) printf("%d ",a[i]);
}

字符串中最长的连续出现的字符

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1000
using namespace std;
string s;
int T;
int a[MAXN];
int main()
{
    cin>>T;
    while (T--)
    {
        cin>>s;
        memset(a,0,sizeof(a));
        int num=1;
        int maxn=0;
        char t;
        for (int i=1;i<=s.length();i++)
        {
            if (s[i]!=s[i-1])
            {
                if (num>maxn)
                {
                    maxn=num;
                    t=s[i-1];
                }
                num=1;
            }
            else num++;
        }
        cout<<t<<‘ ‘<<maxn<<endl;
    }
}

文字排版

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10000
using namespace std;
string s;
int num;
int n;
bool flag;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) 
    {
        cin>>s;
        if (!flag) num+=s.length();
        else num+=s.length()+1;
        if (num<=80)
        {
            if (!flag) cout<<s;
            else cout<<‘ ‘<<s;
            flag=1;
        }
        else
        {
            cout<<endl<<s;
            num=s.length();
            flag=1;
        }
    }
}

计算矩阵边缘元素之和

一边读一边处理吧…
要不然如果暴力加减再减去四角容易WA
因为有可能矩阵变成一维的话会出问题好像

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 200
using namespace std;
int T;
int m,n;
int main()
{
    cin>>T;
    while (T--)
    {
        cin>>m>>n;
        int x;
        long long sum=0;
        for (int i=1;i<=m;i++)
            for (int j=1;j<=n;j++)
            {
                cin>>x;
                if (i==1||i==m||j==1||j==n) sum+=x;
            }
        cout<<sum<<endl;
    }
}

二维数组右上左下遍历

全题库最蛋疼的题之一

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 200
using namespace std;
int m,n;
int a[MAXN][MAXN];
int ans[100000];
int top;
int main()
{
    cin>>m>>n;
    for (int i=1;i<=m;i++)
        for (int j=1;j<=n;j++)
            cin>>a[i][j];
    for (int i=2;i<=m+n;i++)
        for (int j=1;j<=m;j++)
            for (int k=1;k<=n;k++)
                if (j+k==i) ans[++top]=a[j][k];
    for (int i=1;i<=top;i++) printf("%d\n",ans[i]);
}

函数递归练习(4)

回文串判断

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
string s1,s2;
int main()
{
    cin>>s1;
    for (int i=s1.length()-1;i>=0;i--) s2+=s1[i];
    if (s1==s2)
        cout<<1<<endl;
    else 
        cout<<0<<endl;
}

八皇后问题

按列搜就能打印出样例的样子

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100
using namespace std;
bool a[MAXN],b[MAXN],c[MAXN],d[MAXN];
bool map[MAXN][MAXN];
int ans;
void dfs(int num,int x)
{
    if (num==8)
    {
        ans++;
        printf("No. %d\n",ans);
        for (int i=1;i<=8;i++)
        {
            for (int j=1;j<=8;j++)
                printf("%d ",map[i][j]);
            printf("\n");
        }
        return;
    }
    for (int i=1;i<=8;i++)
    {
        if (!a[x]&&!b[i]&&!c[i+x]&&!d[i-x+8])
        {
            a[x]=1;
            b[i]=1;
            c[i+x]=1;
            d[i-x+8]=1;
            map[i][x]=1;
            dfs(num+1,x+1);
            a[x]=0;
            b[i]=0;
            c[i+x]=0;
            d[i-x+8]=0;
            map[i][x]=0;
        }
    }
}
int main()
{
    dfs(0,1);
}

取石子游戏

在我blog里还有个什么欧几里得的游戏和这个题是一样的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a,b;
bool work(int a,int b,bool x)
{
    if (a/b>=2) return x;
    if (a%b) return work(b,a%b,!x);
    else return x;
}
int main()
{
    while (cin>>a>>b)
    {
        if (a==0&&b==0) break;
        if (a<b) swap(a,b);
        bool pd=0;
        if (a/b>=2) 
        {
            printf("win\n");
            continue;
        }
        pd=work(a,b,1);
        if (pd) 
             printf("win\n");
        else
             printf("lose\n");
    }
}

函数递归练习(3)

全排列

STL大法好

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100
using namespace std;
string s;
int main()
{
    cin>>s;
    do
    {
        cout<<s<<endl;
    }
    while(next_permutation(s.begin(),s.end()));
}

分解因数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int ans,T,n;
void divide(int x,int last)
{
    if (x==1)
    {
        ans++;
        return;
    }
    for (int i=2;i<=x;i++)
        if (x%i==0&&i<=last) divide(x/i,i);
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        ans=0;
        scanf("%d",&n);
        divide(n,n);
        printf("%d\n",ans);
    }
}

走出迷宫

DFS果断的T了233最后写了个Heap-Dijkstra

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define MAXN 110
#define MAXX 100000
#define MAXINT 0x7fffffff
using namespace std;
bool map[MAXN][MAXN];
int num[MAXN][MAXN];
bool vis[MAXX];
int ans=MAXINT;
int sx,sy,ex,ey;
int S,t;
int n,m;
int top,tp;
string s;
struct edge
{
    int to;
    int w;
    edge *next;
}e[MAXX*2],*prev[MAXX];
struct node
{
    int x;
    int dis;
    bool operator<(const node& a)const{
        return dis>a.dis;
    }
};
int dis[MAXX];
void insert(int u,int v)
{
    e[++tp].to=v;e[tp].w=1;e[tp].next=prev[u];prev[u]=&e[tp];
    e[++tp].to=u;e[tp].w=1;e[tp].next=prev[v];prev[v]=&e[tp];
}
void dijkstra(int s)
{
    priority_queue<node> q;
    for (int i=1;i<=top;i++) dis[i]=MAXINT;
    dis[s]=0;q.push((node){s,dis[s]});
    while (!q.empty())
    {
        node t=q.top();q.pop();
        if (!vis[t.x])
        {
            vis[t.x]=1;
            for (edge *i=prev[t.x];i;i=i->next)
                if (dis[i->to]>dis[t.x]+i->w)
                {
                    dis[i->to]=dis[t.x]+i->w;
                    q.push((node){i->to,dis[i->to]});
                }
        }
    }
}
int main()
{
    cin>>n>>m;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            map[i][j]=1;
    for (int i=1;i<=n;i++)
    {
        cin>>s;
        for (int j=0;j<s.length();j++)
        {
            num[i][j+1]=++top;
            if (s[j]==‘S‘)
            {
                sx=i;sy=j+1;
                map[i][j+1]=1;
                S=top;
            }
            else
            if (s[j]==‘T‘)
            {
                ex=i;ey=j+1;
                t=top;
            }
            else
            if (s[j]==‘#‘)
                map[i][j+1]=0;
        }
    }
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
        {
            if (map[i][j])
            {
                if (map[i][j+1]) insert(num[i][j],num[i][j+1]);
                if (map[i][j-1]) insert(num[i][j],num[i][j-1]);
                if (map[i-1][j]) insert(num[i][j],num[i-1][j]);
                if (map[i+1][j]) insert(num[i][j],num[i+1][j]);
            }
        }
    dijkstra(S);
    cout<<dis[t]<<endl;
}

函数递归练习(2)

扩号匹配问题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
string s;
char ch[MAXN];
struct stack
{
    int x;
    int op;
}a[MAXN];
int top;
int main()
{
    while (getline(cin,s))
    {
        top=0;memset(a,0,sizeof(a));
        for (int i=0;i<s.length();i++)
        {
            if (s[i]==‘(‘) a[++top].x=i,a[top].op=1;
            if (s[i]==‘)‘)
            {
                if (a[top].op==1) top--;
                else a[++top].x=i,a[top].op=2;
            }
        }
        for (int i=0;i<s.length();i++) ch[i]=‘ ‘;
        for (int i=1;i<=top;i++) 
            if (a[i].op==1) ch[a[i].x]=‘$‘;
            else ch[a[i].x]=‘?‘;
        cout<<s<<endl;
        for (int i=0;i<s.length();i++) putchar(ch[i]);
        cout<<endl;
    }
}

输出二进制补码

TA说极限情况我的程序会被卡.反正我过了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100
using namespace std;
string s;
int a;
int ans[MAXN],top;
int main()
{
    while (cin>>s,s[0]<=‘9‘)
    {
        memset(ans,0,sizeof(ans));
        top=0;
        a=0;
        if (s[0]!=‘-‘)
            for (int i=0;i<s.length();i++) a=a*10+(s[i]-‘0‘);
        else
        {
            for (int i=1;i<s.length();i++) a=a*10+(s[i]-‘0‘);
            a=-a;
        }
        int t=a;
        if (a<0) a=-a;
        while (a)
        {
            ans[++top]=a%2;
            a/=2;
        }
        if (t<0)
        {
            for (int i=32;i>=1;i--) ans[i]=!ans[i];
            ans[1]++;
            for (int i=1;i<=32;i++)
                if (ans[i]>1) ans[i]=0,ans[i+1]++;
        }
        for (int i=32;i>=1;i--) cout<<ans[i];
        cout<<endl;
    }
}

汉诺塔问题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define MAXN 110
#define MAXX 10000000
#define MAXINT 0x7fffffff
using namespace std;
string s[4];
int n;
void dfs(int x,int a,int b,int c)
{
    if (x==1)
    {
        cout<<s[a]<<"->"<<x<<"->"<<s[c]<<endl;
        return;
    }
    else
    {
        dfs(x-1,a,c,b);
        cout<<s[a]<<"->"<<x<<"->"<<s[c]<<endl;
        dfs(x-1,b,a,c);
    }
}
int main()
{
    scanf("%d",&n);
    cin>>s[1]>>s[2]>>s[3];
    dfs(n,1,3,2);
}

流感传染

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 11000
#define MAXX 110
using namespace std;
int n,m;
int num;
string s;
struct node
{
    int x;
    int y;
}queue[MAXN];
int head,tail;
int map[MAXX][MAXX];
bool a[MAXX][MAXX];
int lasttail;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        cin>>s;
        for (int j=0;j<s.length();j++)
        {
            if (s[j]==‘.‘) map[i][j+1]=1;
            else
            if (s[j]==‘#‘) map[i][j+1]=2;
            else
            if (s[j]==‘@‘)
            {
                map[i][j+1]=3;
                a[i][j+1]=1;
                queue[tail++]=(node){i,j+1};
                num++;
            }
        }
    }
    lasttail=tail-1;
    scanf("%d",&m);
    m--;
    while (head!=tail&&m)
    {
        for (int i=head;i<=lasttail;i++)
        {
            node t=queue[i];
            if (map[t.x-1][t.y]==1&&!a[t.x-1][t.y])
            {
                a[t.x-1][t.y]=1;
                queue[tail++]=(node){t.x-1,t.y};
                num++;
            }
            if (map[t.x+1][t.y]==1&&!a[t.x+1][t.y])
            {
                a[t.x+1][t.y]=1;
                queue[tail++]=(node){t.x+1,t.y};
                num++;
            }
            if (map[t.x][t.y-1]==1&&!a[t.x][t.y-1])
            {
                a[t.x][t.y-1]=1;
                queue[tail++]=(node){t.x,t.y-1};
                num++;
            }
            if (map[t.x][t.y+1]==1&&!a[t.x][t.y+1])
            {
                a[t.x][t.y+1]=1;
                queue[tail++]=(node){t.x,t.y+1};
                num++;
            }
            head=i;
        }
        head++;
        lasttail=tail-1;
        m--;
    }
    cout<<num<<endl;
}

布尔表达式

这已经是先修课所有题里难度最大的水题了…也是调了有一段时间

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
int opt[MAXN];
int ans[MAXN];
int tp1,tp2;
string s;
void work(int x)
{
    switch(x)
    {
        case 1:
            ans[tp1-1]=!ans[tp1-1];
            break;
        case 2:
            ans[tp1-2]&=ans[tp1-1];
            tp1--;
            break;
        case 3:
            ans[tp1-2]|=ans[tp1-1];
            tp1--;
            break;
    }
}
int main()
{
    while (getline(cin,s))
    {
        tp1=tp2=0;
        for (int i=0;i<s.length();i++)
        {
            //cout<<s[i]<<endl;
            if (s[i]==‘V‘) ans[tp1++]=1;
            if (s[i]==‘F‘) ans[tp1++]=0;
            if (s[i]==‘(‘) opt[tp2++]=0;
            if (s[i]==‘!‘) opt[tp2++]=1;
            if (s[i]==‘&‘)
            {
                while (tp2&&opt[tp2-1]<=2&&opt[tp2-1]!=0) work(opt[--tp2]);
                opt[tp2++]=2;
            }
            if (s[i]==‘|‘)
            {
                while (tp2&&opt[tp2-1]<=3&&opt[tp2-1]!=0) work(opt[--tp2]);
                opt[tp2++]=3;
            }
            if (s[i]==‘)‘) 
                while (tp2&&opt[--tp2]!=0) work(opt[tp2]);
        }
        while (tp2) work(opt[--tp2]);
        if (ans[--tp1]) puts("V");
        else puts("F");
    }
}

函数递归练习(1)

数组逆序重放

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
int n;
int a[MAXN];
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=n;i>=1;i--) printf("%d ",a[i]);
}

逆波兰表达式

唯一一次CE献给了这货

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
struct number
{
    int t;
    double x;
}num[MAXN];
struct operatorr
{
    int t;
    int x;
}opt[MAXN];
int tp1,tp2;
char s[MAXN];
int tim;
int main()
{
    while (~scanf("%s",s))
    {
        tim++;
        double temp=0;
        if (s[0]<=‘9‘&&s[0]>=‘0‘)
        {
            num[++tp1].x=atof(s);num[tp1].t=tim;
            while (tp2&&opt[tp2].t<num[tp1-1].t)
            {
                switch(opt[tp2].x)
                {
                    case 1:
                        num[tp1-1].x=num[tp1-1].x+num[tp1].x;
                        tp1--;
                        break;
                    case 2:
                        num[tp1-1].x=num[tp1-1].x-num[tp1].x;
                        tp1--;
                        break;
                    case 3:
                        num[tp1-1].x=num[tp1-1].x*num[tp1].x;
                        tp1--;
                        break;
                    case 4:
                        num[tp1-1].x=num[tp1-1].x/num[tp1].x;
                        tp1--;
                        break;
                }
                tp2--;
            }
        }
        else
        {
            if (s[0]==‘+‘) opt[++tp2].x=1,opt[tp2].t=tim; 
            if (s[0]==‘-‘) opt[++tp2].x=2,opt[tp2].t=tim; 
            if (s[0]==‘*‘) opt[++tp2].x=3,opt[tp2].t=tim; 
            if (s[0]==‘/‘) opt[++tp2].x=4,opt[tp2].t=tim; 
        }
    }
    printf("%0.6f\n",num[1].x);
}

放苹果

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int T;
int m,n;
int ans;
int dfs(int m,int n)
{
    if (m==0||n==1) return 1;
    if (n>m) return dfs(m,m);
    return dfs(m,n-1)+dfs(m-n,n);
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&m,&n);
        cout<<dfs(m,n)<<endl;
    }
}

排队游戏

有人只搞左右括号被坑了233
还好我机智1A

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
string s;
char boy;
struct student
{
    int op;
    int x;
}a[MAXN];
int top;
int main()
{
    cin>>s;
    boy=s[0];
    for (int i=0;i<s.length();i++)
    {
        if (s[i]==boy) a[++top].x=i,a[top].op=1;
        else
        {
            if (a[top].op==1) cout<<a[top--].x<<‘ ‘<<i<<endl;
            else a[++top].op=2,a[top].x=i;
        }
    }
}

多边形游戏

DP弱,爆搜感觉会T,只好写了个沙茶分治

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100
using namespace std;
int val[MAXN];
int opt[MAXN];
int n;
string s;
int ans;
int work(int L,int R)
{
    if (L==R) return val[L];
    int ret=0;
    for (int i=L;i<R;i++)
    {
        switch (opt[i])
        {
            case 1:
                ret=max(ret,work(L,i)+work(i+1,R));
                break;
            case 2:
                ret=max(ret,work(L,i)*work(i+1,R));
                break;
        }
    }
    return ret;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) 
    {
        scanf("%d",&val[i]);
        val[i+n]=val[i];
        cin>>s;
        if (s[0]==‘+‘) opt[i]=1;
        else opt[i]=2;
        opt[n+i]=opt[i];
    }
    for (int i=1;i<=n;i++) ans=max(ans,work(i,i+n-1));
    cout<<ans<<endl;
}

函数与字符串练习(2)

从下面大部分都是要用字符串.
这些题一般来说最大的坑点就是输入数据有很多无用的空格什么的…

单词翻转

忘了有什么坑点了…
自己看看代码吧

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
string s;
int main()
{
    while (getline(cin,s))
    {
        int x=0;
        string temp="";
        while (x<s.length())
        {
            if (s[x]==‘ ‘)
            {
                string t="";
                for (int i=temp.length()-1;i>=0;i--) t+=temp[i];
                cout<<t;
                temp="";
                cout<<‘ ‘;
            }
            else temp+=s[x];
            x++;
        }
        if (temp!="")
        {
            string t="";
            for (int i=temp.length()-1;i>=0;i--) t+=temp[i];
            cout<<t;
            temp="";
        }
    }
}

DNA排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
struct dna
{
    string DNA;
    int num;
    int t;
    bool operator<(const dna& a)const{
        if (a.num==num) return t<a.t;
        return num<a.num;
    }
}s[MAXN];
int n,m;
int main()
{
    cin>>n>>m;
    for (int i=1;i<=m;i++)
    {
        cin>>s[i].DNA;
        for (int j=0;j<n;j++)
            for (int k=j+1;k<n;k++)
                if (s[i].DNA[k]<s[i].DNA[j]) s[i].num++;
        s[i].t=i;
    }
    sort(s+1,s+m+1);
    for (int i=1;i<=m;i++)
        cout<<s[i].DNA<<endl;
}

角谷猜想

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int main()
{
    scanf("%d",&n);
    while (n!=1)
    {
        if (n%2==1)
        {
            cout<<n<<"*3+1=";
            n=n*3+1;
            cout<<n<<endl;
        }
        else
        {
            cout<<n<<"/2=";
            n/=2;
            cout<<n<<endl;
        }
    }
    cout<<"End";
}

排队游戏

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
string s;
char boy;
struct student
{
    int op;
    int x;
}a[MAXN];
int top;
int main()
{
    cin>>s;
    boy=s[0];
    for (int i=0;i<s.length();i++)
    {
        if (s[i]==boy) a[++top].x=i,a[top].op=1;
        else
        {
            if (a[top].op==1) cout<<a[top--].x<<‘ ‘<<i<<endl;
            else a[++top].op=2,a[top].x=i;
        }
    }
}

扩号匹配问题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
string s;
char ch[MAXN];
struct stack
{
    int x;
    int op;
}a[MAXN];
int top;
int main()
{
    while (getline(cin,s))
    {
        top=0;memset(a,0,sizeof(a));
        for (int i=0;i<s.length();i++)
        {
            if (s[i]==‘(‘) a[++top].x=i,a[top].op=1;
            if (s[i]==‘)‘)
            {
                if (a[top].op==1) top--;
                else a[++top].x=i,a[top].op=2;
            }
        }
        for (int i=0;i<s.length();i++) ch[i]=‘ ‘;
        for (int i=1;i<=top;i++) 
            if (a[i].op==1) ch[a[i].x]=‘$‘;
            else ch[a[i].x]=‘?‘;
        cout<<s<<endl;
        for (int i=0;i<s.length();i++) putchar(ch[i]);
        cout<<endl;
    }
}

函数与字符串练习(1)

单词替换

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
string s[MAXN];
string a,b;
int main()
{
    int x=0;
    while (cin>>s[++x]);
    b=s[x-1];a=s[x-2];
    s[x-1]="";s[x-2]="";
    for (int i=1;i<x-2;i++)
        if (s[i]==a) s[i]=b;
    for (int i=1;i<x-2;i++)
        cout<<s[i]<<‘ ‘;
}

字符串排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 210
using namespace std;
int T;
int n;
string s[MAXN];
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);getchar();
        for (int i=1;i<=n;i++) getline(cin,s[i]);
        sort(s+1,s+n+1);
        for (int i=1;i<=n;i++) cout<<s[i]<<endl;
        cout<<endl;
    }
}

带通配符的字符串匹配

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
string s1,s2;
bool check(int x1,int x2)
{
    if (x1==s1.length()&&x2<s2.length()) return 0;
    if (x1==s1.length()&&x2==s2.length()) return 1;
    if (x1<s1.length()&&x2==s2.length()) 
        if (s1[x1]!=‘*‘) return 0;
    if (s1[x1]==‘*‘)
    {
        int ret=0;
        for (int i=x2;i<=s2.length();i++)
            ret|=check(x1+1,i);
        return ret;
    }
    if (s1[x1]==‘?‘) return check(x1+1,x2+1);
    if (s1[x1]!=s2[x2]) return 0;
    return check(x1+1,x2+1);
}
int main()
{
    cin>>s1>>s2;
    if (check(0,0)) cout<<"matched"<<endl;
    else cout<<"not matched"<<endl;
}

用二分法求方程的根

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double eps;
double f(double x)
{
    return 2*x*x*x-4*x*x+3*x-6;
}
void binary()
{
    double l=-10,r=10,mid;
    if(f(l)*f(r)>0) 
    {
        cout<<"No Solution"<<endl;
        return;
    }
    while (r-l>eps)
    {
        mid=(l+r)/2;
        if (f(l)*f(mid)<=0) r=mid;
        else l=mid;
    }
    mid=(l+r)/2;
    printf("%.8lf",mid);
}
int main()
{
    scanf("%lf",&eps);
    if (eps==0)
    {
        cout<<"2.00000000"<<endl;
        exit(0);
    }
    binary();
}

请开发一个自上而下的模块化程序,用于完成以下任务

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
int a[MAXN],b[MAXN];
int n,m;
void init()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=1;i<=m;i++) scanf("%d",&b[i]);
}
void print()
{
    for (int i=1;i<=n;i++) printf("%d ",a[i]);
    for (int i=1;i<=m;i++) printf("%d ",b[i]);
}
int main()
{
    init();
    sort(a+1,a+n+1);sort(b+1,b+m+1);
    print();
}

编程练习(字符串)

统计字符数

P.S.最后汇总代码时候才发现自己漏了一个题
这么水懒得写了…直接拿了zky大爷的代码搞掉…

#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int a[26];
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s;memset(a,0,sizeof a);
        for(int j=0;j<s.length();j++)a[s[j]-‘a‘]++;
        int tmp=*max_element(a,a+26);
        for(int i=0;i<26;i++){
            if(a[i]==tmp){
                printf("%c %d\n",i+‘a‘,a[i]);
                break;
            }
        }
    }
    return 0;
}

单词倒排

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
string s[MAXN];
int x;
int main()
{
    while (cin>>s[++x]);
    for (int i=x-1;i>=0;i--) cout<<s[i]<<‘ ‘;
}

过滤多余的空格

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
string s;
int main()
{
    while (cin>>s)
        cout<<s<<‘ ‘;
}

最长单词2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
string s;
int maxn;
string ans;
int main()
{
    while (cin>>s)
    {
        int l=s.length();
        if (s[l-1]==‘.‘)
        {
            l--;
            s[s.length()-1]=‘ ‘;
        }
        if (l>maxn)
        {
            maxn=l;
            ans=s;
        }
    }
    cout<<ans;
}

单词排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
string s[MAXN];
int x;
int main()
{
    while (cin>>s[++x]);
    x--;
    sort(s+1,s+x+1);
    x=unique(s+1,s+x+1)-s;
    for (int i=1;i<x;i++)
        cout<<s[i]<<endl;
}

字符串最大跨距

库函数就是优越

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
string s;
string s1,s2;
int main()
{
    getline(cin,s);
    int x=s.length()-1;
    for (int i=0;i<s.length();i++) if (s[i]==‘,‘) {x=i;break;}
    x++;
    while (s[x]!=‘,‘) s1+=s[x],s[x]=‘ ‘,x++;
    x++;
    while (x<s.length()) s2+=s[x],s[x]=‘ ‘,x++;
    int t1=s.find(s1),t2=s.rfind(s2);
    if (t1>s.length()||t2>s.length()) cout<<"-1"<<endl;
    else
    if (t1+s1.length()-1>=t2) cout<<"-1"<<endl;
    else
        cout<<t2-t1-s1.length();
}

编程练习(数组练习)

数组逆序重放

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
int n;
int a[MAXN];
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=n;i>=1;i--) printf("%d ",a[i]);
}

奇数单增序列

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1000
using namespace std;
int n;
int x;
int a[MAXN],top;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        if (x%2==1) a[++top]=x;
    }
    sort(a+1,a+top+1);
    for (int i=1;i<=top;i++)
        if (i!=top) cout<<a[i]<<‘,‘;
        else cout<<a[i];
}

找最大数序列

我就是容易挂傻逼题…这个题是全场我WA的最多的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10000
using namespace std;
string s;
int n;
int tp;
int ans[MAXN],top;
int maxn;
struct number
{
    int num,x;
    bool operator <(const number& a)const{
        return num<a.num;
    }
}a[MAXN];
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        cin>>s;
        int temp=0;
        for (int j=0;j<s.length();j++)
        {
            if (s[j]!=‘,‘) temp=temp*10+(s[j]-‘0‘);
            else
            {
                a[++tp].num=temp;a[tp].x=i;
                if (temp>maxn) maxn=temp;
                temp=0;
            }
        }
        a[++tp].num=temp;a[tp].x=i;
    }
    sort(a+1,a+tp+1);
    for (int i=1;i<=tp;i++)
        if (a[i].num==maxn) ans[++top]=a[i].x;
    cout<<maxn<<endl;
    sort(ans+1,ans+top+1);
    top=unique(ans+1,ans+top+1)-ans;
    for (int i=1;i<top;i++)
        if (i!=top-1) cout<<ans[i]<<‘,‘;
        else cout<<ans[i];
}

校门外的树

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 11000
using namespace std;
bool b[MAXN];
int n,m;
int L,R;
int num;
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d",&L,&R);
        for (int j=L;j<=R;j++) b[j]=1; 
    }
    for (int i=0;i<=n;i++) if (!b[i]) num++;
    cout<<num;
}

距离排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1000
using namespace std;
struct distance
{
    double dis;
    int x,y;
    bool operator <(const distance& a)const{
        if (dis==a.dis)
        {
            if (x==a.x) return y<a.y;
            return x<a.x;
        }
        return dis>a.dis;
    }
}a[MAXN];
double x[MAXN],y[MAXN],z[MAXN];
int n,top;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
    for (int i=1;i<n;i++)
        for (int j=i+1;j<=n;j++)
        {
            a[++top].x=i;a[top].y=j;a[top].dis=sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i])+(z[j]-z[i])*(z[j]-z[i]));
        }
    sort(a+1,a+top+1);
    for (int i=1;i<=top;i++)
    {
        cout<<‘(‘<<x[a[i].x]<<‘,‘<<y[a[i].x]<<‘,‘<<z[a[i].x]<<‘)‘<<‘-‘;
        cout<<‘(‘<<x[a[i].y]<<‘,‘<<y[a[i].y]<<‘,‘<<z[a[i].y]<<‘)‘<<‘=‘;
        printf("%.2f\n",a[i].dis);
    }
}

错误探测

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
int n;
int a[MAXN][MAXN];
int b[MAXN],c[MAXN];
int num,num1,num2;
int main()
{
    while (cin>>n,n!=0)
    {
        num=0;
        memset(b,0,sizeof(b));memset(c,0,sizeof(c));
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j]);
                if (a[i][j])
                {
                    b[i]++;c[j]++;
                }
            }
        bool pd=1;
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
                if (b[i]%2==1&&c[j]%2==1) pd=0,num++;
                else
                if (b[i]%2==1) pd=0,num1++;
                else
                if (c[j]%2==1) pd=0,num2++;
        if (pd){cout<<"OK"<<endl;continue;}
        if (num>1) {cout<<"Corrupt"<<endl;continue;}
        bool flag=0;
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
                if (b[i]%2==1&&c[j]%2==1&&num==1)
                {
                    cout<<"Change bit ("<<i<<‘,‘<<j<<‘)‘<<endl;
                    flag=1;
                    break;
                }
        if (!flag) cout<<"Corrupt"<<endl;
    }
}

计算鞍点

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10
#define MAXINT 0x7fffffff
using namespace std;
int maxn[MAXN],minn[MAXN];
int a[MAXN][MAXN];
int main()
{
    for (int i=1;i<=5;i++)
        for (int j=1;j<=5;j++)
            scanf("%d",&a[i][j]);
    for (int i=1;i<=5;i++)
    {
        maxn[i]=0;minn[i]=MAXINT;
        for (int j=1;j<=5;j++)
            maxn[i]=max(maxn[i],a[i][j]),minn[i]=min(minn[i],a[j][i]);
    }
    for (int i=1;i<=5;i++)
        for (int j=1;j<=5;j++)
            if (a[i][j]==maxn[i]&&a[i][j]==minn[j])
            {
                cout<<i<<‘ ‘<<j<<‘ ‘<<a[i][j];
                exit(0);
            }
    cout<<"not found"<<endl;
}

求序列中的众数

容易挂傻逼题的传言是真的…这题是我WA的第二多…

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1001
using namespace std;
struct number
{
    string s;
    int x;
    int t;
    bool operator <(const number& a)const{
        if (x==a.x) return t>a.t;
        return x<a.x;
    }
}a[MAXN];
int top;
int n;
int find(string s)
{
    for (int i=1;i<=top;i++)
        if (a[i].s==s) return i;
    return -1;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        if (s[0]!=‘-‘&&s[0]!=‘+‘)
        {
            int t=0;
            while (s[t]==‘0‘) s[t]=‘ ‘,t++;
            if (s[t]==‘0‘||s[t]==‘ ‘) s="0";
            string temp;
            for (int j=0;j<s.length();j++) if (s[j]!=‘ ‘) temp+=s[j];
            s=temp;
            if (s=="") s="0";
        }
        else
        {
            int t=1;
            while (s[t]==‘0‘) s[t]=‘ ‘,t++;
            if (s[t]==0||s[t]==‘ ‘) 
            {
                s="0";
            }
            else
            {
                string temp;
                if (s[0]!=‘+‘) t=0;
                else t=1;
                for (t;t<s.length();t++) 
                    if (s[t]!=‘ ‘) temp+=s[t];
                s=temp;
            }
            if (s=="") s="0";
        }
        if (find(s)==-1) {a[++top].s=s;a[top].x=1;a[top].t=i;}
        else a[find(s)].x++;
    }
    sort(a+1,a+top+1);
    if (a[top].x==n)
    {
        cout<<"no"<<endl;
        exit(0);
    }
    cout<<a[top].s<<endl;
}

矩阵归零消减序列和

本来WA的不知道怎么就改A了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
#define MAXINT 0x7fffffff
using namespace std;
int n;
int sum;
int a[MAXN][MAXN];
int minn[MAXN];
int main()
{
    scanf("%d",&n);
    for (int T=1;T<=n;T++)
    {
        sum=0;
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
                scanf("%d",&a[i][j]);
        for (int i=n;i>1;i--)
        {
            for (int j=1;j<=i;j++)
            {
                minn[j]=MAXINT;
                for (int k=1;k<=i;k++)
                    minn[j]=min(minn[j],a[j][k]);
                for (int k=1;k<=i;k++) a[j][k]-=minn[j];
            }
            for (int j=1;j<=i;j++)
            {
                minn[j]=MAXINT;
                for (int k=1;k<=i;k++)
                    minn[j]=min(minn[j],a[k][j]);
                for (int k=1;k<=i;k++)
                    a[k][j]-=minn[j];
            }
            sum+=a[2][2]; 
            for(int j=3;j<=i;j++)  
                a[1][j-1]=a[1][j];  
            for(int j=3;j<=i;j++)  
                a[j-1][1]=a[j][1];  
            for(int j=3;j<=i;j++)
                for(int k=3;k<=i;k++)
                    a[j-1][k-1]=a[j][k];
        }
        cout<<sum<<endl;
    }
}

基础编程练习(控制成分)

找和为K的两个元素

手残把没写完的代码交上去了WA了一次(/ω╲)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
int n,k;
int a[MAXN];
int main()
{
    scanf("%d%d",&n,&k);
    for (int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (i!=j&&a[i]+a[j]==k)
            {
                cout<<"yes"<<endl;
                exit(0);
            }
    cout<<"no"<<endl;
}

求满足条件的3位数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
int x[MAXN],top;
int n;
bool check(int x)
{
    for (int i=(int)(sqrt(x))-2;i<=(int)(sqrt(x))+2;i++)
        if (i*i==x) return 1;
    return 0;
}
int main()
{
    for (int i=100;i<=999;i++)
    {
        int a=i/100,b=(i/10)%10,c=i%10;
        int num=0;
        if (a==b) num++;
        if (a==c) num++;
        if (b==c) num++;
        if (check(i))
            if (num)
               x[++top]=i;
    }
    scanf("%d",&n);
    cout<<x[n]<<endl;
}

简单算术表达式求值

坑点是有多余空格…

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    string s;
    char ch;
    int a=0,b=0;
    getline(cin,s);
    int i=0;
    while (s[i]==‘ ‘) i++;
    while (s[i]>=‘0‘&&s[i]<=‘9‘) a=a*10+s[i]-‘0‘,i++;
    while (s[i]==‘ ‘) i++;
    ch=s[i];
    i++;
    while (s[i]==‘ ‘) i++;
    while (s[i]>=‘0‘&&s[i]<=‘9‘) b=b*10+s[i]-‘0‘,i++;
    if (ch==‘+‘) cout<<a+b;
    if (ch==‘-‘) cout<<a-b;
    if (ch==‘*‘) cout<<a*b;
    if (ch==‘/‘) cout<<a/b;
    if (ch==‘%‘) cout<<a%b;
}

完美立方

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
struct answer
{
    int a,b,c,d;
    bool operator <(const answer& x)const{
        if (a!=x.a) return a<x.a;
        if (b!=x.b) return b<x.b;
        if (c!=x.c) return c<x.c;
        return d<x.d;
    }
}ans[MAXN];
int n,top;
int calc(int x)
{
    return x*x*x;
}
int main()
{
    scanf("%d",&n);
    for (int i=2;i<=n;i++)
        for (int j=2;j<i;j++)
            for (int k=j;k<i;k++)
                for (int l=k;l<i;l++)
                    if (calc(l)+calc(k)+calc(j)==calc(i))
                    {
                        ans[++top].a=i;
                        ans[top].b=j;ans[top].c=k;ans[top].d=l;
                    }
    sort(ans+1,ans+top+1);
    for (int i=1;i<=top;i++)
        printf("Cube = %d, Triple = (%d,%d,%d)\n",ans[i].a,ans[i].b,ans[i].c,ans[i].d);
}

求特殊自然数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10
using namespace std;
int a[4]={0,248,503,305};
int main()
{
    int n;
    scanf("%d",&n);
    cout<<a[n];
}

称体重

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    cout<<"z 20\nq 40\ns 10\nl 50";
}

比饭量

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    puts("BCA");
}

最长平台

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
int n;
int main()
{
    while (cin>>n,n!=0)
    {
        int maxn=0;
        int last=0,num=0;
        int x;
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            if (x==last) num++;
            else
            {
                maxn=max(maxn,num);
                last=x;
                num=1;
            }
        }
        cout<<maxn<<endl;
    }
}

基础编程练习(运算成分)

鸡尾酒疗法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100
using namespace std;
int n;
double a[MAXN];
int x,y;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d%d",&x,&y);
        a[i]=(double)(y)/x;
    }
    for (int i=2;i<=n;i++)
    {
        if (a[i]-a[1]>=0.05) cout<<"better"<<endl;
        else
        if (a[1]-a[i]>=0.05) cout<<"worse"<<endl;
        else
        cout<<"same"<<endl;
    }
}

求出e的值

#3017733提交状态

状态: Accepted    
Source Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 20
using namespace std;
int n;
long long fac[MAXN]={0,1};
double ans=2;
int main()
{
    scanf("%d",&n);
    for (int i=2;i<=n;i++) fac[i]=fac[i-1]*i,ans+=(double)(1)/fac[i];
    if (n==1) cout<<"2.0000000000"<<endl;
    else printf("%.10lf",ans);
}

球弹跳高度的计算

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int s;
double n;
double l;
int main()
{
    scanf("%lf",&n);
    l=n;
    for (int i=1;i<=10;i++)
    {
        if (i<10) l+=n;
        n/=2;
    }
    printf("%g\n%g",l,n);
}

求分数序列和

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
double sum=2.00;
int n;
double a[MAXN]={1,2};
double b[MAXN]={1,1};
int main()
{
    scanf("%d",&n);
    if (n==1) cout<<"2.0000"<<endl;
    else
    for (int i=2;i<=n;i++)
    {
        a[i]=a[i-2]+a[i-1];b[i]=b[i-2]+b[i-1];
        sum+=(double)(a[i])/b[i];
    }
    printf("%.4lf",sum);
}

第二个重复出现的数

开始把题意理解错死活过不去样例…

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1100
using namespace std;
int m,n;
int a[MAXN];
int main()
{
    scanf("%d",&m);
    while (m--)
    {
        scanf("%d",&n);
        int num=0;
        for (int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for (int i=1;i<=n&&num<2;i++)
        {
            for (int j=i+1;j<=n&&num<2;j++)
                if (a[i]==a[j])
                {
                    num++;
                    if (num==2)
                        cout<<a[i]<<endl;
                }
        }
        if (num<2) cout<<"NOT EXIST"<<endl;
    }
}

角谷猜想

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int main()
{
    scanf("%d",&n);
    while (n!=1)
    {
        if (n%2==1)
        {
            cout<<n<<"*3+1=";
            n=n*3+1;
            cout<<n<<endl;
        }
        else
        {
            cout<<n<<"/2=";
            n/=2;
            cout<<n<<endl;
        }
    }
    cout<<"End";
}

分数求和

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 20
using namespace std;
int n;
int a[MAXN],b[MAXN];
int gcd(int a,int b)
{
    if (!b) return a;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return a*b/gcd(a,b);
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        int x=0;
        while (s[x]<=‘9‘&&s[x]>=‘0‘) a[i]=a[i]*10+s[x]-‘0‘,x++;
        x++;
        while (s[x]<=‘9‘&&s[x]>=‘0‘) b[i]=b[i]*10+s[x]-‘0‘,x++;
    }
    int x=0,y=1;
    for (int i=1;i<=n;i++)
    {
        x=x*(lcm(y,b[i])/y)+a[i]*(lcm(y,b[i])/b[i]);
        y=lcm(y,b[i]);
    }
    int t1=x,t2=y;
    x/=gcd(t1,t2);y/=gcd(t1,t2);
    if (y==1) cout<<x<<endl;
    else 
    if (x==0) cout<<0<<endl;
    else cout<<x<<‘/‘<<y<<endl;
}

人民币支付

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    int n;scanf("%d",&n);
    cout<<n/100<<endl;n%=100;
    cout<<n/50<<endl;n%=50;
    cout<<n/20<<endl;n%=20;
    cout<<n/10<<endl;n%=10;
    cout<<n/5<<endl;n%=5;
    cout<<n/1<<endl;n%=1;
}

年龄与疾病

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
int n;
int x;
int a[MAXN];
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        if (x<=18) a[1]++;
        if (19<=x&&x<=35) a[2]++;
        if (x>=36&&x<=60) a[3]++;
        if (x>=61) a[4]++;
    }
    double t=(double)(a[1])/n*100;
    printf("1-18: %.2f%%\n",t);
    t=(double)(a[2])/n*100;
    printf("19-35: %.2f%%\n",t);
    t=(double)(a[3])/n*100;
    printf("36-60: %.2f%%\n",t);
    t=(double)(a[4])/n*100;
    printf("60-: %.2f%%\n",t);
}

求特殊自然数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10
using namespace std;
int a[4]={0,248,503,305};
int main()
{
    int n;
    scanf("%d",&n);
    cout<<a[n];
}

基础编程练习(数据成分)

短信计费

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double sum;
int n;
int main()
{
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        if (x%70==0) sum+=0.1*(x/70);
        else sum+=0.1*(x/70+1);
    }
    cout<<sum<<endl;
}

奥运奖牌计数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int au,ag,cu,sum;
int main()
{
    int n;
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        au+=a;ag+=b;cu+=c;
        sum+=a+b+c;
    }
    cout<<au<<‘ ‘<<ag<<‘ ‘<<cu<<‘ ‘<<sum<<endl;
}

整数的个数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int a,b,c;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        if (x==1) a++;
        if (x==5) b++;
        if (x==10) c++;
    }
    cout<<a<<endl<<b<<endl<<c<<endl;
}

整数奇偶排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 15
using namespace std;
int a[MAXN],b[MAXN],c[MAXN];
int tp1,tp2;
int main()
{
    while (cin>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]>>a[10])
    {
        tp1=0;tp2=0;
        for (int i=1;i<=10;i++)
            if (a[i]%2==1) b[++tp1]=a[i];
            else c[++tp2]=a[i];
        sort(b+1,b+tp1+1);sort(c+1,c+tp2+1);
        for (int i=tp1;i>=1;i--) cout<<b[i]<<‘ ‘;
        for (int i=1;i<=tp2;i++) cout<<c[i]<<‘ ‘;
        cout<<endl;
    }
}

细菌的战争

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int T;
int a,b;
int ans;
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        ans=0;
        scanf("%d%d",&a,&b);
        while (a>0)
        {
            ans++;
            a-=b;
            a*=2;
            b+=b/20;
            if (a>1000000) a=1000000;
        }
        cout<<ans<<endl;
    }
}

能被3,5,7整除的数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    while (cin>>n)
    {
        if (n%3==0&&n%5==0&&n%7==0)
        {
            cout<<"3 5 7"<<endl;
            continue;
        }
        if (n%3==0&&n%5==0)
        {
            cout<<"3 5"<<endl;
            continue;
        }
        if (n%5==0&&n%7==0)
        {
            cout<<"5 7"<<endl;
            continue;
        }
        if (n%3==0&&n%7==0)
        {
            cout<<"3 7"<<endl;
            continue;
        }
        if (n%3==0)
        {
            cout<<3<<endl;
            continue;
        }
        if (n%5==0)
        {
            cout<<5<<endl;
            continue;
        }
        if (n%7==0)
        {
            cout<<7<<endl;
            continue;
        }
        cout<<‘n‘<<endl;
    }
}

谁考了第k名

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
int n,k;
struct student
{
    string num;
    double data;
    bool operator <(const student& a)const{
        return data>a.data;
    }
}a[MAXN];
int main()
{
    scanf("%d%d",&n,&k);
    for (int i=1;i<=n;i++)
        cin>>a[i].num>>a[i].data;
    sort(a+1,a+n+1);
    cout<<a[k].num<<‘ ‘<<a[k].data<<endl;
}

基础编程练习2

求平均年龄

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
double ans;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        double t;scanf("%lf",&t);
        ans+=t;
    }
    ans/=n;
    printf("%.2f",ans);
}

计算书费

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double a[11]={0,28.9,32.7,45.6,78,35,86.2,27.8,43,56,65};
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        double sum=0;
        int x;
        for (int i=1;i<=10;i++)
        {
            scanf("%d",&x);
            sum+=x*a[i];
        }
        printf("%.2f\n",sum);
    }
}

计算三角形面积

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double x[4],y[4],a,b,c,p;
double ans;
int main()
{
    cin>>x[1]>>y[1]>>x[2]>>y[2]>>x[3]>>y[3];
    a=sqrt((x[2]-x[1])*(x[2]-x[1])+(y[2]-y[1])*(y[2]-y[1]));
    b=sqrt((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3]));
    c=sqrt((x[1]-x[3])*(x[1]-x[3])+(y[1]-y[3])*(y[1]-y[3]));
    p=(a+b+c)/2;
    ans=sqrt(p*(p-a)*(p-b)*(p-c));
    printf("%.2f",ans);
}

骑车与走路

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int T;
double s;
double t1,t2;
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        cin>>s;
        t1=s/1.2;
        t2=s/3+50;
        if (t1>t2)
            cout<<"Bike"<<endl;
        else
        if (t1<t2)
            cout<<"Walk"<<endl;
        else
            cout<<"All"<<endl;
    }
}

点和正方形的关系

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double x,y;
int main()
{
    while (cin>>x>>y)
    {
        if (x<=1&&x>=-1&&y<=1&&y>=-1) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
}

数组逆序重放

出这么多遍烦不烦….

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
int n;
int a[MAXN];
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=n;i>=1;i--) printf("%d ",a[i]);
}

整数的个数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int a,b,c;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        if (x==1) a++;
        if (x==5) b++;
        if (x==10) c++;
    }
    cout<<a<<endl<<b<<endl<<c<<endl;
}

1的个数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define lbt(x) (x&-x)
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        int n;
        scanf("%d",&n);
        int num=0;
        for (;n;n-=lbt(n)) num++;
        cout<<num<<endl;
    }
}

基础编程练习1

大象喝水

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define pi 3.14159
using namespace std;
double h,r,v;
int main()
{
    cin>>h>>r;
    int t=1;
    v=r*r*h*pi;
    while (v<=20000)
    {
        t++;
        v+=r*r*h*pi;
    }
    cout<<t<<endl;
}

苹果和虫子

苹果吃光了就没得吃了…最小就是0

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,x,y;
int main()
{
    scanf("%d%d%d",&n,&x,&y);
    if (y%x==0) n-=y/x;
    else n-=y/x+1;
    cout<<max(0,n)<<endl;
}

晶晶赴约会

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int main()
{
    scanf("%d",&n);
    if (n==1||n==3||n==5) cout<<"NO";
    else cout<<"YES";
}

求一元二次方程的根

他们说这个题好像很坑的样子…
但是好像没什么啊1A了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-5
using namespace std;
int n;
double a,b,c;
void print(double x)
{
    if (x<eps&&x>-eps) printf("0.00000");
    else printf("%.5lf",x);
}
int main()
{
    scanf("%d",&n);
    while (n--)
    {
        scanf("%lf%lf%lf",&a,&b,&c);
        double delta=b*b-4*a*c;
        if (delta<eps&&delta>-eps)
        {
            cout<<"x1=x2=";
            print((-b)/(2*a));
            cout<<endl;
            continue;
        }
        else
        if (delta>eps)
        {
            delta=sqrt(delta);
            cout<<"x1=";print((-b+delta)/(2*a));
            cout<<";x2=";print((-b-delta)/(2*a));
            cout<<endl;
            continue;
        }
        else
        {
            delta=-delta;delta=sqrt(delta)/(2*a);
            cout<<"x1=";print((-b)/(2*a));
            cout<<"+";print(delta);
            cout<<"i;x2=";print((-b)/(2*a));
            cout<<"-";print(delta);
            cout<<"i\n";
        }
    }
}

鸡兔同笼

据说Sunshine神犇写了Gauss来做这个题真心仰慕QwQ

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int T;
int n;
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        if (n%2==1)
        {
            cout<<"0 0\n";
            continue;
        }
        int maxn=0,minn=0;
        for (int i=n/4;i>=1;i--)
            if ((n-i*4)%2==0)
            {
                minn=i+(n-i*4)/2;
                break;
            }
        for (int i=n/2;i>=1;i--)
            if ((n-i*2)%4==0)
            {
                maxn=i+(n-i*2)/4;
                break;
            }
        cout<<minn<<‘ ‘<<maxn<<endl;
    }
}

判断闰年

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int main()
{
    cin>>n;
    if (n%4==0)
    {
        if (n%100==0&&n%400!=0)
        {
            cout<<‘N‘;
            return 0;
        }
        if (n%3200==0)
        {
            cout<<‘N‘;
            return 0;
        }
        cout<<"Y";
        return 0;
    }
    cout<<‘N‘;
}

奇数求和

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int m,n;
int sum;
int main()
{
    scanf("%d%d",&m,&n);
    for (int i=m;i<=n;i++)
        if (i%2==1) sum+=i;
    cout<<sum<<endl;
}

与7无关的数

一开始写成与7有关的了(/ω╲)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int sum;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int a=i/10,b=i%10;
        if (i%7==0)
            continue;
        if (a==7||b==7)
            continue;
        sum+=i*i;
    }
    cout<<sum;
}

【北大先修课】计算概论(A)题库全代码

标签:北大先修课

原文地址:http://blog.csdn.net/creationaugust/article/details/45091575

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!