Week11作业必做题1-4(签到题)

必做题11-1

问题描述

蒜头君从现在开始工作,年薪 N 万。他希望在蒜厂附近买一套 60 平米的房子,现在价格是 200 万。假设房子价格以每年百分之 K 增长,并且蒜头君未来年薪不变,且不吃不喝,不用交税,每年所得 N 万全都积攒起来,问第几年能够买下这套房子?(第一年年薪 N 万,房价 200 万)

Input

一行,包含两个正整数 N(10≤N≤50),K(1≤K≤20),中间用单个空格隔开。

Output

如果在第
20
20 年或者之前就能买下这套房子,则输出一个整数
M
M,表示最早需要在第
M
M 年能买下;否则输出"Impossible"。

输出时每行末尾的多余空格,不影响答案正确性

Sample input

50 10

Sample output

8

解题思路

签到题,算数就行了,别忘了开始判断一下如果N>=200直接输出1.

完整代码

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

int n,k;
double money=200.0;
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    n=getint(); k=getint();
    if(n>=200){
        printf("1\n");
        return 0;
    }
    for (int i=2; i<=20; i++){
        money=money*(1+k/100.0);
        if(i*n>=money){
            printf("%d\n",i);
            return 0;
        }
    }
    printf("Impossible\n");
    return 0;
}

必做题11-2

问题描述

蒜头君的班级里有 n*n 个同学,现在全班同学已经排列成一个 n∗n 的方阵,但是老师却临时给出了一组新的列队方案

为了方便列队,所以老师只关注这个方阵中同学的性别,不看具体的人是谁

这里我们用 0 表示男生,用 1 表示女生

现在蒜头君告诉你同学们已经排好的方阵是什么样的,再告诉你老师希望的方阵是什么样的

他想知道同学们已经列好的方阵能否通过顺时针旋转变成老师希望的方阵

不需要旋转则输出 0

顺时针旋转 90° 则输出 1

顺时针旋转 180° 则输出 2

顺时针旋转 270° 则输出 3

若不满足以上四种情况则输出 −1

若满足多种情况,则输出较小的数字

Input

第一行为一个整数 n

接下来的 n 行同学们已经列好的 01 方阵;

再接下来的 n 行表示老师希望的的 01 方阵。

对于 100% 的数据中,1≤n≤20

输出时每行末尾的多余空格,不影响答案正确性

Output

输出仅有一行,该行只有一个整数,如题所示。

Sample input

4
0 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 0

Sample output

1

解题思路

找规律,自己在纸上画一下顺时针旋转横纵座标如何变化就行了。

完整代码

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn=30;
int n,a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
bool check(){
    for (int i=1; i<=n; i++)
        for (int j=1; j<=n; j++)
            if(a[i][j]!=b[i][j]) return false;
    return true;
}
bool turn(){
    for (int i=1; i<=n; i++)
        for (int j=1; j<=n; j++)
            c[j][n-i+1]=a[i][j];
    for (int i=1; i<=n; i++)
        for (int j=1; j<=n; j++)
            a[i][j]=c[i][j];
    return check();
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    n=getint();
    for (int i=1; i<=n; i++)
        for (int j=1; j<=n; j++)
            a[i][j]=getint();
    for (int i=1; i<=n; i++)
        for (int j=1; j<=n; j++)
            b[i][j]=getint();
    if(check()) printf("0\n");
    else if(turn()) printf("1\n");
    else if(turn()) printf("2\n");
    else if(turn()) printf("3\n");
    else printf("-1\n");
    return 0;
}

必做题11-3

问题描述

Julius Caesar 曾经使用过一种很简单的密码。对于明文中的每个字符,将它用它字母表中后 5 位对应的字符来代替,这样就得到了密文。比如字符’A’用’F’来代替。如下是密文和明文中字符的对应关系。

密文
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

明文
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

你的任务是对给定的密文进行解密得到明文。

你需要注意的是,密文中出现的字母都是大写字母。密文中也包括非字母的字符,对这些字符不用进行解码。

Input

一行,给出密文,密文不为空,而且其中的字符数不超过 200。

Output

输出一行,即密文对应的明文。

输出时每行末尾的多余空格,不影响答案正确性

Sample input

NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX

Sample output

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES

解题思路

将A-Z重复两遍存到一个字符串中,直接输出对应字符-‘A’+26-5位置对应的字符即可。存两遍是为了处理A-E可以和其他使用一种方法处理。

完整代码

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

string s="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
string str;
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    while(cin>>str){
        for (int i=0; i<str.size(); i++){
            if(str[i]>='A' && str[i]<='Z')
                printf("%c",s[str[i]-'A'+26-5]);
            else printf("%c",str[i]);
        }
        printf(" ");
    }
    return 0;
}

必做题11-4

问题描述

东东和他的女朋友(幻想的)去寿司店吃晚餐(在梦中),他发现了一个有趣的事情,这家餐厅提供的 n 个的寿司被连续的放置在桌子上 (有序),东东可以选择一段连续的寿司来吃

东东想吃鳗鱼,但是东妹想吃金枪鱼。核 平 起 见,他们想选择一段连续的寿司(这段寿司必须满足金枪鱼的数量等于鳗鱼的数量,且前一半全是一种,后一半全是另外一种)我们用1代表鳗鱼,2代表金枪鱼。

比如,[2,2,2,1,1,1]这段序列是合法的,[1,2,1,2,1,2]是非法的。因为它不满足第二个要求。

东东希望你能帮助他找到最长的一段合法寿司,以便自己能吃饱。

Input

第一行:一个整数n(2≤n≤100000),寿司序列的长度。
第二行:n个整数(每个整数不是1就是2,意义如上所述)

Output

输出:一个整数(代表东东可以选择的最长的一段连续的且合法的寿司)

Sample input & output

Sample input1
7
2 2 2 1 1 2 2
Sample output1
4
Sample input2
6
1 2 1 2 1 2
Sample output2
2
Sample input3
9
2 2 1 1 1 2 2 2 2
Sample output3
6

解题思路

这个题比前三个题稍微复杂了一点,根据题意,我们需要找到最长的"1…12…2"或者"2…21…1"这样的数列。思路是如果当前我们从头开始找到了""1…12…2"这样的数列,记录下1出现的次数和2出现的次数,然后取最小值和ans比,再取最大的ans。取最小值是因为1和2的个数必须一样,和ans取最大值是我们要找到最长的结果。下一次再找到1时,重置1出现的次数,这样后面统计的就是"2…21…1"这种形态,等再一次碰到2,重置2出现的次数,后面就是统计"1…12…2"这种情况。

比如说,对于1 2 1 1 2 2,我们统计了1 2,2 1 1,1 1 2 2这三种情况,最终ans=2,输出结果将ans乘2即可。

完整代码

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

int n,ans,a[3],cnt;
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    n=getint();
    int last=0;//记录上一个数据
    for (int i=1; i<=n; i++){
        int temp=getint();//当前数据
        if(last!=temp) cnt++;
        if(cnt==3) a[temp]=0,cnt=2;//应该重置了
        a[temp]++;
        ans=max(ans,min(a[1],a[2]));
        last=temp;
    }
    printf("%d\n",ans*2);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章