Hackerrank——Week of Code 29

Day of the Programmer

题意:给你年份,输出这一年的第256天的年月日,两种日历计算方法,注意区分

#include <bits/stdc++.h>

using namespace std;

int Mouth(int y,int m) {
    if(m == 1 || m== 3 || m==5 || m== 7 || m==8||m==10||m==12) return 31;
    else if( m == 2) {
        if(y < 1918) {
            if(y%4 ==0) return 29;
            else return 28;
        }
        if(y%400==0|| (y %4 ==0 && y %100!=0)) return 29;
        else return 28;
    }
    return 30;
}

int main(){
    int y;
    scanf("%d",&y);
    int ant = 256;
    if(y == 1918) {
        ant -= 46;
        for(int i = 3;i<=12;i++) {
          if(ant <=Mouth(y,i)) {
              printf("%02d.%02d.%d",ant,i,y);
              break;
          }
          else ant -= Mouth(y,i);

        }
    }
    else {
        for(int i = 1;i<=12;i++) {
          if(ant <=Mouth(y,i)) {
              printf("%02d.%02d.%d",ant,i,y);
              break;
          }
          else ant -= Mouth(y,i);
        }
    }
    return 0;
}

Big Sorting

题意:给你一些数,排序,数字比较大,字符串处理

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6+100;;
vector<string> num[maxn];
int main(){
    std::ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string data;
    int End = 0;
    for(int i = 0 ;i<n;i++) {
        cin>>data;
        num[data.size()].push_back(data);
        int len = data.size();
        End = max(End,len);
    }
    for(int i = 1;i<=End;i++) {
        if(num[i].size()) {
            sort(num[i].begin(),num[i].end());
            for(int j = 0;j<num[i].size();j++) cout<<num[i][j]<<endl;
        }
    }
    return 0;
}

A Circle and a Square

题意:给你一个矩阵,问所给圆和正方形共同覆盖的点。判断点是不是在圆中和是不是在多边形中。模板题

#include <bits/stdc++.h>

using namespace std;

const double eps = 1e-9;

struct Point{
    double x,y;
    Point() {}
    Point(double _x,double _y):x(_x),y(_y) {}
    Point operator + (const Point p) const{
        return Point(p.x+x,p.y+y);
    }
    Point operator / (const double p) const{
        return Point(x/p,y/p);
    }
    Point operator - (const Point p) const{
        return Point(x-p.x,y-p.y);
    }
} square[5];

int _sign(double x){
    return x>eps?1:(x<-eps?2:0);
}

double xmult(Point p1, Point p2, Point p){
    return (p1.x - p.x) * (p2.y - p.y) - (p2.x - p.x)*(p1.y - p.y);
}
int IsPointSquare(Point p){
    int s[3] = {1,1,1};
    for(int i = 0; i < 4 && s[1]|s[2]; i++){
        s[_sign(xmult(square[(i+1)%4], p, square[i]))] = 0;
    }
    return s[1]|s[2] ;
}

Point Rotate(Point p,int op){
    return Point(-p.y * op, p.x * op);
}

double xcir,ycir,r;

bool IsPointCircle(Point p){
    return (xcir - p.x)*(xcir - p.x)+(ycir - p.y) * (ycir - p.y) <= r * r;
}

bool IsPointShape(Point p){
    if(IsPointCircle(p) || IsPointSquare(p)) return true;
    return false;
}
int w,h;
bool vis[110][110];
int main()
{
    scanf("%d %d",&h,&w);
    scanf("%lf %lf %lf",&ycir, &xcir, &r);
    scanf("%lf %lf %lf %lf",&square[0].y, &square[0].x, &square[2].y, &square[2].x);
    Point center = (square[0] + square[2])/2;
    square[1] = Rotate(square[2] - center,-1) + center;
    square[3] = Rotate(square[2] - center, 1) + center;
    for(int i = 0; i < w; i++){
        for(int j = 0; j < h; j++){
            vis[i][j] = IsPointShape(Point(i,j));

        }
    }
    for(int i  = 0; i < w; i++)
    {
        for(int j = 0; j < h; j++)
        {
            if(vis[i][j]) printf("#");
            else printf(".");
        }
        printf("\n");
    }
    return 0;
}

Megaprime Numbers

题意:给你一个区间,问区间中数是素数并且每一位也是素数。小于10的素数只有2,3,5,7。由于 LR109 ,所以意味着我们只需要考虑9位,所以我们从高位到地位进行搜索,当然要考虑当前的搜索状态是不是达到区间的上限或者下限。素数判断用 MillerRabin,加个math.h的头文件,莫名卡超时

#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
LL L,R;
const int S=10;

LL mult_mod(LL a,LL b,LL mod)
{
    a%=mod;
    b%=mod;
    LL ans=0;
    while(b)
    {
        if(b&1)
        {
            ans=ans+a;
            if(ans>=mod)
            ans=ans-mod;
        }
        a=a<<1;
        if(a>=mod) a=a-mod;
        b=b>>1;
    }
    return ans;
}

LL pow_mod(LL a,LL b,LL mod)
{
    LL ans=1;
    a=a%mod;
    while(b)
    {
        if(b&1)
        {
            ans=mult_mod(ans,a,mod);
        }
        a=mult_mod(a,a,mod);
        b=b>>1;
    }
    return ans;
}


bool check(LL a,LL n,LL x,LL t)
{
    LL ret=pow_mod(a,x,n);
    LL last=ret;
    for(int i=1;i<=t;i++)
    {
        ret=mult_mod(ret,ret,n);
        if(ret==1 && last!=1 && last!=n-1) return true;
        last=ret;
    }
    if(ret!=1) return true;
    else return false;
}


bool Miller_Rabin(LL n)
{
    if(n<2)return false;
    if(n==2) return true;
    if( (n&1)==0) return false;
    if(n>=10&&(n%2 == 0||n%3 == 0|| n%5 ==0||n%7==0)) return false;
    LL x=n-1;
    LL t=0;
    while( (x&1)==0 ) { x>>=1;t++;}
    for(int i=0;i<S;i++)
    {
        LL a=rand()%(n-1)+1;
        if(check(a,n,x,t))
        return false;
    }
    return true;
}


int a[20],b[20];
int pri[] = {2,3,5,7};
int ans ;
void GetDidig(LL s,int bite[]) {
    memset(bite,0,sizeof(bite));
    while(s) {
        bite[++bite[0]] = s%10;
        s/=10;
    }
}

int Getnum(int s) {
    if(s<=2) return 0;
    if(s<=3) return 1;
    if(s<=5) return 2;
    if(s<=7) return 3;
    return 4;
}
int Getdown(int s) {
    if(s>=7) return 3;
    if(s>=5) return 2;
    if(s>=3) return 1;
    if(s>=2) return 0;
    return -1;
}
void dfs(int len,LL s,bool limita,bool limitb,bool limit) {
    if(len == 0) {
        ans += Miller_Rabin(s);
        return ;
    }
    int Begin ,end;
    if(limit) {
        if(!limita || (limita && 0>=a[len]))
            dfs(len-1,s*10,1, 0==b[len] && limitb,1);
    }
    if(limita) {
        Begin = Getnum(a[len]);
    }
    else Begin = 0;
    if(limitb) {
        end = Getdown(b[len]);
    }
    else end = 3;
    for(;Begin<=end;Begin++) {
        dfs(len-1,s*10+pri[Begin],pri[Begin] == a[len] && limita,pri[Begin] == b[len] && limitb,0);
    }
    return ;
}

int Solve(LL L,LL R) {
    GetDidig(L,a);
    GetDidig(R,b);
    ans = 0;
    dfs(max(a[0],b[0]),0,1,1,1);
    return ans;
}

int main()
{
    while(~scanf("%lld%lld",&L,&R)){
        printf("%d\n",Solve(L,R));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/huayunhualuo/article/details/59481657
29