1. 程式人生 > 實用技巧 >ACM實驗室2020.10.17 天梯賽三

ACM實驗室2020.10.17 天梯賽三

7-1 判斷素數 (10分)

做法:模子一卡完事

程式碼:

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
// ###### ###### #include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<string> #include<map> #include<sstream> #include<cstring> #include<vector> #include<iomanip> #include<queue> #include<set> #define
LL long long #define _64 __int64 const double PI = atan(1.)*4.; using namespace std; int prime(LL n){ if(n==1){ return 0; } int i=floor(sqrt(n)); int k=2; while(k<=i&&n%k!=0) k++; if(k>i) return 1; else return 0; } int main(){ int
n; cin >> n; while(n--){ LL x; cin >> x; int flag; flag = prime(x); if(flag == 1){ cout << "Yes" << endl; }else{ cout << "No" << endl; } } }

7-2 尋找250 (10分)

做法:PASS

程式碼:

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int main(){
    string s;
    int flag = 0;
    int countn = 0;
    while(1){

        cin >> s;
        countn++;

        if(s == "250"){
            flag = 1;
            break;
        }else{
            continue;
        }
    }
    if(flag == 1){
        cout << countn << endl;
    }else{
        return 0;
    }
}

7-3 幸運彩票 (15分)

做法:把數字當作char型別輸入就ok

程式碼:

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;
char a[10];

int main(){
    int N;
    cin >> N;
    while(N--){
        for(int i = 0;i < 6;i++){
            cin >> a[i];
        }
        if((a[0] + a[1] +a[2])==(a[3] + a[4] + a[5])){
            cout << "You are lucky!" << endl;
        }else{
            cout << "Wish you good luck." << endl;
        }
    }
}

7-4 出生年 (15分)

做法:先把一年的四個數字分別存到數組裡,之後一年一年的迴圈判斷,碰到合適的就跳出

程式碼:

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int main(){
    int y,n;
    cin >> y >> n;
    int xxx;
    int countn = 0;
    while(1){

        int num = y+countn;
        int flag = 0;

        int year[10] = {0};
        int a,b,c,d;
        a = num / 1000;
        b = num /100%10;
        c = num/10%10;
        d = num%10;
        year[a]++;
        year[b]++;
        year[c]++;
        year[d]++;
        if(year[a]==4||year[b]==4||year[c]==4||year[d]==4)
        flag =  1;

    else if(year[a]==3||year[b]==3||year[c]==3||year[d]==3)
        flag =  2;

    else if(year[a]==1&&year[b]==1&&year[c]==1&&year[d]==1)
        flag =  4;

    else if(year[a]==1||year[b]==1||year[c]==1||year[d]==1)
        flag =  3;
    else
        flag =  2;
        if(flag == n){
                xxx = countn;
            break;
        }else{
            countn++;
        }

    }
    //cout << xxx<<" "<<y+xxx<< endl;
    printf("%d %04d\n",xxx,y+xxx);
}

7-5 敲笨鍾 (20分)

做法:首先得判斷這個句子是押韻的,找到“,”和“.”,往前找ong,之後只要找到了,就從尾往前統計空格,把第三個空格記錄下來,輸出”qiao ben zhong.“即可

程式碼:

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int main(){
    int n;
    cin >> n;
    string s;
    int len,flaga,flagb;
    int vis;

    getchar();
    while(n--){
        flaga = 0;
        flagb = 0;
        len = 0;
        int con = 0;
        getline(cin,s);
        len = s.length();
        for(int j = 0;j < len;j++){
            if(s[j] == ',' && s[j-1] == 'g' && s[j-2] == 'n' && s[j-3] == 'o'){
                flaga = 1;
            }
            if(s[j] == '.' && s[j-1] == 'g' && s[j-2] == 'n' && s[j-3] == 'o'){
                flagb = 1;
            }
        }
        if(flaga == 1 && flagb == 1){
            for(int i = len-1;i >= 0;i--){
                if(s[i] == ' '){
                    con++;
                }
                if(con == 3){
                    vis = i;
                    break;
                }
            }

            for(int j = 0;j <= vis;j++){
                cout << s[j];
            }
            cout << "qiao ben zhong." << endl;
        }else{
            cout << "Skipped" << endl;
        }
    }
}

7-6 日期格式化 (5分)

做法:用string存資料,之後迴圈的時候控制一下下標輸出就行

程式碼:

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;



int main(){
    string s;
    cin >> s;
    for(int i = 6;i <= 9;i++){
        cout << s[i];
    }
    cout << "-";
    for(int i = 0;i <= 1;i++){
        cout << s[i];
    }
    cout << "-";
    for(int i = 3;i <= 4;i++){
        cout << s[i] ;
    }
    cout << endl;

}

7-7 A-B (20分)

做法:用char陣列存資料,以第一個字串的長度為外迴圈,內部迴圈如果字元相等就跳過,一直迴圈就行

程式碼:

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
//#include<iostream>
//#include<algorithm>
#include<stdio.h>
#include<string.h>
//#include<cmath>
/*#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
//using namespace std;
*/

int main(){
    char a[200005];
    char b[200005];
    gets(a);
    gets(b);
    int len = strlen(b);
    int i = 0,j = 0;
    while(a[j] != '\0'){
            int flag = 0;
        for(i = 0;i < len;i++){
            if(b[i] == a[j]){
                flag = 1;
                break;
            }
        }
        if(flag != 1){
            printf("%c",a[j]);
        }
        j++;

    }
    printf("\n");
    //cout << a << endl;
}

7-8 計算指數 (5分)

全部PASS

7-9 小字輩 (25分)

做法:我是直接暴力的,因為苦思冥想也想不出什麼好辦法,具體的想法就是每個人遍歷找他的父輩是誰,一直找到-1或者找不到為止,比較大小,但是有三個樣例點超時,優化的想法想不出,網上程式碼看不懂也不是很想看,先暫定這樣吧

程式碼:(超時)

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int main(){
   // ios::sync_with_stdio(false);
    map<int,int>mp;
    int beifen[200005];
    int N;
    scanf("%d",&N);
    int x;
    int c = 1;
    for(int i = 1;i <=N;i++){

        scanf("%d",&x);

        mp[c] = x;
        c++;
    }
    //for(int i = 0;i <=N;i++){
    //    cout << mp[i] << endl;
    //}
    int minn = -5;
    //int ans = -5;32
   // int a[200005];
    for(int j = 1;j <= N;j++){
        int a,b = j;
        int cnt = 1;

        while(mp[b] != -1 || mp[b] == 0){
            a = b;
            b = mp[a];
            cnt++;
        }
        minn = max(cnt,minn);
        beifen[j] = cnt;
    }
    printf("%d\n",minn);
    int a[200005];
    int l = 0;
    for(int i = 1;i <=N;i++){
        if(beifen[i]==minn){
            a[l] = i;
            l++;
        }
    }
    sort(a,a+l);
    for(int i = 0;i <l;i++){
        if(i == 0){
            printf("%d",a[i]);
        }else if( i != 0){
            printf(" %d",a[i]);
        }
    }
    printf("\n");
}

7-10 搶紅包 (25分)

做法:用結構體排序做就很好做了,兩個迴圈存資料順便求答案,再用sort一排輸出即可,具體還是看程式碼吧

程式碼:

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

struct x{
    int id;
    int mon;
    int num;
}a[20005];

int sum[20005];

bool cmp(x a,x b){
    if(a.mon != b.mon){
        return a.mon > b.mon;
    }
    if(a.num != b.num){
        return a.num > b.num;
    }
    return a.id < b.id;
}

int main(){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++){
        a[i].id = i;
        a[i].mon = 0;
        a[i].num = 0;
    }
    for(int i = 1;i <= n;i++){
        int K;
        cin >> K;
        for(int j = 1;j <= K;j++){
            int d,p;
            cin >> d >> p;
            a[d].mon += p;
            a[d].num++;
            sum[i] += p;
        }
    }

    for(int i = 1;i <= n;i++){
        a[i].mon -= sum[i];
    }
    sort(a+1,a+1+n,cmp);
    for(int i = 1;i <= n;i++){
        double ans = 1.0*a[i].mon/100;
        printf("%d %.2f\n",a[i].id,ans);
    }
}

7-11 互評成績 (25分)

做法:這個也不難,看程式碼

程式碼

//去吧馬里奧!把AC公主救回來!
//        ********
//       ************
//       ####....#.
//     #..###.....##....
//     ###.......######
//        ...........
//       ##*#######
//    ####*******######
//   ...#***.****.*###....
//   ....**********##.....
//   ....****    *****....
//     ####        ####
//   ######        ######
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<sstream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<queue>
#include<set>
#define LL long long
#define _64 __int64
const double PI = atan(1.)*4.;
using namespace std;

int main(){
    int N,K,M;
    cin >> N >> K >> M;
    double g[20005];
    int cnt = 0;

    double a[20];
    while(N--){

        for(int i = 0;i < K;i++){
            cin >> a[i];
        }
        sort(a,a+K);
        double sum = 0;
        for(int i = 1;i < K-1;i++){
            sum += a[i];
        }
        double ans = 0;
        ans = sum / (K-2);
        g[cnt] = ans;
        cnt++;
    }
    sort(g,g+cnt);
//for(int i = 0;i < cnt;i++){
 //   printf("%.3f\n",g[i]);
//}
    for(int i = cnt-M;i <cnt;i++){
        if(i == cnt-M){
            printf("%.3f",g[i]);
        }else{
            printf(" %.3f",g[i]);
        }
    }
    cout << endl;
}