Week Of Code 27

这个比赛是编程马拉松风格的,参赛者需要在七天时间内完成七道题的挑战,每天解锁一题,难度递增。

这次我做出来四道题,第五题Hard难度是一个超级大模拟,不爱写了

A.Drawing Book

https://www.hackerrank.com/contests/w27/challenges/drawing-book

题目大意:

有一本书,有n页,第一页在右侧,你可以从头翻也可以从尾翻,一次翻一页,问你想看第p页,需要翻几次?

题目分析:

从前面翻是p/2页,从后面翻是(n-p)/2页。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int n,p;
    cin>>n>>p;
    cout<<min((n-p)/2,p/2)<<endl;

}

B.Tailor Shop(排序)

https://www.hackerrank.com/contests/w27/challenges/tailor-shop

题目大意:

Jaime想在裙子上镶n堆扣子,每一堆都有不同的颜色。其中第i堆每个扣子花费p元,这一堆需要花费至少 ai 元,她希望每一堆的个数都不同,且总花销最小。

题目分析:

先计算出每一堆至少需要几枚扣子,排序,然后修正为严格递增的序列即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,p;
int a[100005];
int main(){
    cin>>n>>p;
    for(int i=0;i<n;i++) {
        cin>>a[i];
    }
    vector<int> v;
    for(int i=0;i<n;i++)
        v.push_back(ceil(a[i]*1.0/p));
    sort(v.begin(),v.end());
    for(int i=1;i<n;i++) {
        if(v[i]<=v[i-1])
            v[i]=v[i-1]+1;

    }
    ll sum=0;
    for(int i=0;i<n;i++) {
        sum+=v[i];
    }
    cout<<sum<<endl;
}

C.Hackonacci Matrix Rotations

https://www.hackerrank.com/contests/w27/challenges/hackonacci-matrix-rotations

题目大意:

定义Hackonacci数列如下:
* Hackonacci(n)=Hackonacci(n1)+2Hackonacci(n2)+3Hackonacci(n3)
* Hackonacci(1)=1
* Hackonacci(2)=2
* Hackonacci(3)=3

定义Hackonacci矩阵为一个n*n的矩阵,其中第i行j列(i,j从1开始)为字符X,当且仅当 Hackonacci((ij)2) 是偶数,否则是字符Y。

输入q个角度,问将矩阵旋转这个角度后得到的新矩阵和原矩阵有多少个不同的字符?

题目分析:

首先推导一下Hackonacci数列的奇偶性,发现循环周期是7,而且n不大,所以直接模拟即可,需要细心推导3种旋转情况下新老位置的对应关系。有一个需要注意的就是有可能(ij)^2爆int。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2222][2222];
int n,q;
int main(){
    cin>>n>>q;
    for(ll i=1;i<=n;i++) {//用int会爆!!
        for(ll j=1;j<=n;j++) {
            ll temp = i*j*i*j;
            a[i][j]=(temp%7==1)||(temp%7==3)||(temp%7==6)||(temp%7==0);
        }
    }
    int ans1=0,ans2=0,ans3=0;//90 180 270
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            if(a[n-j+1][i]!=a[i][j])
                ans1++;
            if(a[n-i+1][n-j+1]!=a[i][j])
                ans2++;
            if(a[j][n-i+1]!=a[i][j])
                ans3++;
        }
    }
    while(q--) {
        int x;
        cin>>x;
        if(x%360==90)
            cout<<ans1<<endl;
        else if(x%360==180)
            cout<<ans2<<endl;
        else if(x%360==270)
            cout<<ans3<<endl;
        else
            cout<<0<<endl;
    }
}

D.Zero-Move Nim(变种Nim游戏)

https://www.hackerrank.com/contests/w27/challenges/zero-move-nim

题目大意:

在普通Nim游戏的基础上,增加一个操作,每一堆都有一次机会拿0个(注意不是每个人),给出每一堆的数量,问先手胜还是后手胜?

题目分析:

设普通Nim游戏的sg函数为 nim(x) ,易证 nim(x)=x .

接下来推导此变种游戏的sg(x):

sg(0)=0 ,显然先手必败。

sg(1)=mex{sg(0),nim(1)}=mex{0,1}=2.

sg(2)=mex{sg(0),sg(1),nim(2)}=mex{0,2}=1.

以此类推我们得知,n为偶数时 sg(x)=x1 ,n为奇数时 sg(x)=x+1 .(这个应该用数学归纳法可以证明)。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int g,p;
    cin>>g;
    while(g--) {
        int sg=0,t;
        cin>>t;
        while(t--) {
            cin>>p;
            if(p&1)
                sg^=p+1;
            else
                sg^=p-1;
        }
        if(sg)
            cout<<"W"<<endl;
        else
            cout<<"L"<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I don't have the capability to provide you with the exact code for analyzing the product demand in different time periods. However, I can provide you with some guidelines that can help you get started with your analysis. 1. Import the necessary libraries: You'll need to import the pandas library to read and manipulate the CSV file and the matplotlib library to plot the data. 2. Load the CSV file: Use the pandas read_csv() function to load the CSV file into a pandas dataframe. 3. Convert the date column: If the date column is not in a datetime format, use the pandas to_datetime() function to convert it to a datetime format. 4. Group the data by time period: Use the pandas groupby() function to group the data by the desired time period. For example, if you want to group the data by month, use the groupby() function with the resample() function and specify the frequency as 'M'. 5. Calculate the demand: Use the pandas sum() function to calculate the demand for each time period. 6. Plot the data: Use the matplotlib library to plot the demand data over time. Here's some example code to get you started: ``` import pandas as pd import matplotlib.pyplot as plt # Load the CSV file df = pd.read_csv('product_demand.csv') # Convert the date column to datetime format df['Date'] = pd.to_datetime(df['Date']) # Group the data by month grouped_data = df.groupby(pd.Grouper(key='Date', freq='M')) # Calculate the demand for each month demand_data = grouped_data['Demand'].sum() # Plot the demand data over time demand_data.plot() plt.show() ``` This code will group the data by month and plot the demand data over time. You can modify the code to group the data by other time periods such as week, quarter, or year, depending on your needs.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值