PAT团体天梯

L1-001:略。

L-002:
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印



*



所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。

给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。

输入格式:

输入在一行给出1个正整数N(<=1000)和一个符号,中间以空格分隔。

输出格式:

首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。

输入样例:
19 *
输出样例:

  *****
   ***
    *
   ***
  *****

L1-002

题目解析:
图形上下对称,输入为n, 设图案倒三角形层数为m。
第一层个数: m * 2 - 1;
%利用等差公式
则除去顶角的倒三角形总的个数为:(3 + m * 2 - 1) * ( m -1) / 2
化简得 : m^2 - 1
倒三角形所有图案个数为 m ^ 2
利用输入n,求出 m。
(int)(n + 1 )/2 = m ^ 2

如例题:
(int)(19 + 1)/ 2 = m ^ 2
10 = m^2
m = 3
则倒三角形的图案层数为3层
图案个数为9个
但是正三角形与之共用顶点, 所以总的个数减一
sum = 9 * 2 - 1
剩余 19 - sum = 2

代码:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{

    int n,n1 ;
    char ch;
    cin >> n ;
    cin >> ch;
    n1 = n;
    n += 1;
    n /= 2;
    n = sqrt(n);
    for(int i = 0;i < n; i++){
        for(int j = 0;j < i; j++){
            cout<<' ';
        }
        for(int j = 0; j <(n-i)*2-1;j++){
            cout<<ch;
        }
        cout<<endl;
    }
    for( int i = n-2;i >= 0;i--){
        for(int j = 0;j < i;j++){
            cout<<' ';
        }
        for(int j = 0;j < (n-i)*2 - 1; j++){
            cout<<ch;
        }
        cout<<endl;
    }
    cout<< n1- n*n*2+1;
}

L1-003:
给定一个k位整数N = dk-1*10k-1 + … + d1*101 + d0 (0<=di<=9, i=0,…,k-1, dk-1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定N = 100311,则有2个0,3个1,和1个3。

输入格式:

每个输入包含1个测试用例,即一个不超过1000位的正整数N。

输出格式:

对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。

输入样例:
100311
输出样例:
0:2
1:3
3:1

输入直接用string保存,(string)str.length() 得到长度去遍历, 定义一个num[10]初始化为0,每个下标就是数字0 - 9 。
遍历每一个字符减去’0’就得到了下标。

代码

#include"iostream"
#include"cstdio"
#include"string"
using namespace std;
int main()
{

    int num[10] = {0};
    string n;
    cin>>n;
    for(int i = 0 ; i < n.length();i++){
        char ch = n[i];
        num[ch - '0']++;
    }
    for(int i = 0 ; i < 10 ; i++){
        if(num[i]){
            cout<<i;
            cout<<":";
            cout<<num[i]<<endl;
        }
    }
}

L1-004 ——L1-005:略

L1-006:
一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7,其中5、6、7就是3个连续的数字。给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。

输入格式:
输入在一行中给出一个正整数N (1< N <231)。

输出格式:

首先在第1行输出最长连续因子的个数;然后在第2行中按“因子1*因子2*……*因子k”的格式输出最小的连续因子序列,其中因子按递增顺序输出,1不算在内。

输入样例:
630
输出样例:
3
5*6*7

代码:

#include<iostream>
using namespace std;
int main()
{
    int n ;
    cin>>n;
    int lenght1 = 0 , lenght2 = 0, sign1 = 0,sign2 = 0;
    for(int i = 2; i < 100000000;i++){

        if(n % i == 0){
            lenght1 = 0;
            sign1 = i;
            for(int j = i ; j < 100000000;j++){
                if(n % j == 0){
                    lenght1++;
                }else {
                    break;
                }
            }

            if(lenght1 > lenght2){
                lenght2 = lenght1;
                sign2 = sign1;
            }
            lenght1 = 0;
        }
    }
    cout<<lenght2<<endl;
    for(int i = sign2 ; i < lenght2+sign2;i++){
        cout<<i;
        if(i < lenght2+sign2 - 1){
            cout<<'*';
        }
    }
}

L1-007:
输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出“fu”字。十个数字对应的拼音如下:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu
输入格式:

输入在一行中给出一个整数,如: 1234 。

提示:整数包括负数、零和正数。

输出格式:

在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如 yi er san si。

输入样例:
-600
输出样例:
fu liu ling ling

代码:

#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
    map<char,string> num;
    num['-'] = "fu";
    num['0'] = "ling";
    num['1'] = "yi";
    num['2'] = "er";
    num['3'] = "san";
    num['4'] = "si";
    num['5'] = "wu";
    num['6'] = "liu";
    num['7'] = "qi";
    num['8'] = "ba";
    num['9'] = "jiu";

    string str ;
    cin>>str;
    for( int i = 0; i < str.length();i++){
        cout<<num[str[i]];
        if(i < str.length() - 1){
            cout<<' ';
        }
    }
}

L1-008:给定两个整数A和B,输出从A到B的所有整数以及这些数的和。

输入格式:

输入在一行中给出2个整数A和B,其中-100<=A<=B<=100,其间以空格分隔。

输出格式:

首先顺序输出从A到B的所有整数,每5个数字占一行,每个数字占5个字符宽度,向右对齐。最后在一行中输出全部数字的和。

输入样例:
-3 8
输出样例:
-3 -2 -1 0 1
2 3 4 5 6
7 8
Sum = 30

代码:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{

    int n , m , sum=0;
    cin>>n>>m;
    int l = m - n + 1;
    for(int i = 0 ;i < l ; i++){
        printf("%5d",i+n);
        sum += i+ n;
        if((i+1)%5 == 0)
        cout<<endl;
    }
    if(l %5 != 0)
    cout<<endl;
    cout<<"Sum = "<<sum;
}

L1-009:本题的要求很简单,就是求N个数字的和。麻烦的是,这些数字是以有理数“分子/分母”的形式给出的,你输出的和也必须是有理数的形式。

输入格式:

输入第一行给出一个正整数N(<=100)。随后一行按格式“a1/b1 a2/b2 …”给出N个有理数。题目保证所有分子和分母都在长整型范围内。另外,负数的符号一定出现在分子前面。

输出格式:

输出上述数字和的最简形式 —— 即将结果写成“整数部分 分数部分”,其中分数部分写成“分子/分母”,要求分子小于分母,且它们没有公因子。如果结果的整数部分为0,则只输出分数部分。

输入样例1:
5
2/5 4/15 1/30 -2/60 8/3
输出样例1:
3 1/3
输入样例2:
2
4/3 2/3
输出样例2:
2
输入样例3:
3
1/3 -1/6 1/8
输出样例3:
7/24

利用:最小公倍数

long long  gcd(long long  a , long long  b)
{
    return b == 0?a:gcd(b,a%b);
}

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long  gcd(long long  a , long long  b)
{
    return b == 0?a:gcd(b,a%b);
}
long long ab(long long a){
    return a > 0?a:-a;
}
int main()
{

    long long gvelues,suma = 0, sumb = 1, a,b, n;
    cin>>n;
    for(int i = 0; i < n ; i++){
        scanf("%lld/%lld",&a,&b);
        gvelues = (suma == 1)?1:gcd(ab(suma),sumb);
        suma /= gvelues;
        sumb /= gvelues;
        gvelues = (a == 1)?1:gcd(ab(a),b);
        a /= gvelues;
        b /= gvelues;
        suma = suma * b + a * sumb;
        sumb = b * sumb;
    }
    gvelues = (suma == 1)?1:gcd(ab(suma),sumb);
    suma /= gvelues;
    sumb /= gvelues;
    if(!suma){
        cout<<0;
        return 0;
    }
    if(suma / sumb){
        cout<<suma/sumb;
        suma = ab(suma %sumb);
        if(suma){
            cout<<' '<<suma<<'/'<<sumb;
        }
    }else cout<<suma<<'/'<<sumb;
}

L1-010:
本题要求将输入的任意3个整数从小到大输出。

输入格式:

输入在一行中给出3个整数,其间以空格分隔。

输出格式:

在一行中将3个整数从小到大输出,其间以“->”相连。

输入样例:
4 2 8
输出样例:
2->4->8

代码:

#include<iostream>
#include<stdlib.h>
using namespace std;
int cmp(const void *a,const void *b){
    return *(int*)a - *(int*)b;
}
int main()
{

    int a[3];
    cin>>a[0]>>a[1]>>a[2];
    qsort(a,3,sizeof(int),cmp);
    cout<<a[0]<<"->"<<a[1]<<"->"<<a[2];
}

L1-011:
本题要求你计算A-B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A-B。

输入格式:

输入在2行中先后给出字符串A和B。两字符串的长度都不超过104,并且保证每个字符串都是由可见的ASCII码和空白字符组成,最后以换行符结束。

输出格式:

在一行中打印出A-B的结果字符串。

输入样例:
I love GPLT! It’s a fun game!
aeiou
输出样例:
I lv GPLT! It’s fn gm!

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{

    char str1[10000]={0},str2[10000]={0};
    cout<<(int)'z'<<endl;
    gets(str1);
    gets(str2);
    int str[256]={1};
    int len = strlen(str1);

    for(int i = 0; i < strlen(str2);i++){
        str[str2[i]] = 0;

    }
    for(int i = 0 ;i  < len;i++){
        if(str[str1[i]]){
            cout<<str1[i];
        }
    }
}

L1-012:
真的没骗你,这道才是简单题 —— 对任意给定的不超过10的正整数n,要求你输出2n。不难吧?

输入格式:

输入在一行中给出一个不超过10的正整数n。

输出格式:

在一行中按照格式“2^n = 计算结果”输出2n的值。

输入样例:
5
输出样例:
2^5 = 32

利用快速幂:

b为偶数时:ab=a(b/2)*2=(a2)b/2b为奇数时:ab=a*ab-1=a*(a2)(b-1)/228=(22)4         27=2*(22)3

代码:

#include<iostream>
using namespace std;

int main()
{
    int n1,n ;
    cin>> n1 ;
    n = n1;
    int ans = 1;
    int base = 2;
    while(n){
        if(n&1){
            ans = ans * base;
        }
        base *= base;
        n>>=1;
    }
    cout<<2<<'^'<<n1<<' '<<'='<<' '<<ans;
}

L1-013:
对于给定的正整数N,需要你计算 S = 1! + 2! + 3! + … + N!。

输入格式:

输入在一行中给出一个不超过10的正整数N。

输出格式:

在一行中输出S的值。

输入样例:
3
输出样例:
9

代码:

#include<iostream>
using namespace std;
int Factorial(int n){
    int sum = 1;
    if(n == 0 || n == 1)
    return sum;
    for(int i = 2; i <= n;i++){
        sum *= i;
    }
    return sum;
}
int main()
{

    int n ;
    cin>>n;
    int sum  = 0;
    for(int i = 1; i <= n ;i++){
        sum+=Factorial(i);
    }
    cout<<sum;
}

L1-014:
这次真的没骗你 —— 这道超级简单的题目没有任何输入。

你只需要在一行中输出事实:“This is a simple problem.”就可以了。

代码:

#include<iostream>
using namespace std;
int main()
{

    cout<<"This is a simple problem.";
}

L1-015:

美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!

输入格式:

输入在一行中给出正方形边长N(3<=N<=21)和组成正方形边的某种字符C,间隔一个空格。

输出格式:

输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%(四舍五入取整)。

输入样例:
10 a
输出样例:
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa

代码:

#include<iostream>
using namespace std;
int main()
{

    int n;
    char a;
    cin>>n>>a;
    int m;
    if(n & 1){
        m = n / 2 + 1;
    }else m = n / 2;

    for(int i = 0; i < m;i++){
        for(int j = 0; j < n ;j++){
            cout<<a;
        }
        cout<<endl;
    }
}

L1-016:

一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:

首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2

现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

输入格式:

输入第一行给出正整数N(<= 100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。

输出格式:

按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,则输出“All passed”。

输入样例1:
4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
输出样例1:
12010X198901011234
110108196711301866
37070419881216001X
输入样例2:
2
320124198808240056
110108196711301862
输出样例2:
All passed

代码:

#include<iostream>
using namespace std;
int main()
{
    int weights[]={-1,7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    int Z[11] = {'1','0','X','9','8','7','6', '5', '4','3','2'};
    int n;
    cin>>n;
    int flag = 1;
    for(int i = 0;i < n; i++){
        string str;
        cin>>str;
        int sum = 0;
        for(int j = 0; j < 17;j++){
            sum += (weights[j+1] * (str[j] - '0'));
        }
        sum %= 11;
        if(Z[sum] != str[17]){
            cout<<str<<endl;
            flag = 0;
        }
    }
    if(flag)
    {
        cout<<"All passed";
    }


}

L1-017:
一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字“-13142223336”是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11*1.5*2*100%,约为81.82%。本题就请你计算一个给定整数到底有多二。

输入格式:

输入第一行给出一个不超过50位的整数N。

输出格式:

在一行中输出N犯二的程度,保留小数点后两位。

输入样例:
-13142223336
输出样例:
81.82%

代码:

#include<string>
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{

    string str;
    cin>>str;
    int begin = 0;
    int flag = 0;
    double numbers = 0;
    if(str[0] == '-'){
        flag = 1;
        begin = 1;
    }
    for(int i = begin; i < str.length(); i++){
        if(str[i] == '2'){
            numbers++;
        }

    }
    if(flag){
        numbers /= (str.length()-1);
    }else
        numbers /= str.length();
    if((str[str.length()-1]-'0') %2 == 0){
        numbers *= 2;
    }
    if(flag){
        numbers *= 1.5;
    }
    numbers *= 100;
    printf("%.2lf%%",numbers);
}

L1-018:
微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉。不过由于笨钟自己作息也不是很规律,所以敲钟并不定时。一般敲钟的点数是根据敲钟时间而定的,如果正好在某个整点敲,那么“当”数就等于那个整点数;如果过了整点,就敲下一个整点数。另外,虽然一天有24小时,钟却是只在后半天敲1~12下。例如在23:00敲钟,就是“当当当当当当当当当当当”,而到了23:01就会是“当当当当当当当当当当当当”。在午夜00:00到中午12:00期间(端点时间包括在内),笨钟是不敲的。

下面就请你写个程序,根据当前时间替大笨钟敲钟。

输入格式:

输入第一行按照“hh:mm”的格式给出当前时间。其中hh是小时,在00到23之间;mm是分钟,在00到59之间。

输出格式:

根据当前时间替大笨钟敲钟,即在一行中输出相应数量个“Dang”。如果不是敲钟期,则输出:

Only hh:mm. Too early to Dang.
其中“hh:mm”是输入的时间。

输入样例1:
19:05
输出样例1:
DangDangDangDangDangDangDangDang
输入样例2:
07:05
输出样例2:
Only 07:05. Too early to Dang.

代码:

#include<string>
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{

    int hours,minues;
    scanf("%d:%d",&hours,&minues);
    if(hours<=12){
        printf("Only %02d:%02d.  Too early to Dang.",hours,minues);
    }else {
        for(int i = 0 ; i < hours - 12 ; i++){
            cout<<"Dang";
        }
        if(minues> 0){
            cout<<"Dang";
        }
    }
}

L1-019:

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就输了,输家罚一杯酒。两人同赢或两人同输则继续下一轮,直到唯一的赢家出现。

下面给出甲、乙两人的酒量(最多能喝多少杯不倒)和划拳记录,请你判断两个人谁先倒。

输入格式:

输入第一行先后给出甲、乙两人的酒量(不超过100的非负整数),以空格分隔。下一行给出一个正整数N(<=100),随后N行,每行给出一轮划拳的记录,格式为:

甲喊 甲划 乙喊 乙划

其中“喊”是喊出的数字,“划”是划出的数字,均为不超过100的正整数(两只手一起划)。

输出格式:

在第一行中输出先倒下的那个人:A代表甲,B代表乙。第二行中输出没倒的那个人喝了多少杯。题目保证有一个人倒下。注意程序处理到有人倒下就终止,后面的数据不必处理。

输入样例:
1 1
6
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15
15 1 1 16
输出样例:
A
1

代码:

#include<iostream>
using namespace std;
int main()
{

    int formula1 , formula2;
    cin>>formula1>>formula2;
    int action_formula1 = formula1,action_formula2 = formula2;
    int n;
    cin>>n;
    int num1_drink,num1_action,num2_drink,num2_action;
    for(int i = 0;i < n;i++){
        cin>>num1_drink>>num1_action>>num2_drink>>num2_action;
        if(num1_action == num2_action)continue;
        int num_sumdrink = num1_drink+num2_drink;
        if(num1_action == num_sumdrink){
            action_formula1--;
            if(action_formula1 < 0){
                cout<<"A"<<endl<<formula2 - action_formula2;
                return 0;
            }
        }
        if(num2_action == num_sumdrink){
            action_formula2--;
            if(action_formula2 < 0){
                cout<<"B"<<endl<<formula1 - action_formula1;
                return 0 ;
            }
        }
    }
}

L1-020:

当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。

输入格式:

输入第一行给出一个正整数N(<=100),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K(<=1000),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(<=10000),为待查询的人数;随后一行中列出M个待查询的ID,以空格分隔。

注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有K超过1的朋友圈里都至少有2个不同的人。

输出格式:

按输入的顺序输出那些帅到没朋友的人。ID间用1个空格分隔,行的首尾不得有多余空格。如果没有人太帅,则输出“No one is handsome”。

注意:同一个人可以被查询多次,但只输出一次。

输入样例1:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
输出样例1:
10000 88888 23333
输入样例2:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
输出样例2:
No one is handsome

代码:

#include<set>
#include<iostream>
using namespace std;
int main()
{
    set<string> a;
    int flag = 1;
    int n ;
    cin>>n;
    for(int i = 0 ;i < n ; i++){
        int m;
        cin>>m;
        for(int j = 0 ; j < m ; j++){
            string alternative;
            cin>>alternative;
            if(m >= 2)
            a.insert(alternative);
        }
    }

    cin>>n ;
    set<string> ans;
    for(int i =0 ; i < n ; i++){
        string alternative ;
        cin>>alternative;
        if(a.find(alternative) == a.end() && ans.find(alternative) == ans.end()){
            if(!flag)
                cout<<' ';
            cout<<alternative;
            ans.insert(alternative);
            flag = 0;
        }
    }
    if(flag)
    cout<<"No one is handsome";
}

L1-021:

这道超级简单的题目没有任何输入。

你只需要把这句很重要的话 —— “I’m gonna WIN!”——连续输出三遍就可以了。

注意每遍占一行,除了每行的回车不能有任何多余字符。

代码:

#include<iostream>
using namespace std;
int main(){
    string str = "I'm gonna WIN!";
    cout<<str<<endl<<str<<endl<<str;
}

L1-022:
给定N个正整数,请统计奇数和偶数各有多少个?

输入格式:

输入第一行给出一个正整N(<= 1000);第2行给出N个正整数,以空格分隔。

输出格式:

在一行中先后输出奇数的个数、偶数的个数。中间以1个空格分隔。

输入样例:
9
88 74 101 26 15 0 34 22 77
输出样例:
3 6

代码:

#include<iostream>
using namespace std;
int main(){
    int n;
    int odd = 0,even = 0;
    cin>>n;
    for(int i = 0; i < n ; i++){
        int input;
        cin>>input;
        if(input & 1){
            odd++;
        }else{
            even++;
        }
    }
    cout<<odd<<' '<<even;
}

L1-023:

给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按“GPLTGPLT….”这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT的顺序打印,直到所有字符都被输出。

输入格式:

输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。

输出格式:

在一行中按题目要求输出排序后的字符串。题目保证输出非空。

输入样例:
pcTclnGloRgLrtLhgljkLhGFauPewSKgt
输出样例:
GPLTGPLTGLTGLGLL

代码:

#include<iostream>
using namespace std;
int main()
{
    int num[4] = {0};
    string str;
    cin>>str;
    for(int i = 0 ; i < str.length();i++){
        switch(str[i]){
            case 'G':
            case 'g':num[0]++;
                break;
            case 'P':
            case 'p':num[1]++;
                break;
            case 'L':
            case 'l':num[2]++;
                break;
            case 'T':
            case 't':num[3]++;
                break;
            dafult:continue;
        }
    }
    for(int i = 0; i+1;i++){
        if(num[0]==0&&num[1]==0&&num[2]==0&&num[3]==0){
            break;
        }
        if(num[0]){
            cout<<"G";
            num[0]--;
        }
        if(num[1]){
            cout<<"P";
            num[1]--;
        }
        if(num[2]){
            cout<<"L";
            num[2]--;
        }
        if(num[3]){
            cout<<"T";
            num[3]--;
        }
    }
    return 0;
}

L1-024:

如果今天是星期三,后天就是星期五;如果今天是星期六,后天就是星期一。我们用数字1到7对应星期一到星期日。给定某一天,请你输出那天的“后天”是星期几。

输入格式:

输入第一行给出一个正整数D(1 <= D <=7),代表星期里的某一天。

输出格式:

在一行中输出D天的后天是星期几。

输入样例:
3
输出样例:
5

代码:

#include<iostream>
using namespace std;
int main()
{
    int num[7]={7,1,2,3,4,5,6};
    int n;  
    cin>>n;
    cout<<num[(n+2)%7];
    return 0;
}

L1-025:

本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000]。稍微有点麻烦的是,输入并不保证是两个正整数。

输入格式:

输入在一行给出A和B,其间以空格分开。问题是A和B不一定是满足要求的正整数,有时候可能是超出范围的数字、负数、带小数点的实数、甚至是一堆乱码。

注意:我们把输入中出现的第1个空格认为是A和B的分隔。题目保证至少存在一个空格,并且B不是一个空字符串。

输出格式:

如果输入的确是两个正整数,则按格式“A + B = 和”输出。如果某个输入不合要求,则在相应位置输出“?”,显然此时和也是“?”。

输入样例1:
123 456
输出样例1:
123 + 456 = 579
输入样例2:
22. 18
输出样例2:
? + 18 = ?
输入样例3:
-100 blabla bla…33
输出样例3:
? + ? = ?

代码:

/**
 * 懒得写了。直接copy大佬的
**/
#include <iostream>
#include <cctype>
using namespace std;
bool islegal(string s) {
    if(s.length() == 0)
        return false;
    for(int i = 0; i < s.length(); i++) {
        if(!isdigit(s[i])) {
            return false;
        }
    }
    int temp = stoi(s);
    if(temp < 1 || temp > 1000) {
        return false;
    }
    return true;
}

int main() {
    string a, b;
    string t;
    getline(cin, t);
    for(int i = 0; i < t.length(); i++) {
        if(t[i] == ' ') {
            a = t.substr(0, i);
            b = t.substr(i + 1, t.size()- i - 1);
            break;
        }
    }
    if(!islegal(a)) {
        a = "?";
    }
    if(!islegal(b)) {
        b = "?";
    }
    cout << a << " + " << b << " = ";
    if(a != "?" && b != "?") {
        int atemp = stoi(a);
        int btemp = stoi(b);
        cout << atemp + btemp;
    } else {
        cout << "?";
    }
    return 0;
}

L1-026:

这道超级简单的题目没有任何输入。

你只需要把这句很重要的话 —— “I Love GPLT”——竖着输出就可以了。

所谓“竖着输出”,是指每个字符占一行(包括空格),即每行只能有1个字符和回车。

代码:

L1-027:
下面是新浪微博上曾经很火的一张图:
这里写图片描述

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1,index[1]=0 对应 arr[0]=8,index[2]=3 对应 arr[3]=0,以此类推…… 很容易得到电话号码是18013820100。

本题要求你编写一个程序,为任何一个电话号码生成这段代码 —— 事实上,只要生成最前面两行就可以了,后面内容是不变的。

输入格式:

输入在一行中给出一个由11位数字组成的手机号码。

输出格式:

为输入的号码生成代码的前两行,其中arr中的数字必须按递减顺序给出。

输入样例:
18013820100
输出样例:
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};
代码:

import java.util.Scanner;
import java.lang.Integer;
import java.lang.String;

public class L1_027{
    public static void main(String[] args) {
        Scanner in  = new Scanner(System.in);//读取文件
        String str = in.nextLine();//18013820100
        int[] num = new int[10];//纪录哪些号码被使用
        for(int i  = 0 ; i < str.length();i++){
            int n =  str.charAt(i) - '0';
            num[n]++;
        }
        int[] arr = new int[10];//吧使用的数字添加到数组
        int arrlength = 0;//纪录arr数组的长度
        for(int i = 9; i >= 0 ; i--){//数字从大到小遍历
            if(num[i] != 0){
                arr[arrlength++] = i;
            }
        }
        int[] index = new int[11];
        for (int i = 0 ; i < str.length();i++) {
            int n = str.charAt(i) - '0';
            for(int j = 0 ;j < arrlength ; j++){
                if(arr[j] == n){
                    index[i] = j;
                }
            }
        }
        System.out.print("int[] arr = new int[]{");
        for(int i = 0 ; i < arrlength ; i++){
            System.out.print(arr[i]);
            if(i != arrlength - 1)
                System.out.print(',');
        }
        System.out.println("};");
        System.out.print("int[] index = new int[]{");
        for(int i = 0 ; i < 11;i++){
            System.out.print(index[i]);
            if(i != 10)
                System.out.print(',');
        }
        System.out.print("};");


    }
}

L1-028:

本题的目标很简单,就是判断一个给定的正整数是否素数。

输入格式:

输入在第一行给出一个正整数N(<=10),随后N行,每行给出一个小于231的需要判断的正整数。

输出格式:

对每个需要判断的正整数,如果它是素数,则在一行中输出“Yes”,否则输出“No”。

输入样例:
2
11
111
输出样例:
Yes
No

代码:

import java.util.Scanner;

public class L1_028 {
    public static void main(String[] args) {
        int n ;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i  = 0 ; i < n  ; i++){
            int num = Integer.valueOf(in.nextInt());
            if(getPrime(num)) {
                System.out.println("No");
            }else
                System.out.println("Yes");
        }
    }
    public static boolean getPrime(int n){
        if( n == 1){
            return  true;
        }
        for(int i = 2 ; i <= Math.sqrt(n); i++){
            if(n % i == 0) {
                return true;
            }
        }
        return false;
    }
}

L1-029:

据说一个人的标准体重应该是其身高(单位:厘米)减去100、再乘以0.9所得到的公斤数。已知市斤是公斤的两倍。现给定某人身高,请你计算其标准体重应该是多少?(顺便也悄悄给自己算一下吧……)

输入格式:

输入第一行给出一个正整数H(100 < H <= 300),为某人身高。

输出格式:

在一行中输出对应的标准体重,单位为市斤,保留小数点后1位。

输入样例:
169
输出样例:
124.2

代码:

import java.util.Scanner;

public class L1_029 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num;
        num = Integer.valueOf(in.nextInt());
        System.out.printf("%.2f",Double.valueOf(num - 100)*1.8);
    }
}

L1-030:

“一帮一学习小组”是中小学中常见的学习组织方式,老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组。本题就请你编写程序帮助老师自动完成这个分配工作,即在得到全班学生的排名后,在当前尚未分组的学生中,将名次最靠前的学生与名次最靠后的异性学生分为一组。

输入格式:

输入第一行给出正偶数N(<=50),即全班学生的人数。此后N行,按照名次从高到低的顺序给出每个学生的性别(0代表女生,1代表男生)和姓名(不超过8个英文字母的非空字符串),其间以1个空格分隔。这里保证本班男女比例是1:1,并且没有并列名次。

输出格式:

每行输出一组两个学生的姓名,其间以1个空格分隔。名次高的学生在前,名次低的学生在后。小组的输出顺序按照前面学生的名次从高到低排列。

输入样例:
8
0 Amy
1 Tom
1 Bill
0 Cindy
0 Maya
1 John
1 Jack
0 Linda
输出样例:
Amy Jack
Tom Linda
Bill Maya
Cindy John

代码:

import java.util.Scanner;

public class L1_030 {
    static class Student{
        private int sign ;
        private String name;
        public Student(int sign , String name){
            this.sign = sign;
            this.name = name;
        }
        public Student(){

        }
        public void setSign(int sign) {
            this.sign = sign;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSign() {
            return sign;
        }

        public String getName() {
            return name;
        }
    }

    public static void main(String[] args) {
        int n;
        Scanner in = new Scanner(System.in);
        n = Integer.valueOf(in.nextInt());
        Student[] students = new Student[n];
        for(int i = 0 ; i < n ; i++){
            int sign ;
            String name;
            sign = Integer.valueOf(in.nextInt());
            name = String.valueOf(in.next());
            students[i] = new Student();
            students[i].setName(name);
            students[i].setSign(sign);
        }
        int[] num = new int[n];
        for(int i = 0; i < n;i++){
            for(int j = n-1; j>=0;j--){
                if(num[j] == 0&&students[j].getSign()!=students[i].getSign()){
                    System.out.println(students[i].getName()+' '+students[j].getName());
                    num[j] = num[i] = 1;
                    break;
                }
            }

        }
    }
}

L1-031:

据说一个人的标准体重应该是其身高(单位:厘米)减去100、再乘以0.9所得到的公斤数。真实体重与标准体重误差在10%以内都是完美身材(即 |真实体重-标准体重| < 标准体重x10%)。已知1市斤=0.5公斤。现给定一群人的身高和实际体重,请你告诉他们是否太胖或太瘦了。

输入格式:

输入第一行给出一个正整数N(<= 20)。随后N行,每行给出两个整数,分别是一个人的身高H(120 < H < 200;单位:厘米)和真实体重W(50 < W <= 300;单位:市斤),其间以空格分隔。

输出格式:

为每个人输出一行结论:如果是完美身材,输出“You are wan mei!”;如果太胖了,输出“You are tai pang le!”;否则输出“You are tai shou le!”。

输入样例:
3
169 136
150 81
178 155
输出样例:
You are wan mei!
You are tai shou le!
You are tai pang le!

代码:

import java.util.Scanner;

public class L1_031 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.valueOf(in.nextInt());
        for(int i = 0; i < n; i++){
            Double heigh = Double.valueOf(in.nextInt());
            Double weith = Double.valueOf(in.nextInt());
            double standard = (heigh - 100)*1.8;
            double scope = standard * 0.1;
            double diffrence = weith - standard;
            if(diffrence*100 >= scope*100){
                System.out.println("You are tai pang le!");
            }else if(diffrence*100 <= -scope*100){
                System.out.println("You are tai shou le!");
            }else System.out.println("You are wan mei!");
        }
    }
}

L1-032:

根据新浪微博上的消息,有一位开发者不满NPM(Node Package Manager)的做法,收回了自己的开源代码,其中包括一个叫left-pad的模块,就是这个模块把javascript里面的React/Babel干瘫痪了。这是个什么样的模块?就是在字符串前填充一些东西到一定的长度。例如用“*”去填充字符串“GPLT”,使之长度为10,调用left-pad的结果就应该是“******GPLT”。Node社区曾经对left-pad紧急发布了一个替代,被严重吐槽。下面就请你来实现一下这个模块。

输入格式:

输入在第一行给出一个正整数N(<=104)和一个字符,分别是填充结果字符串的长度和用于填充的字符,中间以1个空格分开。第二行给出原始的非空字符串,以回车结束。

输出格式:

在一行中输出结果字符串。

输入样例1:
15 _
I love GPLT
输出样例1:
____I love GPLT
输入样例2:
4 *
this is a sample for cut
输出样例2:
cut

代码:

#include"iostream"
#include"cstdio"
using namespace std;
int main()
{

    int n ;
    char ch;
    cin>> n >> ch;
    string str;
    getchar();
    getline(cin,str);
    if(str.length() > n){

        for(int i = str.length() - n; i < str.length();i++)
            cout<<str[i];
    }else {
        for(int i = 0 ;i < n - str.length();i++){
            cout<<ch;
        }
        cout<<str;
    }
}

L1-033:
这里写图片描述
以上是新浪微博中一奇葩贴:“我出生于1988年,直到25岁才遇到4个数字都不相同的年份。”也就是说,直到2013年才达到“4个数字都不相同”的要求。本题请你根据要求,自动填充“我出生于y年,直到x岁才遇到n个数字都不相同的年份”这句话。

输入格式:

输入在一行中给出出生年份y和目标年份中不同数字的个数n,其中y在[1, 3000]之间,n可以是2、或3、或4。注意不足4位的年份要在前面补零,例如公元1年被认为是0001年,有2个不同的数字0和1。

输出格式:

根据输入,输出x和能达到要求的年份。数字间以1个空格分隔,行首尾不得有多余空格。年份要按4位输出。注意:所谓“n个数字都不相同”是指不同的数字正好是n个。如“2013”被视为满足“4位数字都不同”的条件,但不被视为满足2位或3位数字不同的条件。

输入样例1:
1988 4
输出样例1:
25 2013
输入样例2:
1 2
输出样例2:
0 0001

代码:

#include<iostream>
#include<set>
using namespace std;

int main()
{

    int year , n;
    cin>>year>>n;

    for(int i = year ; i <= 3012;i++){
        set<int> st ;
        int num = i;
        for(int j = 0 ; j < 4;j++){
            st.insert(num%10);
            num/=10;
        }
        if(st.size() == n){
            printf("%d %04d",i-year,i);
            break;
        }
    }
}

L1-034:

微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持。每篇博文都有一些刻画其特性的标签,而你点赞的博文的类型,也间接刻画了你的特性。本题就要求你写个程序,通过统计一个人点赞的纪录,分析这个人的特性。

输入格式:

输入在第一行给出一个正整数N(<=1000),是该用户点赞的博文数量。随后N行,每行给出一篇被其点赞的博文的特性描述,格式为“K F1 … FK”,其中 1<=K<=10,Fi(i=1, …, K)是特性标签的编号,我们将所有特性标签从1到1000编号。数字间以空格分隔。

输出格式:

统计所有被点赞的博文中最常出现的那个特性标签,在一行中输出它的编号和出现次数,数字间隔1个空格。如果有并列,则输出编号最大的那个。

输入样例:
4
3 889 233 2
5 100 3 233 2 73
4 3 73 889 2
2 233 123
输出样例:
233 3

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    int n ;
    cin>>n ;
    int num[1005] = {0};
    for(int i = 0 ;i < n ; i++){
        int m;
        cin>>m;
        for(int j = 0 ; j < m;j++){
            int sign ;
            cin>>sign;
            num[sign]++;
        }
    }

    int value = 0;
    int sign = 0;
    for(int i  = 0 ; i < 1001;i++){
        if(value <= num[i]){
            value = num[i];
            sign = i;
        }
    }
    cout<<sign << ' '<<value<<endl;

}

L1-035:
这里写图片描述
以上是朋友圈中一奇葩贴:“2月14情人节了,我决定造福大家。第2个赞和第14个赞的,我介绍你俩认识…………咱三吃饭…你俩请…”。现给出此贴下点赞的朋友名单,请你找出那两位要请客的倒霉蛋。

输入格式:

输入按照点赞的先后顺序给出不知道多少个点赞的人名,每个人名占一行,为不超过10个英文字母的非空单词,以回车结束。一个英文句点“.”标志输入的结束,这个符号不算在点赞名单里。

输出格式:

根据点赞情况在一行中输出结论:若存在第2个人A和第14个人B,则输出“A and B are inviting you to dinner…”;若只有A没有B,则输出“A is the only one for you…”;若连A都没有,则输出“Momo… No one is for you …”。

输入样例1:
GaoXZh
Magi
Einst
Quark
LaoLao
FatMouse
ZhaShen
fantacy
latesum
SenSen
QuanQuan
whatever
whenever
Potaty
hahaha
.
输出样例1:
Magi and Potaty are inviting you to dinner…
输入样例2:
LaoLao
FatMouse
whoever
.
输出样例2:
FatMouse is the only one for you…
输入样例3:
LaoLao
.
输出样例3:
Momo… No one is for you …

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{

    int n = 0;
    string temporar = "4795",str1 ,str2 ;
    while(temporar != "."){
        cin>>temporar;
        n++;
        if(n == 2){
            str1 = temporar;
        }
        if( n == 14){
            str2 = temporar;
        }
    }
    n--;
    if(n < 2){
        cout<<"Momo... No one is for you ...";
    }else if(n<14){
        cout<<str1<<" is the only one for you...";
    }else {
        cout<<str1<<" and "<<str2<<" are inviting you to dinner...";
    }
}

L1-036:

看我没骗你吧 —— 这是一道你可以在10秒内完成的题:给定两个绝对值不超过100的整数A和B,输出A乘以B的值。

输入格式:

输入在第一行给出两个整数A和B(-100 <= A, B, <= 100),数字间以空格分隔。

输出格式:

在一行中输出A乘以B的值。

输入样例:
-8 13
输出样例:
-104

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    int a, b;
    cin>>a>>b;
    cout<<a*b;
}

L1-037:
真的是简单题哈 —— 给定两个绝对值不超过100的整数A和B,要求你按照“A/B=商”的格式输出结果。

输入格式:

输入在第一行给出两个整数A和B(-100 <= A, B, <= 100),数字间以空格分隔。

输出格式:

在一行中输出结果:如果分母是正数,则输出“A/B=商”;如果分母是负数,则要用括号把分母括起来输出;如果分母为零,则输出的商应为“Error”。输出的商应保留小数点后2位。

输入样例1:
-1 2
输出样例1:
-1/2=-0.50
输入样例2:
1 -3
输出样例2:
1/(-3)=-0.33
输入样例3:
5 0
输出样例3:
5/0=Error

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    int a ,b;
    cin>>a>>b;
    if(b == 0){
        printf("%d/%d=", a,b);
        cout<<"Error";
        return 0;
    }
    printf("%d", a);
    if(b < 0){
        printf("/(%d)", b);
    }else {
        printf("/%d", b);
    }
    printf("=%.2lf", (double)a/(double)b);
}

L1-038:

这道超级简单的题目没有任何输入。

你只需要在第一行中输出程序员钦定名言“Hello World”,并且在第二行中输出更新版的“Hello New World”就可以了。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    cout<<"Hello World"<<endl;
    cout<<"Hello New World"<<endl;
}

L1-039:

中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。

输入格式:

输入在第一行给出一个正整数N(<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。

输出格式:

按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)

输入样例:
4
This is a test case
输出样例:
asa T
st ih
e tsi
ce s
代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    int n ;
    string str;
    cin>>n;
    getchar();
    getline(cin,str);
    int m = str.length() / n;
    if(str.length() % n > 0){
        m +=  1;
    }
    //cout<<(int)'a'<<' '<<(int)'z'<<' '<<(int)'A'<<' '<<(int)'Z'<<endl;
    char num[1000][1000] = {0};

    int i = 0;
        for(int j = m -1 ; j >= 0 ;j--){
            for(int k = 0;k < n ; k++){

                //cout<<(int)str[i]<<' ' << i<<' ';
                    num[k][j] = str[i++];
                    //cout<<(int)num[k][j]<<endl;
                if(i == str.length()){
                    break;
                }
            }
            if(i == str.length()){
                break;
            }
        }
    for(int i = 0 ;i < n;i++){
        for(int j = 0 ;j < m;j++){
            if(num[i][j] != 0 || num[i][j] == ' ')
                cout<<num[i][j];
            else cout<<' ';
        }
        cout<<endl;
    }
}

L1-040:

专家通过多组情侣研究数据发现,最佳的情侣身高差遵循着一个公式:(女方的身高)×1.09=(男方的身高)。如果符合,你俩的身高差不管是牵手、拥抱、接吻,都是最和谐的差度。

下面就请你写个程序,为任意一位用户计算他/她的情侣的最佳身高。

输入格式:

输入第一行给出正整数N(<=10),为前来查询的用户数。随后N行,每行按照“性别 身高”的格式给出前来查询的用户的性别和身高,其中“性别”为“F”表示女性、“M”表示男性;“身高”为区间 [1.0, 3.0] 之间的实数。

输出格式:

对每一个查询,在一行中为该用户计算出其情侣的最佳身高,保留小数点后2位。

输入样例:
2
M 1.75
F 1.8
输出样例:
1.61
1.96

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    int n;
    cin>>n;
    char ch ;
    double heigh;
    for(int i = 0 ; i < n ; i++){
        cin>>ch>>heigh;
        if(ch == 'F'){
            printf("%.2lf\n", heigh*1.09);
        }else printf("%.2lf\n",heigh/1.09 );
    }
}

L1-041:

这里写图片描述
对方不想和你说话,并向你扔了一串数…… 而你必须从这一串数字中找到“250”这个高大上的感人数字。

输入格式:

输入在一行中给出不知道多少个绝对值不超过1000的整数,其中保证至少存在一个“250”。

输出格式:

在一行中输出第一次出现的“250”是对方扔过来的第几个数字(计数从1开始)。题目保证输出的数字在整型范围内。

输入样例:
888 666 123 -233 250 13 250 -222
输出样例:
5

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    bool falg = false;
    int n = 0 ;
    int num;
    while(~scanf("%d",&num)){
        if(!falg)
            n++;
        if(num == 250){
            falg = true;
        }
    }
    cout<<n;
}

L1-042:

世界上不同国家有不同的写日期的习惯。比如美国人习惯写成“月-日-年”,而中国人习惯写成“年-月-日”。下面请你写个程序,自动把读入的美国格式的日期改写成中国习惯的日期。

输入格式:

输入在一行中按照“mm-dd-yyyy”的格式给出月、日、年。题目保证给出的日期是1900年元旦至今合法的日期。

输出格式:

在一行中按照“yyyy-mm-dd”的格式给出年、月、日。

输入样例:
03-15-2017
输出样例:
2017-03-15

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    int  year, month, day;
    scanf("%d-%d-%d",&month,&day,&year);

    printf("%4d-%02d-%02d\n", year,month ,day);
}

L1-043:

天梯图书阅览室请你编写一个简单的图书借阅统计程序。当读者借书时,管理员输入书号并按下S键,程序开始计时;当读者还书时,管理员输入书号并按下E键,程序结束计时。书号为不超过1000的正整数。当管理员将0作为书号输入时,表示一天工作结束,你的程序应输出当天的读者借书次数和平均阅读时间。

注意:由于线路偶尔会有故障,可能出现不完整的纪录,即只有S没有E,或者只有E没有S的纪录,系统应能自动忽略这种无效纪录。另外,题目保证书号是书的唯一标识,同一本书在任何时间区间内只可能被一位读者借阅。

输入格式:

输入在第一行给出一个正整数N(<= 10),随后给出N天的纪录。每天的纪录由若干次借阅操作组成,每次操作占一行,格式为:

书号([1, 1000]内的整数) 键值(“S”或“E”) 发生时间(hh:mm,其中hh是[0,23]内的整数,mm是[0, 59]内整数)

每一天的纪录保证按时间递增的顺序给出。

输出格式:

对每天的纪录,在一行中输出当天的读者借书次数和平均阅读时间(以分钟为单位的精确到个位的整数时间)。

输入样例:
3
1 S 08:10
2 S 08:35
1 E 10:00
2 E 13:16
0 S 17:00
0 S 17:00
3 E 08:10
1 S 08:20
2 S 09:00
1 E 09:20
0 E 17:00
输出样例:
2 196
0 0
1 60

#include <iostream>
#include <cstring>

using namespace std;  

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int id = -1;
        double time = 0;
        int num = 0;
        int mintue[2001] = {0};
        bool flag[2001];
        memset(flag, false, sizeof(flag));
        while (id != 0) {
            char a;
            int h, m;
            scanf("%d %c %d:%d", &id, &a, &h, &m);
            if (id == 0) {
                break;
            } else if (a == 'S') {
                mintue[id] = h * 60 + m;
                flag[id] = true;
            } else if (a == 'E') {
                if (flag[id]) {
                    flag[id] = false;
                    time += h * 60 + m - mintue[id];
                    mintue[id] = 0;
                    num++;
                }
            }
        }
        int t;
        if (num != 0) {
            time /= num;
        }
        t = time + 0.5f;
        printf("%d %d\n", num, t);
    }
    return 0;  
}  

L1-044:

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:

这里写图片描述
现要求你编写一个稳赢不输的程序,根据对方的出招,给出对应的赢招。但是!为了不让对方输得太惨,你需要每隔K次就让一个平局。

输入格式:

输入首先在第一行给出正整数K(<=10),即平局间隔的次数。随后每行给出对方的一次出招:“ChuiZi”代表“锤子”、“JianDao”代表“剪刀”、“Bu”代表“布”。“End”代表输入结束,这一行不要作为出招处理。

输出格式:

对每一个输入的出招,按要求输出稳赢或平局的招式。每招占一行。

输入样例:
2
ChuiZi
JianDao
Bu
JianDao
Bu
ChuiZi
ChuiZi
End
输出样例:
Bu
ChuiZi
Bu
ChuiZi
JianDao
ChuiZi
Bu

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define true 1
#define false 0
using namespace std;
int main()
{
    int n ;
    cin>>n;
    int i = 0;
    string str;
    while(cin>>str&&str != "End"){

        if(i == n){
            i = 0;
            cout<<str<<endl;
            continue;
        }
        i++;
        if(str == "ChuiZi"){
            cout<<"Bu"<<endl;
        }else if(str == "Bu"){
            cout<<"JianDao"<<endl;
        }else cout<<"ChuiZi"<<endl;
    }
}

L1-045:略

L1-046:
这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如,111111就可以被13整除。 现在,你的程序要读入一个整数x,这个整数一定是奇数并且不以5结尾。然后,经过计算,输出两个数字:第一个数字s,表示x乘以s是一个光棍,第二个数字n是这个光棍的位数。这样的解当然不是唯一的,题目要求你输出最小的解。

提示:一个显然的办法是逐渐增加光棍的位数,直到可以整除x为止。但难点在于,s可能是个非常大的数 —— 比如,程序输入31,那么就输出3584229390681和15,因为31乘以3584229390681的结果是111111111111111,一共15个1。

输入格式:

输入在一行中给出一个不以5结尾的正奇数x(< 1000)。

输出格式:

在一行中输出相应的最小的s和n,其间以1个空格分隔。

输入样例:
31
输出样例:
3584229390681 15

代码:

import java.math.*;
import java.util.*;
public class Main{
    public static void main(String[] args){
        BigInteger num = new BigInteger("1");

        Scanner in = new Scanner(System.in);

        BigInteger n = in.nextBigInteger();
        for(int i = 1 ; i < 100000000;i++){
            if(num.remainder(n).intValue() == 0){
                System.out.print(num.divide(n)+" "+i);
                return ;
            }
            num = num.multiply(new BigInteger(String.valueOf(10)));
            //System.out.println(num);
            num = num.add(new BigInteger(String.valueOf(1)));
            //System.out.println(num);
        }
    }
}

L1-047:
你永远叫不醒一个装睡的人 —— 但是通过分析一个人的呼吸频率和脉搏,你可以发现谁在装睡!医生告诉我们,正常人睡眠时的呼吸频率是每分钟15-20次,脉搏是每分钟50-70次。下面给定一系列人的呼吸频率与脉搏,请你找出他们中间有可能在装睡的人,即至少一项指标不在正常范围内的人。

输入格式:

输入在第一行给出一个正整数N(<= 10)。随后N行,每行给出一个人的名字(仅由英文字母组成的、长度不超过3个字符的串)、其呼吸频率和脉搏(均为不超过100的正整数)。

输出格式:

按照输入顺序检查每个人,如果其至少一项指标不在正常范围内,则输出其名字,每个名字占一行。

输入样例:
4
Amy 15 70
Tom 14 60
Joe 18 50
Zoe 21 71
输出样例:
Tom
Zoe

代码:

import java.math.*;
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i = 0; i < n; i++){
            String str = in.next();
            int a = in.nextInt();
            int b = in.nextInt();
            if(a>20||a<15||b<50||b>70){
                System.out.println(str);
            }
        }
    }
}

L1-048:
给定两个矩阵A和B,要求你计算它们的乘积矩阵AB。需要注意的是,只有规模匹配的矩阵才可以相乘。即若A有Ra行、Ca列,B有Rb行、Cb列,则只有Ca与Rb相等时,两个矩阵才能相乘。

输入格式:

输入先后给出两个矩阵A和B。对于每个矩阵,首先在一行中给出其行数R和列数C,随后R行,每行给出C个整数,以1个空格分隔,且行首尾没有多余的空格。输入保证两个矩阵的R和C都是正数,并且所有整数的绝对值不超过100。

输出格式:

若输入的两个矩阵的规模是匹配的,则按照输入的格式输出乘积矩阵AB,否则输出“Error: Ca != Rb”,其中Ca是A的列数,Rb是B的行数。

输入样例1:
2 3
1 2 3
4 5 6
3 4
7 8 9 0
-1 -2 -3 -4
5 6 7 8
输出样例1:
2 4
20 22 24 16
53 58 63 28
输入样例2:
3 2
38 26
43 -5
0 17
3 2
-11 57
99 68
81 72
输出样例2:
Error: 2 != 3

代码:

#include <bits/stdc++.h>
using namespace std;
int i[105][105],j[105][105];
int sum[105][105];
int main()
{
    memset(sum,0,sizeof(sum));
    int Ra,Ca,Rb,Cb;
    scanf("%d%d",&Ra,&Ca);
    for(int a = 0; a < Ra; a ++)
    {
        for(int b = 0; b < Ca; b ++)
            scanf("%d",&i[a][b]);
    }
    scanf("%d%d",&Rb,&Cb);
    for(int a = 0; a < Rb; a ++)
    {
        for(int b = 0; b < Cb; b ++)
            scanf("%d",&j[a][b]);
    }
    if(Ca!=Rb)
        printf("Error: %d != %d",Ca,Rb);
    else
    {
        for(int a = 0; a < Ra; a ++)
        {
            for(int b = 0; b < Cb; b ++)
            {
                for(int c = 0; c < Rb; c ++)
                    sum[a][b]=sum[a][b]+i[a][c]*j[c][b];
            }
        }
        printf("%d %d\n",Ra,Cb);
    for(int a = 0; a < Ra; a ++)
    {
        for(int b = 0; b < Cb; b ++)
        {
            printf(b==Cb-1?"%d\n":"%d ",sum[a][b]);
        }
    }
    }
    return 0;
}

L:
L0450:

猜你喜欢

转载自blog.csdn.net/king_giji/article/details/79371893