zoukankan      html  css  js  c++  java
  • 枚举与模拟

    1.枚举

    KY15 abc

    题目描述

    设a、b、c均是0到9之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值。

    输入描述:

    题目没有任何输入。

    输出描述:

    请输出所有满足题目条件的a、b、c的值。
    a、b、c之间用空格隔开。
    每个输出占一行。

    输入

    输出

    代码

    #include <iostream>
    
    using namespace std;
    
    int main(){
        for(int a = 0; a <= 9; ++a){
            for (int b = 0; b <= 9 ; ++b) {
                for (int c = 0; c <=9 ; ++c) {
                    if (a * 100 + b * 110 + c * 12 ==532 ){
                        printf("%d %d %d\n" , a, b, c);
                    }
                }
    
            }
        }
        return 0;
    }
    

    KY266 反序数

    题目描述

    设N是一个四位数,它的9倍恰好是其反序数(例如:1234的反序数是4321)
    求N的值

    输入描述:

    程序无任何输入数据。

    输出描述:

    输出题目要求的四位数,如果结果有多组,则每组结果之间以回车隔开。

    输入

    输出

    代码

    #include <iostream>
    
    using namespace std;
    
    int Reverse(int x){
        int revx = 0;
        while (x != 0){
            revx *= 10;
            revx += x % 10;
            x /= 10;
        }
        return revx;
    }
    
    int main(){
        for (int i = 1000; i <= 9999 ; ++i) {
            if (i * 9 == Reverse(i)){
                printf("%d\n",i);
            }
        }
        return 0;
    }
    

    KY267 对称平方数1

    题目描述

    打印所有不超过256,其平方具有对称性质的数。如2,11就是这样的数,因为22=4,1111=121。

    输入描述:

    无任何输入数据

    输出描述:

    输出具有题目要求的性质的数。如果输出数据不止一组,各组数据之间以回车隔开。

    输入

    输出

    代码

    #include <iostream>
    
    using namespace std;
    
    int Reverse(int x){
        int revx = 0;
        while (x != 0){
            revx *= 10;
            revx += x % 10;
            x /= 10;
        }
        return revx;
    }
    
    int main(){
        for (int i = 0; i <= 256; ++i) {
            if (i * i == Reverse(i * i)){
                printf("%d\n",i);
            }
        }
        return 0;
    }
    
    

    KY50 与7无关的数

    题目描述

    一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7, 则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和。

    输入描述:

    案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)

    输出描述:

    对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。

    输入

    21
    

    输出

    2336
    

    代码

    #include <iostream>
    
    using namespace std;
    
    bool judge(int n){
        if(n % 7 == 0)
            return false;
        if(n % 10 == 7)
            return false;
        if(n / 10 == 7)
            return false;
        return true;
    }
    
    int main(){
        int n;
        while(cin >> n){
            int ret = 0;
            for(int i = 1; i <= n; i++){
                if(judge(i)){
                    ret = ret + i * i;
                }
            }
            cout << ret << endl;
        }
        return 0;
    

    KY156 百鸡问题

    题目描述

    用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一种小鸡,分别记为x只,y只,z只。编程求解x,y,z所有可能解。

    输入描述:

    ​ 测试数据有多组,输入n。

    输出描述:

    ​ 对于每组输入,请输出x,y,z所有可行解,按照x,y,z依次增大的顺序输出。

    输入

    40
    

    输出

    x=0,y=0,z=100
    x=0,y=1,z=99
    x=0,y=2,z=98
    x=1,y=0,z=99
    

    代码

    #include <iostream>
    
    using namespace std;
    
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            for(int i = 0;i <= 100; ++i){
                for(int j = 0;j <= 100-i; ++j){
                    int k=100-i-j;
                    if(15*i+9*j+1*k<=3*n)
                        printf("x=%d,y=%d,z=%d\n",i,j,k);
                }
            }
        }
    } 
    

    KY95 Old Bill

    题目描述

    Among grandfather's papers a bill was found.

    72 turkeys $_679_

    The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted_ ), for they are faded and are illegible. What are the two faded digits and what was the price of one turkey?

    We want to write a program that solves a general version of the above problem.

    N turkeys $_XYZ_

    The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the turkeys cost the same price.

    Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. In case that there is more than one candidate for the original price, the output should be the most expensive one. That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.

    输入描述:

    The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys. In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $XYZ.

    输出描述:

    For each case, output the two faded digits and the maximum price per turkey for the turkeys.

    输入

    72
    6 7 9
    5
    2 3 7
    78
    0 0 5
    

    输出

    3 2 511
    9 5 18475
    0
    

    代码

    #include<iostream>
    #include<cstring>
    using namespace std;
    int Solve(int N, int X, int Y, int  Z) {
        bool find = false;
        int sum = 0;
        for (int i = 9; i >= 1; i--) {
            for (int j = 9; j >= 0; j--) {
                sum = i * 10000 + X * 1000 + Y * 100 + Z * 10 + j;
                if (sum%N == 0) {
                    find = true;
                    cout << i << " " << j << " " << (sum / N) << endl;
                    return 0;
                }
            }
        }
        if (!find) {
            cout << 0 << endl;
        }
        return 0;
    }
    int main() {
        int N, X, Y, Z;
        while (cin >> N >> X >> Y >> Z) {
            Solve(N, X, Y, Z);
        }
        return 0;
    }
    

    2.模拟

    1. 图形排版

    QH 输出梯形

    题目描述

    输入一个高度h,输出一个高为h,上底边为h的梯形。

    输入:

    一个整数h(1<=h<=1000)。

    输出:

    h所对应的梯形。

    样例输入

    4

    样例输出

    代码

    #include <iostream>
    
    using namespace std;
    
    int main(){
        int h;
        while(scanf("%d", &h) != EOF){
            int row = h;
            int col = h + (h - 1) * 2;
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    if (j < col - (h + 2 * i)) {
                        printf(" ");
                    } else {
                        printf("*");
                    }
                }
                printf("\n");
            }
        }
        return 0;
    }
    
    

    HDU 叠筐

    Problem Description

    需要的时候,就把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。

    Input

    输入是一个个的三元组,分别是,外筐尺寸n(n为满足0<n<80的奇整数),中心花色字符,外筐花色字符,后二者都为ASCII可见字符;

    Output

    输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。

    Sample Input

    11 B A
    5 @ W
    

    Sample Output

     AAAAAAAAA 
    ABBBBBBBBBA
    ABAAAAAAABA
    ABABBBBBABA
    ABABAAABABA
    ABABABABABA
    ABABAAABABA
    ABABBBBBABA
    ABAAAAAAABA
    ABBBBBBBBBA
     AAAAAAAAA 
    
     @@@ 
    @WWW@
    @W@W@
    @WWW@
     @@@ 
    

    代码

    #include <iostream>
    #include <cstdio>
    
    using  namespace std;
    
    char matrix[80][80];
    
    int main(){
        int n;
        char a,b;
        bool firstCase = true;
        while (scanf("%d %c %c", &n,&a,&b) != EOF){
            if(firstCase == true){
                firstCase = false;
            }else{
                printf("\n");
            }
            for (int i = 0; i <= n / 2; ++i) {
                int j = n - i - 1;
                int length = n - 2 * i;
                char c;
                if((n / 2 - i) % 2 == 0){
                    c = a;
                }else{
                    c = b;
                }
                for (int k = 0; k < length; ++k) {
                    matrix[i][i + k] = c;
                    matrix[i + k][i] = c;
                    matrix[j][j - k] = c;
                    matrix[j - k][j] = c;
                }
    
            }
            if( n != 1){
                matrix[0][0] = ' ';
                matrix[0][n-1]= ' ';
                matrix[n-1][0]= ' ';
                matrix[n-1][n-1]= ' ';
            }
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    printf("%c", matrix[i][j]);
                }
                printf("\n");
            }
        }
        return 0;
    }
    

    POJ Repeater

    题目描述

    Harmony is indispensible in our daily life and no one can live without it----may be Facer is the only exception. One day it is rumored that repeat painting will create harmony and then hundreds of people started their endless drawing. Their paintings were based on a small template and a simple method of duplicating. Though Facer can easily imagine the style of the whole picture, but he cannot find the essential harmony. Now you need to help Facer by showing the picture on computer.

    You will be given a template containing only one kind of character and spaces, and the template shows how the endless picture is created----use the characters as basic elements and put them in the right position to form a bigger template, and then repeat and repeat doing that.

    Here is an example.

    输入描述:

    The input contains multiple test cases.
    The first line of each case is an integer N, representing the size of the template is N*N (N could only be 3, 4 or 5).
    Next N lines describe the template.
    The following line contains an integer Q, which is the Scale Level of the picture.
    Input is ended with a case of N=0.
    It is guaranteed that the size of one picture will not exceed 3000*3000.

    输出描述:

    For each test case, just print the Level Q picture by using the given template.

    输入

    3
    # #
     # 
    # #
    1
    3
    # #
     # 
    # #
    3
    4
     OO 
    O  O
    O  O
     OO 
    2
    0
    

    输出

    # #
     # 
    # #
    # #   # #         # #   # #
     #     #           #     # 
    # #   # #         # #   # #
       # #               # #   
        #                 #    
       # #               # #   
    # #   # #         # #   # #
     #     #           #     # 
    # #   # #         # #   # #
             # #   # #         
              #     #          
             # #   # #         
                # #            
                 #             
                # #            
             # #   # #         
              #     #          
             # #   # #         
    # #   # #         # #   # #
     #     #           #     # 
    # #   # #         # #   # #
       # #               # #   
        #                 #    
       # #               # #   
    # #   # #         # #   # #
     #     #           #     # 
    # #   # #         # #   # #
         OO  OO     
        O  OO  O    
        O  OO  O    
         OO  OO     
     OO          OO 
    O  O        O  O
    O  O        O  O
     OO          OO 
     OO          OO 
    O  O        O  O
    O  O        O  O
     OO          OO 
         OO  OO     
        O  OO  O    
        O  OO  O    
         OO  OO     
    

    代码

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <string.h>
    using namespace std;
    
    char map[3003][3003];
    char str[6][6];
    int n;
    
    void dfs(int m, int x, int y){
        if (m == 1){
            for (int i = 0; i<n; i++)
                for (int j = 0; j<n; j++)
                    map[x + i][y + j] = str[i][j];
            return;
        }
        int size = (int)pow(n*1.0, m - 1);
        for (int i = 0; i<n; i++){
            for (int j = 0; j<n; j++){
                if (str[i][j] != ' ')
                    dfs(m - 1, x + i*size, y + j*size);
            }
        }
    }
    
    int main(void){
        n = 1;
        while (n){
            cin >> n;
            getchar();
            for (int i = 0; i < n; i++){
                cin.getline(str[i],6);
            }
            int m;
            cin >> m;
            int size = (int)pow(n*1.0, m);
            for (int i = 0; i<size; i++){
                for (int j = 0; j<size; j++)
                    map[i][j] = ' ';
                map[i][size] = '\0';
            }
            dfs(m, 0, 0);
            for (int i = 0; i<size; i++)
                cout<<map[i]<<endl;
        }
        return 0;
    } 
    

    KY147 Hello World for U

    题目描述

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U.

    For example, "helloworld" can be printed as: h d e l l r lowo

    That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters.

    And more, we would like U to be as squared as possible -- that is, it must be satisfied that $n_1 = n_3 = max { k| k <= n_2 \ for\ all \ 3 \le n_2 \le N } $with n1 + n2 + n3 - 2 = N.

    输入描述:

    There are multiple test cases.Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.

    输出描述:

    For each test case, print the input string in the shape of U as specified in the description.

    输入

    helloworld!
    www.nowcoder.com
    

    输出

    h   !
    e   d
    l   l
    lowor
    w    m
    w    o
    w    c
    .    .
    n    r
    owcode
    

    代码

    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main(){
        string s;
        while(cin>>s){
            int len=s.size();
            len += 2;
            int l = len/3  - 1;//(len+2)/3就是n1和n2的值
            int mid=len-len/3*2;
            for(int i = 0;i < l; ++i){
                cout<<s[i];
                for(int j = 0;j < mid-2;++j){
                    cout<<" ";
                }
                cout<<s[s.size()-1-i]<<endl;
            }
            for(int i = l;i < l+mid; ++i){
                cout<<s[i];
            }
        }
        return 0;
    }
    
    1. 日期问题

    KY19 今年的第几天?

    题目描述

    输入年、月、日,计算该天是本年的第几天。

    输入描述:

    包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。

    输出描述:

    输入可能有多组测试数据,对于每一组测试数据,
    输出一个整数,代表Input中的年、月、日对应本年的第几天。

    输入

    1990 9 20
    2000 5 1
    

    输出

    263
    122
    

    代码

    #include <iostream>
    
    using namespace std;
    
    int daytab[2][13] = {
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    };
    bool IsLeapYear(int year){
        return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0);
    }
    int main(){
        int year, month, day;
        while (scanf("%d %d %d", &year, &month, &day) != EOF){
            int number = 0;
            int row = IsLeapYear(year);
            for (int i = 0; i < month; ++i) {
                number += daytab[row][i];
            }
            number += day;
            printf("%d\n",number);
        }
        return 0;
    }
    

    KY222 打印日期

    题目描述

    给出年份m和一年中的第n天,算出第n天是几月几号。

    输入描述:

    输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

    输出描述:

    可能有多组测试数据,对于每组数据,
    按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

    输入

    2000 3
    2000 31
    2000 40
    2000 60
    2000 61
    2001 60
    

    输出

    2000-01-03
    2000-01-31
    2000-02-09
    2000-02-29
    2000-03-01
    2001-03-01
    

    代码

    #include <iostream>
    
    using namespace std;
    
    int daytab[2][13] = {
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    };
    bool IsLeapYear(int year){
        return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0);
    }
    int main(){
        int year, month, day;
        int number;
        while (scanf("%d %d", &year, &number) != EOF){
            month = 0;
            int row = IsLeapYear(year);
            while (number > daytab[row][month]){
                number -= daytab[row][month];
                month++;
            }
            day = number;
            printf("%04d-%02d-%02d\n", year, month, day);
        }
        return 0;
    }
    

    KY258 日期累加

    题目描述

    设计一个程序能计算一个日期加上若干天后是什么日期。

    输入描述:

    输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。

    输出描述:

    输出m行,每行按yyyy-mm-dd的个数输出。

    输入

    1
    2008 2 3 100
    

    输出

    2008-05-13
    

    代码

    #include <iostream>
    
    using namespace std;
    
    int daytab[2][13] = {
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    };
    bool IsLeapYear(int year){
        return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0);
    }
    
    int NumberofYear(int year){
        if(IsLeapYear(year)){
            return 366;
        } else{
            return 365;
        }
    
    }
    
    int main(){
        int year, month, day;
        int number;
        int caseNumber;
        scanf("%d", &caseNumber);
        while (caseNumber--){
            scanf("%d %d %d %d", &year, &month, &day, &number);
            int row = IsLeapYear(year);
            for (int i = 0; i < month; ++i) {
                number += daytab[row][i];
            }
            number += day;
            while (number > NumberofYear(year)){
                number -= NumberofYear(year);
                year++;
            }
            month = 0;
            row = IsLeapYear(year);
            while (number > daytab[row][month]){
                number -= daytab[row][month];
                month++;
            }
            day = number;
            printf("%04d-%02d-%02d\n", year, month, day);
        }
        return 0;
    }
    

    KY111 日期差值

    题目描述

    有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

    输入描述:

    有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

    输出描述:

    每组数据输出一行,即日期差值

    输入

    20110412
    20110422
    

    输出

    11
    

    代码

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    int daytab[2][13] = {
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    };
    
    bool IsLeapYear(int year){
        return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0);
    }
    
    int NumberofYear(int year){
        if(IsLeapYear(year)){
            return 366;
        } else{
            return 365;
        }
    
    }
    int Date(int year, int month, int day) {
        int dateth = 0;
        for (int i = 0; i < year; i++) {
            dateth += NumberofYear(i);
        }
        for (int i = 0; i < month; i++) {
            int row = IsLeapYear(year);
            dateth += daytab[row][i];
        }
        dateth += day;
        return dateth;
    }
    
    int main(){
        int time1,year1,month1,day1;
        int time2,year2,month2,day2;
    
        while(scanf("%d%d",&time1,&time2)!=EOF){
    
            year1 = time1/10000;
            month1 = time1%10000/100;
            day1 = time1%100;
            year2 = time2/10000;
            month2 = time2%10000/100;
            day2 = time2%100;
    
            printf("%d\n", abs(Date(year1, month1, day1)-Date(year2, month2, day2))+1);
        }
        return 0;
    }
    

    KY108 Day of Week

    题目描述

    We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2005, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

    输入描述:

    There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

    输出描述:

    Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

    Month and Week name in Input/Output:
    January, February, March, April, May, June, July, August, September, October, November, December
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

    输入

    9 October 2001
    14 October 2001
    

    输出

    Tuesday
    Sunday
    

    分析

    • 隐藏条件就是1年1月1日是星期一,把这个时间点设为锚点
    • 计算输入的日期与锚点之间隔了多少天
    • 天数对7取余,所得结果就是星期几

    代码

    /**
     * @author: Qiuyue Zhang
     * @date: 2021/2/2 18:59
     * @description:
     */
    #include <cstdio>
    #include <cstring>
    
    int daytab[2][13] = {
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    };
    
    bool IsLeapYear(int year){
        return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0);
    }
    int NumberofYear(int year){
        if(IsLeapYear(year)){
            return 366;
        } else{
            return 365;
        }
    }
    // 计算从公元1年1月1号到当前的天数
    int Date(int year, int month, int day) {
        int dateth = 0;
        for (int i = 1; i < year; i++) {
            dateth += NumberofYear(i);
        }
        for (int i = 0; i < month; i++) {
            int row = IsLeapYear(year);
            dateth += daytab[row][i];
        }
        dateth += day-1;
        return dateth;
    }
    
    char month_name[13][20]={
            "","January","February","March","April","May","June","July","August",
            "September","October","November","December"
    };
    char week_name[7][20]={
            "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"
    };
    
    int main(){
        int year, month, day;
        char Month[20];
        while(scanf("%d%s%d", &day, &Month, &year)!=EOF){
            for (int i = 1; i <= 12; ++i) {
                if(strcmp(Month,month_name[i])==0){
                    month = i;
                    break;
                }
            }
            printf("%s\n",week_name[(Date(year,month,day))%7]);
        }
        return 0;
    }
    

    KY250 日期类

    题目描述

    编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作。

    输入描述:

    输入第一行表示测试用例的个数m,接下来m行每行有3个用空格隔开的整数,分别表示年月日。测试数据不会有闰年。

    输出描述:

    输出m行。按xxxx-xx-xx的格式输出,表示输入日期的后一天的日期。

    输入

    2
    1999 10 20
    2001 1 31
    

    输出

    1999-10-21
    2001-02-01
    

    备注:

    注意个位数日期前面要有0。

    代码

    #include<iostream>
    #include<cstring>
    using namespace std;
    const int month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int fun(int year, int mon, int day) {
        if (mon == 12 && day == 31) {
            year++;
            mon = day = 1;
        }
        else {
            if (day < month[mon]) {
                day++;
            }
            else {
                mon++; 
                day = 1;
            }
        }
        printf("%04d-%02d-%02d\n", year, mon, day);
        return 0;
    }
    int main() {
        int m, year, mon, day;
        while (cin >> m) {
            for (int i = 0; i < m; i++) {
                cin >> year >> mon >> day;
                fun(year, mon, day);
            }
        }
        return 0;
    }
    
    1. 其他模拟

    KY25 剩下的树

    题目描述

    有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L+1棵树。 现在要移走一些树,移走的树的区间用一对数字表示,如 “100 200” 表示移走从100到200之间(包括端点)所有的树。 可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。

    输入描述:

    两个整数L(1<=L<=10000)和M(1<=M<=100)。
    接下来有M组整数,每组有一对数字。

    输出描述:

    可能有多组输入数据,对于每组输入数据,输出一个数,表示移走所有区间的树之后剩下的树的个数。

    输入

    500 3
    100 200
    150 300
    470 471
    

    输出

    298
    

    分析

    • 用bool型数组模拟这条线段,有树就设为true,没树就设为false。
    • 树的棵数为L+1,每次操作将区间内为true元素设为false。

    代码

    #include <iostream>
    #include <cstdio>
    
    using  namespace std;
    
    const int MAXN = 10001;
    
    bool arr[MAXN];
    
    int main(){
        int l, m;
        while (scanf("%d%d", &l, &m) != EOF){
            for (int i = 0; i <= l; ++i) {
                arr[i] = true;
            }
            int number = l + 1;
            while (m--){
                int left, right;
                scanf("%d%d", &left, &right);
                for (int i = left; i <= right ; ++i) {
                    if(arr[i]){
                        arr[i] = false;
                        number -- ;
                    }
                }
            }
            printf("%d\n",number);
        }
    }
    

    KY6 手机键盘

    题目描述

    按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。 如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下 如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。 现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。 现在给出一串字符,需要计算出它所需要花费的时间。

    输入描述:

    一个长度不大于100的字符串,其中只有手机按键上有的小写字母

    输出描述:

    输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间

    输入

    bob
    www
    

    输出

    7
    7
    

    代码

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        int key[26] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4};
        string str;
        while(cin>>str)
        {
            int count = key[str[0]-'a'];
            for(int i=1;i<str.size();++i)
            {
                count += key[str[i]-'a'];
                if(key[str[i]-'a']-key[str[i-1]-'a']==str[i]-str[i-1])//判断是否在同一个按键上
                    count+=2;
            }
            cout<<cout<<endl;
        }
    }
    

    KY132 XXX定律

    题目描述

    对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。 请计算需要经过几步才能将n变到1,具体可见样例。

    输入描述:

    测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)

    输出描述:

    对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。

    输入

    3
    1
    0
    

    输出

    5
    0
    

    代码

    #include <iostream>
    
    using namespace std;
    
    int main(){
        int n;
        while (scanf("%d", &n) != EOF){
            if(n == 0){
                break;
            }
            int step = 0;
            while (n != 1){
                if(n % 2 == 0){
                    n = n / 2;
                }else{
                    n = (3 * n + 1) / 2;
                }
                step++;
            }
            printf("%d\n",step);
        }
    }
    

    KY120 Grading

    题目描述

    Grading hundreds of thousands of Graduate Entrance Exams is a hard work. It is even harder to design a process to make the results as fair as possible. One way is to assign each exam problem to 3 independent experts. If they do not agree to each other, a judge is invited to make the final decision. Now you are asked to write a program to help this process. For each problem, there is a full-mark P and a tolerance T(<P) given. The grading rules are:

    • A problem will first be assigned to 2 experts, to obtain G1 and G2. If the difference is within the tolerance, that is, if \(|G1 - G2| ≤ T\), this problem's grade will be the average of G1 and G2.
    • If the difference exceeds T, the 3rd expert will give G3.
    • If G3 is within the tolerance with either G1 or G2, but NOT both, then this problem's grade will be the average of G3 and the closest grade.
    • If G3 is within the tolerance with both G1 and G2, then this problem's grade will be the maximum of the three grades.
    • If G3 is within the tolerance with neither G1 nor G2, a judge will give the final grade GJ.

    输入描述:

    Each input file may contain more than one test case.
    Each case occupies a line containing six positive integers: P, T, G1, G2, G3, and GJ, as described in the problem. It is guaranteed that all the grades are valid, that is, in the interval [0, P].

    输出描述:

    For each test case you should output the final grade of the problem in a line. The answer must be accurate to 1 decimal place.

    输入

    20 2 15 13 10 18
    

    输出

    14.0
    

    题目大意

    为成千上万的研究生入学考试评分是一项艰苦的工作。评分尽可能公平的就更难了。一种方法是将每个考试题分配给3个独立的专家。如果双方意见不一致,请审判长作出最后决定。现在你被要求写一个程序来帮助这个过程。对于每个问题,都有一个满分P和一个公差T(<P)。评分规则如下:

    • 一道题将首先分配给两位专家,得到G1和G2。如果差值在公差范围内,即如果\(| G1-G2 |≤T\),则该问题的等级将是G1和G2的平均值。
    • 如果差值超过T,第三位专家将给出G3。
    • 如果G3在G1或G2的公差范围内,但不是两者都在公差范围内,则此问题的等级将是G3和最近等级的平均值。
    • 如果G3在G1和G2的公差范围内,则此问题的等级将是三个等级中的最大等级。
    • 如果G3在公差范围内,既没有G1也没有G2,则法官将给出最终等级GJ。

    代码

    #include<iostream>
    #include<iomanip>
    #include<math.h>
    
    using namespace std;
    
    int main() {
        int p, t;
        int g1, g2, g3, g4;
    
        while (scanf("%d%d%d%d%d%d", &p, &t, &g1, &g2, &g3, &g4) != EOF){
            double score;
            if (abs(g1 - g2) <= t)
                score = (double) (g1 + g2) / 2;
            else {
                if (abs(g3 - g1) <= t && abs(g3 - g2) > t)
                    score = (double) (g3 + g1) / 2;
                else if (abs(g3 - g2) <= t && abs(g3 - g1) > t)
                    score = (double) (g3 + g2) / 2;
                else if (abs(g3 - g2) <= t && abs(g3 - g1) <= t) {
                    int temp = g3 > g2 ? g3 : g2;
                    score = temp > g1 ? temp : g1;
                } else score = g4;
            }
            printf("%0.1f\n", score);
        }
        return 0;
    }
    

    KY100 路径打印

    题目描述

    给你一串路径,譬如: a\b\c a\d\e b\cst d\ 你把这些路径中蕴含的目录结构给画出来,子目录直接列在父目录下面,并比父目录向右缩一格,就像这样: a b c d e b cst d 同一级的需要按字母顺序排列,不能乱。

    输入描述:

    每个测试案例第一行为一个正整数n(n<=10)表示有n个路径,当n为0时,测试结束,接下来有n行,每行有一个字串表示一个路径,长度小于50。

    输出描述:

    输出目录结构,每一个测试样例的输出紧跟一个空行。

    输入

    4
    a\b\c
    a\d\e
    b\cst
    d\
    0
    

    输出

    a
      b
        c
      d
        e
    b
      cst
    d
    

    代码

    #include<iostream>
    #include<string>
    #include<algorithm>
    #include <vector>
    
    using namespace std;
    
    const int MAXN = 11;
    vector <string> vec[MAXN];
    int n;
    string s;
    
    int main() {
        while (cin >> n && n != 0) {
            for (int i = 0; i < n; i++) {
                cin >> s;
                vec[i].clear();//清除上一次的
                vec[i].push_back(s);
            }
            sort(vec, vec + n);
            for (int i = 0; i < n; i++) {//截取 把形如“a\b\c”的字符串分割成[“a”,“b”,“c”]这样的字符串数组
                s = vec[i][0];
                vec[i].clear();
                int j = 0, index;
                while (j < s.size()) {
                    if ((index = s.find('\\', j)) != string::npos) {
                        vec[i].push_back(s.substr(j, index - j));//substr截取函数(从j位开始,截取index-j位)
                        j = index + 1;
                    } else {
                        vec[i].push_back(s.substr(j, s.size() - j));
                        break;
                    }
                }
            }
            for (int i = 0; i < n; i++) {//打印
                if (i == 0) {//第一个直接输出
                    for (int j = 0; j < vec[i].size(); j++) {
                        for (int k = 0; k < j; k++)cout << "  ";
                        cout << vec[i][j] << endl;
                    }
                } else {//找到和前一个第一个不相等的位置
                    int j = 0;
                    while (j < vec[i - 1].size() && j < vec[i].size() && vec[i][j] == vec[i - 1][j])j++;
                    if (j == 0) {//如果第一个字母就不相同,直接输出
                        for (int k = 0; k < vec[i].size(); k++) {
                            for (int l = 0; l < k; l++)cout << "  ";
                            cout << vec[i][k] << endl;
                        }
                    } else {//找到了第一个不相同的位置j,从j开始输出
                        for (int k = j; k < vec[i].size(); k++) {
                            for (int l = 0; l < k; l++)cout << "  ";
                            cout << vec[i][k] << endl;
                        }
                    }
                }
            }
            cout << endl;
        }
        return 0;
    }
    

    KY89 坠落的蚂蚁

    题目描述

    一根长度为1米的木棒上有若干只蚂蚁在爬动。它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右。如果两只蚂蚁碰头,则它们立即交换速度并继续爬动。三只蚂蚁碰头,则两边的蚂蚁交换速度,中间的蚂蚁仍然静止。如果它们爬到了木棒的边缘(0或100厘米处)则会从木棒上坠落下去。在某一时刻蚂蚁的位置各不相同且均在整数厘米处(即1,2,3,…99厘米),有且只有一只蚂蚁A速度为0,其他蚂蚁均在向左或向右爬动。给出该时刻木棒上的所有蚂蚁位置和初始速度,找出蚂蚁A从此时刻到坠落所需要的时间。

    输入描述:

    第一行包含一个整数表示蚂蚁的个数N(2<=N<=99),之后共有N行,每一行描述一只蚂蚁的初始状态。每个初始状态由两个整数组成,中间用空格隔开,第一个数字表示初始位置厘米数P(1<=P<=99),第二个数字表示初始方向,-1表示向左,1表示向右,0表示静止。

    输出描述:

    蚂蚁A从开始到坠落的时间。若不会坠落,输出“Cannot fall!”

    输入

    4
    10 1
    90 0
    95 -1
    98 -1
    

    输出

    98
    

    代码

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    struct Ant {
        int position;
        int direct;    //方向
        bool operator<(const Ant &a) const {
            return position < a.position;
        }
    };
    
    int main() {
        int n;
        while (scanf("%d", &n) != EOF) {
            vector<Ant> ant(n);
            for (int i = 0; i < n; i++)
                scanf("%d %d", &ant[i].position, &ant[i].direct);
            sort(ant.begin(), ant.end());
            int target, toLeft = 0;    //这里选用向左走的为基准来做
            for (int i = 0; i < n; i++)    //遍历所有蚂蚁
            {
                if (ant[i].direct == 0)
                    target = i;
                if (ant[i].direct == -1)
                    toLeft++;
            }//现在的target就是静止的蚂蚁左边的数量了
            bool flag = false;
            int ans;
            if (toLeft == target)
                flag = true;
            else if (toLeft > target)//这样的话我们要找的就是所有向左走的蚂蚁中,第target蚂蚁
            {
                int cnt = 0;//计数器
                for (int i = 0; i < n; i++) {
                    if (ant[i].direct == -1 && cnt == target) {
                        ans = ant[i].position;
                        break;
                    } else if (ant[i].direct == -1)
                        cnt++;
                }
            } else    //向左走的蚂蚁少,那么目标蚂蚁会向右落下
            {
                int cnt = 0;
                for (int i = n - 1; i >= 0; i--) {
                    if (ant[i].direct == 1 && cnt == n - target - 1)//相应的变化,cnt要变成静止蚂蚁右边的蚂蚁数量
                    {
                        ans = 100 - ant[i].position;
                        break;
                    } else if (ant[i].direct == 1)
                        cnt++;
                }
            }
            if (flag)
                printf("Cannot fall!\n");
            else
                printf("%d\n", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    tomcat9部署到nginx,不能通过nginx访问到tomcat
    解决Linux系统部署webapp,JavaMail 发送邮件javax.mail.MessagingException: 501 Syntax: HELO hostname问题
    先本地仓库中国添加jar包
    IDEA修改pom.xml文件不自动下载的问题
    JavaWeb路径的理解【加不加斜杠又何区别】
    IDEA好用的模板设置
    使用maven启动web项目报错
    MAVEN的学习(图片资源来自黑马程序员)
    option标签不支持单击事件
    immutable.js学习笔记(九)----- Range 与 Repeat
  • 原文地址:https://www.cnblogs.com/zhangzizi/p/14352317.html
Copyright © 2011-2022 走看看