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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章