简单模拟题

简单模拟

这类题目不涉及算法,完全只是根据题目描述来进行代码的编写,所以考察的是代码能力。

  1. 害死人不偿命
#include 
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
    int n;
    scanf("%d", &n);
    int cnt = 0;
    while(n != 1) {
        if(n % 2 == 0) n /= 2;
        else n = (3*n + 1) / 2;
        cnt++;
    }
    printf("%d", cnt);
    return 0;
}
  1. 挖掘机哪家强
#include 
#include 
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
    int n;
    scanf("%d", &n);
    map schools;
    int maxid;
    int maxscore = -1;
    for(int i = 0; i < n; i++) {
        int id, score;
        scanf("%d%d", &id, &score);
        schools[id] += score;
        if(schools[id] > maxscore) {
            maxid = id;
            maxscore = schools[id];
        }
    }
    printf("%d %d", maxid, maxscore);
    return 0;
}

课后习题

A. 剩下的树

#include 

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool road[10001];
int main() {
    int L, M;
    
    while(scanf("%d%d", &L, &M)) {
        for(int i = 0; i <= L; i++) {
            road[i] = true;
        }
        if(L == 0&& M == 0) break;
        for(int i = 0; i < M; i++) {
            int start, end;
            scanf("%d%d", &start, &end);
            for(int j = start; j <= end; j++) {
                if(road[j]) road[j] = false;
            }
        }
        int cnt = 0;
        for(int i = 0; i <= L; i++) {
            if(road[i]) cnt++;
        }
        printf("%d\n", cnt);
    }
    return 0;
}

B. A+B

#include 
#include 
using namespace std;
int strTnum(string str1) {
    int num1 = 0;
    if(str1[0] == '-') {
        for(int i = 1; i < str1.size(); i++) {
            num1 = num1*10 + (str1[i] - '0');
        }
        num1 = 0 - num1;
    }
    else {
        for(int i = 0; i < str1.size(); i++) {
            num1 = num1*10 + (str1[i] - '0');
        }
    }
    return num1;
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool road[10001];
int main() {

    string str;
    
    while(getline(cin, str)) {
        string word;
        string str1, str2;
        for(int i = 0; i < str.size(); i++) {
            if(str[i] == ' ') {
                str1 = word;
                word = "";
            }
            else if(str[i] != ',') {
                word += str[i];
            }
        }
        str2 = word;
        
        // convert to number
        int num1 = strTnum(str1);
        int num2 = strTnum(str2);
        printf("%d\n", num1+num2);
    }
    
    return 0;
}
  • 特殊乘法
#include 
#include 
using namespace std;

int speciMul(string num1, string num2) {
    int res = 0;
    for(int i = 0; i < num1.size(); i++) {
        for(int j = 0;j < num2.size(); j++) {
            res += (num1[i] - '0') * (num2[j] - '0');
        }
    }
    return res;
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {

    string num1, num2;
    while(cin >> num1 >> num2) {
        int res = speciMul(num1, num2);
        printf("%d\n", res);
    }
    
    return 0;
}
  • 比较奇偶数个数
#include 
#include 
using namespace std;

bool isEven(int num) {
    return num % 2 == 0;
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {

    int n;
    while(scanf("%d", &n) != EOF) {
        int numOfOdd = 0, numOfEven = 0;
        for(int i = 0; i < n; i++) {
            int num;
            scanf("%d", &num);
            if(isEven(num)) numOfEven++;
            else numOfOdd++;
        }
        if(numOfEven > numOfOdd) printf("%s\n", "NO");
        else printf("%s\n", "YES");
    }
    
    
    return 0;
}
  • Shortest Distance
#include 
#include 
#include 
using namespace std;

int roads[100001];
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {

    int n;
    while(scanf("%d", &n) != EOF) {
        int total = 0;
        for(int i = 1; i <= n; i++) {
            int num;
            scanf("%d", &num);
            roads[i] = num;
            total += num;
        }
        
        int m;
        scanf("%d", &m);
        for(int i = 0; i < m; i++) {
            int start, end;
            scanf("%d%d", &start, &end);
            if(start > end) swap(start, end);
            int local = 0;
            for(int j = start; j < end; j++) {
                local += roads[j];  
            }
            int othlocal = total - local;
            int res = othlocal > local? local: othlocal;
            printf("%d\n", res);
        }
    }
    
    
    return 0;
}
  • A+B和C
#include 
#include 
#include 
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {

    int n;
    
    while(scanf("%d", &n) != EOF) {
        int cnt = 1;
        for(int i = 0; i < n; i++) {
            int A, B, C;
            scanf("%d%d%d", &A, &B, &C);
            long res = (long)A + (long)B;
            if(res > C) printf("Case #%d: true\n", cnt++);
            else printf("Case #%d: false\n", cnt++);
        }
        
    }
    
    
    return 0;
}
  • 数字分类
#include 
#include 
#include 
#include 
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {

    int n;
    int num;
    while(scanf("%d",&n)!=EOF){
        int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0;
        int  five_one = 0;
        int cntA4 = 0;
        for(int i = 0; i < n; i++) {
            
            scanf("%d", &num);
            if(num % 5 == 0 && num % 2 == 0) A1 += num;
            else if(num % 5 == 1) {
                if(five_one % 2 == 0) A2 += num;
                else A2 -= num;
                // A2 += (int)pow(-1, five_one) * num;
                five_one ++;
            }
            else if(num % 5 == 2) A3++;
            else if(num % 5 == 3) {
                cntA4++;
                A4 += num;
                
            }
            else if(num%5 == 4){
                if(num > A5) A5 = num;
            }
        }
        if(A1 == 0) printf("N ");
        else printf("%d ", A1);
        if(five_one == 0) printf("N ");
        else printf("%d ", A2);
        if(A3 == 0) printf("N ");
        else printf("%d ", A3);
        if(cntA4 == 0) printf("N ");
        else {
            double fA4 = A4 == 0? 0: double(A4)/cntA4;
            printf("%.1f ", fA4);
        }
        if(A5 == 0) printf("N\n");
        else printf("%d\n", A5);
    }
    
    return 0;
}
  • 部分A+B
#include 
#include 
#include 
#include 
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
long strTl(string str, int n) {
    long res = 0;
    for(int i = 0; i < str.size(); i++) {
        if(str[i] - '0' == n) res = res*10 + n;
    }
    return res;
}

int main() {

    // 假设数字大小不超过2^31
    string A, B;
    int Da, Db;
    cin >> A >> Da >> B >> Db;
    long Pa, Pb;
    Pa = strTl(A, Da);
    Pb = strTl(B, Db);
    cout<
  • 锤子剪刀布
    • 由于scanf使用%c时会将换行符\n读入,因此需要在合适的地方用getchar吸收空格
#include 
#include 
#include 
#include 
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int judge(char formmer, char latter) {
    // 0 means equal, 1 means the formmer wins, 2 means the latter wins 
    if(formmer == 'C') {
        switch(latter) {
            case 'C':
                return 0;
                break;
            case 'J':
                return 1;
                break;
            case 'B':
                return 2;
                break;
        }
    }
    else if(formmer == 'J') {
        switch(latter) {
            case 'C':
                return 2;
                break;
            case 'J':
                return 0;
                break;
            case 'B':
                return 1;
                break;
        }
    }
    else if(formmer == 'B') {
        switch(latter) {
            case 'C':
                return 1;
                break;
            case 'J':
                return 2;
                break;
            case 'B':
                return 0;
                break;
        }
    }
    else return 3; // means error
}

int main() {
    int N;
    int C_1 = 0, J_1 = 0, B_1 = 0, C_2 = 0, J_2 = 0, B_2 = 0;
    int win = 0, fail = 0, equal = 0;
    scanf("%d", &N);
    for(int i = 0; i < N; i++) {
        getchar();
        char formmer, latter;
        scanf("%c %c", &formmer, &latter);
        int sign = judge(formmer, latter);
        if(sign == 0)  equal++;
        else if(sign == 1) {
            win++;
            if(formmer == 'C') C_1++;
            else if(formmer == 'J') J_1++;
            else if(formmer == 'B') B_1++;
        }
        else if(sign == 2) {
            fail++;
            if(latter == 'C') C_2++;
            else if(latter == 'J') J_2++;
            else if(latter == 'B') B_2++;
        }
    }
    printf("%d %d %d\n", win, equal, fail);
    printf("%d %d %d\n", fail, equal, win);
    int max1 = 0;
    char c1;
    if(max1 < B_1) c1 = 'B', max1 = B_1;
    if(max1 < C_1) c1 = 'C', max1 = C_1;
    if(max1 < J_1) c1 = 'J', max1 = J_1;
    int max2 = 0;
    char c2;
    if(max2 < B_2) c2 = 'B', max2 = B_2;
    if(max2 < C_2) c2 = 'C', max2 = C_2;
    if(max2 < J_2) c2 = 'J', max2 = J_2;
    printf("%c %c\n", c1, c2);
    return 0;
}

你可能感兴趣的:(简单模拟题)