Light OJ Beginners Problems

题目: Beginners Problems

解题报告:

1000 Greetings from LightOJ

#include <cstdio>
int main()
{
    int t;
    int a,b;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%d%d",&a,&b);
        printf("Case %d: %d\n",i,a+b);
    }
    return 0;
}

1001 Opposite Task

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int a=0,b=n;
        if(n>10)
            a=10,b=n-10;
        printf("%d %d\n",a,b);
    }
    return 0;
}

1006  Hex-a-bonacci

构造矩阵

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
const long long mod=10000007;
using namespace std;
struct Matrix
{
    long long m[7][7];//之前用的int,WA了,然后__int64 PE 了
};
Matrix E,B;
int a[7];
void Init()
{
    for(int i=1;i<=6;i++)
    for(int j=1;j<=6;j++)
    {
        E.m[i][j]=(i==j);
        B.m[i][j]=0;
    }
    for(int i=1;i<=6;i++)
        B.m[i][1]=1;
    for(int i=1;i<=5;i++)
        B.m[i][i+1]=1;
}
Matrix Multi(Matrix A,Matrix B,int M,int N,int K)
{
    Matrix ans;
    for(int i=1;i<=M;i++)
        for(int j=1;j<=N;j++)
        {
            ans.m[i][j]=0;
            for(int k=1;k<=K;k++)
            {
                ans.m[i][j]=(ans.m[i][j]+((A.m[i][k]%mod)*(B.m[k][j])%mod))%mod;
            }  // 我去,WA了三次,就感觉这里有问题,尼玛,改成longlong就过了,__int64会PE
        }
    return ans;
}
Matrix Pow(Matrix A,int k)
{
    Matrix ans=E;
    while(k)
    {
        if(k&1)
        {
            k--;
            ans=Multi(ans,A,6,6,6);
        }
        else
        {
            k/=2;
            A=Multi(A,A,6,6,6);
        }
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    Init();
    for(int ii=1;ii<=t;ii++)
    {
        int n;
        Matrix D;
        for(int i=1;i<=6;i++)
        {
            scanf("%d",&a[i]);
            D.m[1][7-i]=a[i];
        }
        scanf("%d",&n);
        printf("Case %d: ",ii);
        if(n<6)
            printf("%d\n",a[n+1]%mod);
        else
        {
            n-=5;
            Matrix ans=Multi(D,Pow(B,n),1,6,6);
            printf("%lld\n",ans.m[1][1]%mod);
        }
    }
    return 0;
}

1008  Fibsieve`s Fantabulous Birthday

找规律

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
long long t,n;
long long x,y;
void solve()
{
    long long cnt=sqrt(n*1.0);
    if(cnt*cnt<n)
        cnt++;
    n-=(cnt-1)*(cnt-1);
    if(cnt&1)
    {
        if(n>=cnt)
        {
            y=cnt;
            x=n-cnt;
            x=cnt-x;
        }
        else
        {
            x=cnt;
            y=n;
        }
    }
    else
    {
        if(n>=cnt)
        {
            x=cnt;
            y=n-cnt;
            y=cnt-y;
        }
        else
        {
            y=cnt;
            x=n;
        }
    }
}
int main()
{
    scanf("%lld",&t);
    for(long long i=1;i<=t;i++)
    {
        scanf("%lld",&n);
        solve();
        printf("Case %lld: %lld %lld\n",i,x,y);
    }
    return 0;
}

1010  Knights in Chessboard

//我去  WA了那么多次。。。。
#include <cstdio>
#include <iostream>
using namespace std;
int solve(int m,int n)
{
    if(m==1||n==1)
        return m*n;
    if(m==2||n==2)
    {
        int ans=m*n/8*4;
        if((m*n)%8==2)
            return ans+=2;
        if((m*n)%8==6||(m*n)%8==4)
            return ans+=4;
    }
    return (m*n+1)/2;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("Case %d: ",i);
        printf("%d\n",solve(a,b));
    }
    return 0;
}

1015  Brush (I)

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int n;
        scanf("%d",&n);
        int ans=0,a;
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&a);
            if(a>0)
                ans+=a;
        }
        printf("Case %d: %d\n",i,ans);
    }
    return 0;
}

1022  Circle in Square

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define PI (2*acos(0.0))
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        double n;
        scanf("%lf",&n);
        printf("Case %d: %.2lf\n",i,4*n*n-n*n*PI);
    }
    return 0;
}

1042  Secret Origins

好题 位运算 STL

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int fun(string a)
{
    int ans=1;
    for(int i=1;i<a.size();i++)
    {
        if(a[i]=='0')
            ans*=2;
        else
            ans=(ans*2+1);
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int n;
        scanf("%d",&n);
        string str="";
        while(n)
        {
            if(n&1)
                str.insert(0,"1");
            else
                str.insert(0,"0");
            n/=2;
        }
        if(!next_permutation(str.begin(),str.end()))
        {
            str.insert(0,"10");
            str.erase(str.size()-1,1);
        }
        printf("Case %d: %d\n",i,fun(str));
    }
    return 0;
}

1045  Digits of Factorial

math

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
#define maxn 1000001
double a[maxn];
int main()
{
    a[0]=0;
    for(int i=1;i<=maxn;i++)
        a[i]=a[i-1]+log(i+0.0);
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int m,n;
        scanf("%d%d",&m,&n);
        printf("Case %d: %.lf\n",i,floor(a[m]/log(n*1.0)+1));
    }
    return 0;
}

1053  Higher Math

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
long long a[4];
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        for(int j=1;j<=3;j++)
            scanf("%lld",&a[j]);
        sort(a+1,a+4);
        printf("Case %d: ",i);
        if(a[1]*a[1]+a[2]*a[2]==a[3]*a[3])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

1069  Lift

#include <cstdio>
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        int ans=a-b;
        ans>0?ans:ans*=-1;
        ans+=a;
        ans*=4;
        ans+=19;
        printf("Case %d: ",i);
        printf("%d\n",ans);
    }
    return 0;
}

1072   Calm Down

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define PI 3.14159265
using namespace std;
int t;
int main()
{
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        double R;
        int n;
        scanf("%lf%d",&R,&n);
        double r=R*sin(PI/n)/(1+sin(PI/n));
        printf("Case %d: %lf\n",i,r);
    }
    return 0;
}

1107  How Cow

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        int n;
        scanf("%d",&n);
        printf("Case %d:\n",i);
        while(n--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(a>x1&&a<x2&&b>y1&&b<y2)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

1109  False Ordering

算约数个数 排序

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1001
struct S
{
    int a,b;
};
S s[maxn];
int prime[maxn];
void Prime()
{
    memset(prime,1,sizeof(prime));
    prime[0]=prime[1]=0;
    for(int i=2;i*i<=maxn;i++)
        if(prime[i])
            for(int j=2*i;j<=maxn;j+=i)
                prime[j]=0;
}
int tmp[maxn];
bool cmp(S x,S y)
{
    if(x.a<y.a)
        return true;
    if(x.a==y.a)
        return x.b>y.b;
    return false;
}
int main()
{
    Prime();
    for(int i=1;i<=maxn;i++)
        tmp[i]=1;
    for(int i=1;i<=maxn;i++)
    {
        for(int j=2;j<=i;j++)
            if(prime[j]&&i%j==0)
            {
                int cnt=1;
                int ii=i;
                while(ii%j==0)
                {
                    ii/=j;
                    cnt++;
                }
                tmp[i]*=cnt;
            }
    }
    for(int i=1;i<maxn;i++)
    {
        s[i].a=tmp[i];
        s[i].b=i;
    }
    sort(s+1,s+maxn,cmp);
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int n;
        scanf("%d",&n);
        printf("Case %d: %d\n",i,s[n].b);
    }
    return 0;
}

1113  Discover the Web

#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        stack<string>F,B;
        string str="http://www.lightoj.com/",command;
        printf("Case %d:\n",i);
        while(cin>>command&&command!="QUIT")
        {
            if(command=="BACK")
            {
                if(B.empty())
                    printf("Ignored\n");
                else
                {
                    F.push(str);
                    str=B.top();
                    B.pop();
                    cout<<str<<endl;
                }
            }
            else if(command=="FORWARD")
            {
                if(F.empty())
                    printf("Ignored\n");
                else
                {
                    B.push(str);
                    str=F.top();
                    F.pop();
                    cout<<str<<endl;
                }
            }
            else
            {
                B.push(str);
                cin>>str;
                cout<<str<<endl;
                while(F.size())
                    F.pop();
            }
        }
    }
    return 0;
}

1116  Ekka Dokka

#include <cstdio>
#include <iostream>
using namespace std;
int t;
int main()
{
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        long long n;
        scanf("%lld",&n);
        printf("Case %d: ",i);
        if(n&1)
        {
            printf("Impossible\n");
        }
        else
        {
            long long ans=1;
            while(n%2==0)
            {
                ans*=2;
                n/=2;
            }
            printf("%lld %lld\n",n,ans);
        }
    }
    return 0;
}

1133  Array Simulation

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int a[110];
char str[10];
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        while(m--)
        {
            scanf("%s",str);
            if(str[0]=='P')
            {
                int x,y;
                scanf("%d%d",&x,&y);
                x++;y++;
                swap(a[x],a[y]);
            }
            else if(str[0]=='R')
            {
                for(int i=1;i<=n/2;i++)
                    swap(a[i],a[n+1-i]);
            }
            else if(str[0]=='S')
            {
                int num;
                scanf("%d",&num);
                for(int i=1;i<=n;i++)
                    a[i]+=num;
            }
            else if(str[0]=='M')
            {
                int num;
                scanf("%d",&num);
                for(int i=1;i<=n;i++)
                    a[i]*=num;
            }
            else
            {
                int num;
                scanf("%d",&num);
                for(int i=1;i<=n;i++)
                    a[i]/=num;
            }
        }
        printf("Case %d:\n",ii);
        for(int i=1;i<n;i++)
            printf("%d ",a[i]);
        printf("%d\n",a[n]);
    }
    return 0;
}

1136  Division by 3

分治

/*
// 分治
// 先求从1到n的个数
//枚举之后找规律
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[60];
int b[60];
int main()
{
    int ans=0;
    memset(b,0,sizeof(b));
    for(int i=1;i<=9;i++)
    {
        ans+=i%10;
        ans%=3;
        if(ans%3==0)
            b[i]=b[i-1]+1;
        else
            b[i]=b[i-1];
    }
    for(int i=10;i<=50;i++)
    {
        int ii=i;
        ans+=ii%10;
        ii/=10;
        ans+=ii;
        ans%=3;
        if(ans%3==0)
            b[i]=b[i-1]+1;
        else
            b[i]=b[i-1];
    }
    for(int i=1;i<=50;i++)
        printf("%d.......%d\n",i,b[i]);
    return 0;
}
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int fun(int n)
{
    if(n==1||n==0)
        return 0;
    return n-1-(n-1)/3;
}
int main()
{
    int a,b,t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%d%d",&a,&b);
        printf("Case %d: %d\n",i,fun(b)-fun(a-1));
    }
    return 0;
}

1182  Parity

// 之前看那个位运算的时候发现了这篇博客
// http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=bitManipulation
// 用了下 __builtin_popcount() 感觉还可以
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int n;
        scanf("%d",&n);
        printf("Case %d: ",i);
        if(__builtin_popcount(n)&1)
            printf("odd\n");
        else
            printf("even\n");
    }
    return 0;
}

1189  Sum of Factorials

// 先打表看了一下,18的阶乘就是一个18位数
// 所以就算是枚举也不算难
// 但是这个题可以贪心直接做,因为前n-1项的和肯定是小于第n项的
#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
long long fac(long long n)
{
    if(n==1||n==0)
        return 1;
    return n*fac(n-1);
}
long long f[20];
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        long long n;
        scanf("%lld",&n);
        printf("Case %d: ",i);
        stack<int>s;
        while(!s.empty())
            s.pop();
        int cnt=20;
        int tag=0;
        while(n)
        {
            if(cnt<0)
            {
                tag=1;
                break;
            }
            if(n>=fac(cnt))
            {
                n-=fac(cnt);
                s.push(cnt);
            }
            cnt--;
        }
        if(tag)
            printf("impossible\n");
        else
        {
            while(s.size()!=1)
            {
                printf("%d!+",s.top());
                s.pop();
            }
            printf("%d!\n",s.top());
        }
    }
    return 0;
}

1202  Bishops

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        printf("Case %d: ",i);
        x1=abs(x1-x2);
        y1=abs(y1-y2);
        if(x1==y1)
            printf("1\n");
        else
        {
            if(x1%2==y1%2)
                printf("2\n");
            else
                printf("impossible\n");
        }
    }
    return 0;
}

1211  Intersection of Cubes

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int n;
        int x1,x2,y1,y2,z1,z2;
        int x=0,y=0,z=0;
        int X=1001,Y=1001,Z=1001;
        scanf("%d",&n);
        printf("Case %d: ",i);
        for(int j=1;j<=n;j++)
        {
            scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
            x=max(x,x1);
            y=max(y,y1);
            z=max(z,z1);
            X=min(x2,X);
            Y=min(y2,Y);
            Z=min(z2,Z);
        }
        if(X>x&&Y>y&&Z>z)
            printf("%d\n",(X-x)*(Y-y)*(Z-z));
        else
            printf("0\n");
    }
    return 0;
}

1212  Double Ended Queue

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <deque>
using namespace std;
string str;
int num;
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        printf("Case %d:\n",i);
        int m,n;
        scanf("%d%d",&n,&m);
        deque<int>q;
        while(m--)
        {
            cin>>str;
            if(str=="pushLeft")
            {
                cin>>num;
                if(q.size()!=n)
                {
                    q.push_back(num);
                    cout<<"Pushed in left: "<<num<<endl;
                }
                else
                    cout<<"The queue is full"<<endl;
            }
            else if(str=="pushRight")
            {
                cin>>num;
                if(q.size()!=n)
                {
                    q.push_front(num);
                    cout<<"Pushed in right: "<<num<<endl;
                }
                else
                    cout<<"The queue is full"<<endl;
            }
            else if(str=="popRight")
            {
                if(q.empty())
                    cout<<"The queue is empty"<<endl;
                else
                {
                    cout<<"Popped from right: "<<q.front()<<endl;
                    q.pop_front();
                }
            }
            else
            {
                if(q.empty())
                    cout<<"The queue is empty"<<endl;
                else
                {
                    cout<<"Popped from left: "<<q.back()<<endl;
                    q.pop_back();
                }
            }
        }
    }
    return 0;
}

1214  Large Division

大数整除否

// 我去。。套了模板样例最后一个还是过不了
//  算了 还是看大神的做法
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
bool is_ok(vector<long long>a,int b)
{
    for(int i=a.size()-1;i>=0;i--)
    {
        long long cnt=(a[i]%b)*10;
        if(cnt!=0&&i==0)
            return false;
        if(i!=0)
            a[i-1]+=cnt;
    }
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        vector<long long>a;
        string aa;
        int b;
        cin>>aa;
        cin>>b;
        printf("Case %d: ",ii);
        for(int i=aa.size()-1;i>=0;i--)
            if(aa[i]!='-')
                a.push_back(aa[i]-'0');
        if(is_ok(a,b))
            printf("divisible\n");
        else
            printf("not divisible\n");
    }
    return 0;
}

1216  Juice in the Glass

算圆锥体积

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#define PI (2*acos(0.0))
using namespace std;
int main()
{
    int cases;
    cin>>cases;
    for(int ii=1;ii<=cases;ii++)
    {
        double r1,r2,p,h;
        cin>>r1>>r2>>h>>p;
        cout<<"Case "<<ii<<": ";
        double ans,x,v;
        x=h*r2/(r1-r2);
        ans=(r1-r2)/h*(p+x);
        v=1.0/3*PI*ans*ans*(p+x)-1.0/3*PI*r2*r2*x;
        printf("%.8lf\n",v);
    }
    return 0;
}

1225  Palindromic Numbers (II)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char s[20];
char str[20];
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%s",s);
        printf("Case %d: ",i);
        strcpy(str,s);
        //strrev(str);  我去,用这个PE了
        for(int i=0;i<strlen(str)/2;i++)
            swap(str[i],str[strlen(str)-1-i]);
        if(strcmp(s,str)==0)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

1227  Boiled Eggs

贪心

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int a[40];
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        int n,p,q;
        scanf("%d%d%d",&n,&p,&q);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        printf("Case %d: ",ii);
        int ans=0;
        int cnt=1;
        q-=a[cnt];
        while(q>=0)
        {
            ans++;
            if(cnt==n) //这个地方之前忽略了,WA了
                break; // 细节问题要重视啊
            cnt++;
            q-=a[cnt];
            if(ans>=p)
                break;
        }
        printf("%d\n",ans);
    }
    return 0;
}

1241  Pinocchio

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int a[20];
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int ans=0;
        a[0]=2;//我去,漏掉这个了,看了好久的forum,之前当0做的
        for(int i=0;i<=n-1;i++)
        {
            if(a[i+1]>a[i])
                ans+=(a[i+1]-a[i]-1)/5+1;
        }
        printf("Case %d: %d\n",ii,ans);
    }
    return 0;
}

1249  Chocolate Thief

结构体排序

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
struct stu
{
    char name[25];
    int sum;
};
bool cmp(stu x,stu y)
{
    return x.sum<y.sum;
}
stu s[101];
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int x,y,z;
            scanf("%s%d%d%d",s[i].name,&x,&y,&z);
            s[i].sum=x*y*z;
        }
        printf("Case %d: ",ii);
        sort(s+1,s+n+1,cmp);
        if(s[1].sum==s[n].sum)
            printf("no thief\n");
        else
        {
            printf("%s took chocolate from %s\n",s[n].name,s[1].name);
        }
    }
    return 0;
}

1261  K-SAT Problem

这个做到想吐

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int t,num;
    cin>>t;
    for(int ii=1;ii<=t;ii++)
    {
        cout<<"Case "<<ii<<": ";
        int n,m,k,p;
        cin>>n>>m>>k;
        vector<int>v1(m+1);
        vector<vector<int> >v2(n+1);
        for(int i=1;i<=n;i++)
        {
            v2[i].push_back(0);
            for(int j=1;j<=k;j++)
            {
                cin>>num;
                v2[i].push_back(num);
            }
        }
        cin>>p;
        for(int i=1;i<=p;i++)
        {
            cin>>num;
            v1[num]=1;
        }
        bool tag=0;
        for(int i=1;i<=n;i++)
        {
            int cnt=0;
            for(int j=1;j<=k;j++){
                if(v2[i][j]<0&&!v1[-v2[i][j]])
                    cnt++;
                if(v2[i][j]>0&&v1[v2[i][j]])
                    cnt++;
            }
            if(cnt==0)
                tag=1;
        }
        if(!tag)
            cout<<"Yes\n";
        else
            cout<<"No\n";
    }
    return 0;
}

1294  Positive Negative Sign

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    long long a,b;
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%lld%lld",&a,&b);
        printf("Case %d: ",i);
        printf("%lld\n",a/2*b);
    }
    return 0;
}

1305  Area of a Parallelogram

平行四边形面积

#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
struct LPoint
{
    double x,y;
};
struct Ldir//表示方向向量
{
    double dx,dy;
};
struct Line
{
    LPoint p;
    Ldir dir;
};
void format(Line l,double &A,double &B,double &C)
{
    A=l.dir.dy;
    B=-l.dir.dx;
    C=l.p.y*l.dir.dx-l.p.x*l.dir.dy;
}
double p2ldis(LPoint a,Line l)
{
    double A,B,C;
    format(l,A,B,C);
    return (fabs(A*a.x+B*a.y+C)/sqrt(A*A+B*B));
}
double Distance(LPoint a,LPoint b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        LPoint po[4];
        for(int i=1;i<=3;i++)
            scanf("%lf%lf",&po[i].x,&po[i].y);
        po[0].x=po[1].x+po[3].x-po[2].x;
        po[0].y=po[1].y+po[3].y-po[2].y;
        double ans=Distance(po[1],po[2]);
        Line l;
        l.p=po[1];
        l.dir.dx=po[2].x-po[1].x;
        l.dir.dy=po[2].y-po[1].y;
        ans*=p2ldis(po[0],l);
        printf("Case %d: ",ii);
        printf("%.lf %.lf %.lf\n",po[0].x,po[0].y,ans);
    }
    return 0;
}

1311  Unlucky Bird

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int tt;
    scanf("%d",&tt);
    for(int ii=1;ii<=tt;ii++)
    {
        double v1,v2,v3,a1,a2;
        scanf("%lf%lf%lf%lf%lf",&v1,&v2,&v3,&a1,&a2);
        double t,s;
        s=v1*v1/2/a1+v2*v2/2/a2;
        t=max(v1/a1,v2/a2);
        printf("Case %d: %lf %lf\n",ii,s,t*v3);
    }
    return 0;
}

1331  Agent J

求面积

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
#define PI (2*acos(0.0))
double sita(double a,double b,double c)
{
    return acos((a*a+b*b-c*c)/(2*a*b));
}
double area(double a,double b,double c)
{
    double ans=(a+b+c)/2;
    return sqrt(ans*(ans-a)*(ans-b)*(ans-c));
}// 之前求三角形面积用角度求的,WA了,还是海伦公式丢失精度低
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        double r1,r2,r3;
        scanf("%lf%lf%lf",&r1,&r2,&r3);
        double a=r1+r2;
        double b=r2+r3;
        double c=r3+r1;
        double ans=area(a,b,c);
        ans-=sita(a,c,b)/2*r1*r1;
        ans-=sita(a,b,c)/2*r2*r2;
        ans-=sita(c,b,a)/2*r3*r3;
        printf("Case %d: %lf\n",ii,ans);
    }
    return 0;
}

1338  Hidden Secret!

统计字符串,看是否为子串

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    int t;
    cin>>t;
    getchar();
    for(int ii=1;ii<=t;ii++)
    {
        int a[26];
        int tag=1;
        memset(a,0,sizeof(a));
        char c;
        while(scanf("%c",&c),c!='\n')
        {
            if(c==' ') continue;
            if(c>='A'&&c<='Z') a[c-'A']++;
            if(c>='a'&&c<='z') a[c-'a']++;
        }
        while(scanf("%c",&c),c!='\n')
        {
            if(c==' '||tag==0) continue;
            if(c>='A'&&c<='Z')
            {
                if(a[c-'A']!=0)
                    a[c-'A']--;
                else
                    tag=0;
            }
            else if(c>='a'&&c<='z')
            {
                if(a[c-'a']!=0)
                    a[c-'a']--;
                else
                    tag=0;
            }
        }
        cout<<"Case "<<ii<<": ";
        if(tag)
            cout<<"Yes\n";
        else
            cout<<"No\n";
    }
    return 0;
}

1354  IP Checking

进制转换

#include <cstdio>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
int a[5];
char b[5][8];
int fun(stack<int>s)
{
    int ans=0;
    int dd=1;
    while(s.size())
    {
        ans+=dd*s.top();
        s.pop();
        dd*=2;
    }
    return ans;
}
int main()
{
    int t;
    int tmp;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        stack<int>s[5];
        scanf("%d.%d.%d.%d",&a[1],&a[2],&a[3],&a[4]);
        for(int i=1;i<=4;i++)
        {
            for(int j=1;j<=8;j++)
            {
                scanf("%1d",&tmp);
                s[i].push(tmp);
            }
            if(i!=4)
            scanf("%*c");
        }
        int tag=1;
        for(int i=1;i<=4;i++)
        {
            if(fun(s[i])!=a[i])
            {
                tag=0;
                break;
            }
        }
        printf("Case %d: ",ii);
        if(tag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

1387  Setu

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
char s[10];
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        int ans=0,num;
        int n;
        printf("Case %d:\n",i);
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",s);
            if(s[0]=='d')
            {
                scanf("%d",&num);
                ans+=num;
            }
            else
                printf("%d\n",ans);
        }
    }
    return 0;
}

1414  February 29

// 直接做会TLE,已证明
//400年一个轮回,处理后再做
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
bool is_leap_year(int y)
{
    if(y%4==0)
    {
        if(y%100==0)
        {
            if(y%400==0)
                return true;
            return false;
        }
        return true;
    }
    return false;
}
int the_month(string a)
{
    if(a=="January")
        return 1;
    if(a=="February")
        return 2;
    if(a=="March")
        return 3;
    if(a=="April")
        return 4;
    if(a=="May")
        return 5;
    if(a=="June")
        return 6;
    if(a=="July")
        return 7;
    if(a=="August")
        return 8;
    if(a=="September")
        return 9;
    if(a=="October")
        return 10;
    if(a=="November")
        return 11;
    if(a=="December")
        return 12;
    else
        return 0;
}
int main()
{
    int ii,t;
    cin>>t;
    for(ii=1;ii<=t;ii++)
    {
        string str1,str2;
        int date1,date2;
        int year1,year2;
        cin>>str1;
        cin>>date1;
        getchar();
        cin>>year1;
        cin>>str2;
        cin>>date2;
        getchar();
        cin>>year2;
        int month1=the_month(str1);
        int month2=the_month(str2);
        int ans=(year2-year1)/400*97;
        year1%=400;
        year2%=400;
        year1+=400;
        year2+=400;
        if(year1>year2)
            year2+=400;
        for(int i=year1;i<=year2;i++)
            if(is_leap_year(i))
                ans++;
        if(is_leap_year(year1)&&month1>=3)
            ans--;
        if(is_leap_year(year2))
        {
            if(month2==1)
                ans--;
            if(month2==2&&date2<=28)
                ans--;
        }
        cout<<"Case "<<ii<<": "<<ans<<endl;
    }
    return 0;
}

1433

求弧长

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#define PI (2*acos(0.0))
using namespace std;
struct point
{
    double x,y;
};
double Distance(point a,point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        point A,B,O;
        double a,b,c;
        scanf("%lf%lf%lf%lf%lf%lf",&O.x,&O.y,&A.x,&A.y,&B.x,&B.y);
        a=Distance(O,A);
        b=Distance(O,B);
        c=Distance(A,B);
        double ans=(a+b-c)/(2*sqrt(a*b));
        ans=acos(ans);
        printf("Case %d: %lf\n",ii,ans*sqrt(a));
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值