Summer day 1

今天上午没什么事,布置了三道输入输出水题,一个月的复习之后成功对字符串读入陌生。
第三题老是TLE,暴力之后图简单想减枝过,然而GG,后推导公式一层循环过了。如下。
HDU-2058

Problem Description
Given a sequence 1,2,3,……N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.

Input
Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.

Output
For each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case.

Sample Input
20 10
50 30
0 0

Sample Output
[1,4]
[10,10]

[4,8]
[6,9]
[9,11]
[30,30]

暴力不过就推公式,另所求区间为[ i, i+len ],则可用m和len来推出i,判断i是否为整。
满足输出,len的枚举从大开始。

#include<cstdio>
#include<cmath>

int main()
{
    int n, m;
    while(scanf("%d%d",&n, &m)!=EOF)
    {
        if(n+m == 0)
            break;
        int len = sqrt(2*m)+5;
        if(len>n+1)
            len = n+1;
        for(; len>=0; len--)
        {
            double temp =double(m)/(len+1)-double(len)/2;
            if(temp>0 && temp+len<=n && temp == floor(temp))
            {
                int ans = floor(temp);
                printf("[%d,%d]\n",ans, ans+len);
            }
        }
        printf("\n");
    }
    return 0;
}

接下来定时赛,两道较为复杂的模拟,黑白棋和象棋(Uva220,Uva201)均没写出,其中黑白棋源码L和M有问题,代码冗长不贴了。

接下来练习一些水题。

Uva1399
有关字母表映射,题目提取出来后知,只需要重复次数相同的字母数目也相同即可,也即分别用cnt1,cnt2 记录string的各字母出现次数,将cnt数组排序比较即可。如下。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    char a[105], b[105];
    while(scanf("%s%s",a,b)!=EOF)
    {
        int cnt1[26], cnt2[26];
        memset(cnt1,0,sizeof(cnt1));
        memset(cnt2,0,sizeof(cnt2));
        int len = strlen(a);      
        for(char i = 'A'; i<='Z'; i++)
        {
            for(int j = 0; j<len; j++)
            {
                if(i == a[j])
                    cnt1[i-'A']++;
            }
        }

        for(char i = 'A'; i<='Z'; i++)
        {
            for(int j = 0; j<len; j++)
            {
                if(i == b[j])
                    cnt2[i-'A']++;
            }
        }
        sort(cnt1, cnt1+26);
        sort(cnt2, cnt2+26);

        int flag = 1;
        for(int i = 0; i<26; i++)
        {
            if(cnt1[i] == cnt2[i])
                continue;
            else
            {
                flag = 0;
                break;
            }
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

以及吊死鬼题目

Uva489
思路:用table记录答案各字母出现次数,将猜的内容进行比对即可,此题有坑:重复猜不算错,即猜过a之后再猜a不算加一笔。如下。并不需要那些复杂的状态记录。

#include<cstdio>
#include<cstring>

int main()
{
    int table[27], rnd;
    char a[26], b[26];
    while(scanf("%d",&rnd) == 1 && rnd != -1)
    {
        scanf("%s%s",a,b);
        memset(table, 0, sizeof(table));
        int len = strlen(a);
        for(int i = 0; i<len; i++)
        {
            table[a[i]-'a']++;
        }
        int cnt = 0;
        for(int i = 0; i<strlen(b); i++)
        {
            if(table[b[i]-'a'] == 0)
            {
                cnt++;
                table[b[i]-'a'] = -1;
            }
            if(table[b[i]-'a'] > 0)
            {
                len -= table[b[i]-'a'];
                table[b[i]-'a'] = -1;
            }
            if(cnt >= 7)
            {
                printf("Round %d\nYou lose.\n",rnd);
                goto bkpt;
            }
            if(len == 0)
            {
                printf("Round %d\nYou win.\n",rnd);
                goto bkpt;
            }
        }      
            printf("Round %d\nYou chickened out.\n",rnd);
        bkpt:;
    }
    return 0;
}

今天并不顺,因为有些生疏了,几天的通宵之后智商也有所衰减。
PS 写了黑白棋200+line,发现智商真的捉急。
以上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于您的问题,我会一一回答: 1. 读入 Bike_share.xlsx 文件,并命名为 bike_share。 ```R library(readxl) bike_share <- read_excel("Bike_share.xlsx") ``` 2. 将 hour 变量因子化,并绘制箱线图比较各小时的总租车量。 ```R bike_share$hour <- factor(bike_share$hour) boxplot(count ~ hour, data = bike_share) ``` 3. 增加一列 season,season 将 month 分为 4 组:1-3 月为"spring", 4-6 月为"summer",7-9 月为"autumn",10-12 月为"winter",将其 保存为 bike_share2,然后统计各季节的平均租车量。 ```R bike_share2 <- bike_share bike_share2$season <- cut(bike_share2$month, breaks = c(0,3,6,9,12), labels = c("spring", "summer", "autumn", "winter"), include.lowest = TRUE) aggregate(count ~ season, data = bike_share2, mean) ``` 4. 绘制柱状图查看各小时租车量中各季节的占比,设置 position 参数使得各柱子长度均相等。 ```R library(ggplot2) ggplot(bike_share2, aes(x = hour, fill = season)) + geom_bar(position = "fill") + scale_y_continuous(labels = scales::percent) + labs(title = "Hourly bike rentals by season", x = "Hour", y = "Percentage") ``` 5. 绘制柱状图统计各小时的平均租车量,将工作日和非工作日分面,分面呈现为 2 行。 ```R bike_share$day_type <- ifelse(bike_share$workingday == 1, "Workday", "Weekend/Holiday") ggplot(bike_share, aes(x = hour, y = count, fill = day_type)) + geom_col(position = "dodge") + facet_wrap(~day_type, nrow = 2) + labs(title = "Hourly bike rentals by day type", x = "Hour", y = "Average bike rentals") ``` 6. 将 date 变量转换成标准日期格式,然后按日期加总租车量,并绘制出各天租车量的曲线图。 ```R bike_share$date <- as.Date(bike_share$date, format = "%Y-%m-%d") daily_rentals <- aggregate(count ~ date, data = bike_share, sum) ggplot(daily_rentals, aes(x = date, y = count)) + geom_line() + labs(title = "Daily bike rentals", x = "Date", y = "Total bike rentals") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值