[c/c++入门必刷题(二)]


目录

1051: 反转数

1052: 礼物数目  

1053: 分解质因数 

1054: 求a+aa+aaa+…aa…a(n个a) 

1055: 零花钱 

1056: n!的最高位 

1057: 小车位置 

1058: 乘积计算  

1059: 两个数的最大公约数 

1060: 两个数的最小公倍数 

1061: 九九乘法表(一) 

1062: 九九乘法表(二) 

1063: 字符统计(一) 

1064: 连续阶乘求和 

1065: 求式子的和 

1066: 水仙花数 

1067: 童年生活二三事 

1068: 韩信点兵 

1069: 小球蹦蹦跳 

1070: 十进制转二进制 

1071: 数列有序 

1072: 数组排序 

1073: 有序数组合并 

1074: 数组最小值 

1075: 最受欢迎歌手 

1076: 查找元素位置 

1077: 数字字符加倍 

1078: 报数字说英文 

1079: 二进制转十六进制 

1080: 单词统计 

1081: 最大字符串  

1082: 上三角矩阵 

1083: 矩阵乘积 

1084: 最多字母 

1085: 所在年第几天  

1086: 选美大赛 

1087: 杨辉三角 

1088: 矩阵对角线元素之和 

1089: 数组元素逆置 

1090: 字符统计(二) 

1091: 译密码(二) 

1092: The Decoder 

1093: Above Average 

1094: Summing Digits 

1095: Square Numbers 

1096: B2-Sequence 

1097: Quicksum 

1098: WERTYU 

1099: Integer Prefix 

1100: 素数表(函数专题) 


解题方法全部使用c++

1051: 反转数

题目描述

给定一个整数,请将该数各个数位上的数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零。

输入

一个十进制整数。

输出

对应的反转数。

样例输入

-690

样例输出

-96

源代码

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int ans = 0;
    while(abs(n) > 0)
    {
        ans = ans * 10 + n % 10;
        n /= 10;
    }
    cout<<ans;
    return 0;
}

1052: 礼物数目  

题目描述

小明买了许多圣诞礼物准备用于班级活动,回家后感觉太累了,便让机器人小灵帮忙数一下礼物一共有多少份。但是小灵不喜欢数字4,因此每次数到4时便跨过该数。例,若小灵数到339时,下一份礼物小灵就会数350。

输入

一个整数n,表示小灵给出的礼物的份数,1 < n < 1000,且一定不含整数4。

输出

一个整数为礼物的实际份数。

样例输入

55

样例输出

40

源代码

#include <iostream>
#include <cmath>
using namespace std;
bool check(int n)
{
    while(n > 0)
    {
        int num = n % 10;
        if(num == 4)return true;
        n /= 10;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;
    int ans = n;
    for(int i = 1;i <= n;i ++ )
    {
        if(check(i))ans--;
    }
    cout<<ans;
    return 0;
}

1053: 分解质因数 

题目描述

分解质因数

输入

一个正整数n。

输出

n的质因数的乘积形式

样例输入

36

样例输出

2*2*3*3

源代码

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n;
    cin >> n;
    if(n == 1)
    {
        cout<<'1';
        return 0;
    }
    int w = 2;
    int flag = 0;
    while(n != 1)
    {
        if(n % w == 0&&flag == 0)
        {
            flag = 1;
            cout<<w;
            n /= w;
        }
        else if(n % w == 0&&flag == 1)
        {
            cout<<'*'<<w;
            n /= w;
        }
        else if(n % w != 0)w ++ ;
    } 
    return 0;
}

1054: 求a+aa+aaa+…aa…a(n个a) 

题目描述

输入n和a,求a+aa+aaa+…aa…a(n个a),如当n=3,a=2时,2+22+222=246。

输入

包含两个整数,n和a,含义如上述,n和a都是小于10的非负整数。

输出

输出前n项和,单独占一行

样例输入

3 2

样例输出

246

源代码

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n,a;
    cin>>n>>a;
    int ans = 0;
    int item = a;
    for(int i = 1;i <= n;i ++ )
    {
        ans += item;
        item = item *10 + a;
    }
    cout<<ans;
    return 0;
}

1055: 零花钱 

题目描述

妈妈给了小明m元零花钱,为了鼓励小明节约,说如果小明每天只消费1元,每花k元就可以得到1元额外奖励,如果听妈妈的话小明最多可以花多少天? 

输入

输入2个整数m、k,(2 <= k<=m<= 1000)。

输出

输出一个整数,表示m元可以消费的天数。

样例输入

4 3

样例输出

5

源代码

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int m,k;
    cin >> m >> k;
    int ans = 0,cost = 0;
    while(m -- )
    {
        cost ++ ;
        ans ++ ;
        if(cost % k == 0)m ++ ;
    }
    cout<<ans;
    return 0;
}

1056: n!的最高位 

题目描述

输入一个正整数n。输出n!的最高位上的数字。  

输入

输入一个正整数n(n<=1000)。 

输出

输出n!的最高位上的数字。

样例输入

1000

样例输出

4

源代码

#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
    ll n;
    cin >> n;
    ll ans = 1;
    for(int i = 1;i <= n;i ++ )
    {
        ans = ans * i;
        if(ans > 100000000)ans /= 1000;
    }
    while(ans > 10)ans /= 10;
    cout<<ans;
    return 0;
}

1057: 小车位置 

题目描述

有一辆智能小车,最初(时间为0)的位置为(0,0),我们想知道它最后的位置。小车以每小时10公里的速度向北移动(北为y轴正向,东为x轴正向)。小车会收到一系列依照时间戳记排序的命令,1表示“向左转”,2表示“向右转”,3表示“停止”。每个命令的前面有一个时间戳记,所以知道该命令是何时发出的。最后一个命令一定是“停止”。另外假设,这辆小车非常灵活,它可以在瞬间转弯。

        例,小车在时间为5时收到一个“向左转”的命令1,在时间10收到一个“向右转”的命令2,在时间15收到一个“停止”的命令3。那么在最后时间15时,小车的位置将在(-50,100)。程序只要求输出小车最后的位置,第一个整数是x坐标,第二个整数是y坐标。

输入

输入包含多个命令,每个命令由整数time和command组成,表示在时刻time发出命令command。command的取值范围1-3,含义如上所述。

输出

输出占一行,包含两个整数,表示小车的最终位置。两个整数之间由空格隔开。

样例输入

5 1
10 2
15 3

样例输出

-50 100

源代码

#include <iostream>
using namespace std;
int main()
{
    int x = 0,y = 0;
    int command,curtime,pretime = 0,direction=0;
    while(cin>>curtime>>command)
    {
        switch(direction)
        {
            case 0:y = y + (curtime - pretime) * 10;break;
            case 1:x = x + (curtime - pretime) * 10;break;
            case 2:y = y - (curtime - pretime) * 10;break;
            case 3:x = x - (curtime - pretime) * 10;break;
        }
        switch(command)
        {
            case 1:direction = (direction + 3) % 4;break;
            case 2:direction = (direction + 1) % 4;break;
            case 3:break;
        }
        pretime = curtime;
    }
    cout<<x<<' '<<y;
    return 0;
}

1058: 乘积计算  

题目描述

做作业的时候,邻座的小朋友问你:“五乘以七等于多少?”你应该不失礼貌地微笑着告诉他:“五十三”。本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积。 

输入

输入在第一行给出两个不超过 1000 的正整数 A 和 B,其间以空格分隔。

输出

在一行中倒着输出 A 和 B 的乘积。

样例输入

50 7

样例输出

53

源代码

#include <iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    int sum = a * b,ans = 0;
    while(sum > 0)
    {
        ans = ans * 10 + sum % 10;
        sum /= 10; 
    }
    cout<<ans;
    return 0;
}

1059: 两个数的最大公约数 

题目描述

输入2个正整数a,b,求a与b的最大公约数。 

输入

2个正整数a,b,中间用空格隔开。(1<=a,b <= 10^4)

输出

输出a与b的最大公约数。

样例输入

6 15

样例输出

3

源代码

#include <iostream>
using namespace std;
int gcd(int a,int b)
{
    int c;
    do
    {
        c = a % b;
        if(c == 0)return b;
        a = b;
        b = c;
    }while(c != 0);
}
int main()
{
    int a,b;
    cin>>a>>b;
    int ans = gcd(a,b);
    cout<<ans;
    return 0;
}

1060: 两个数的最小公倍数 

题目描述

正整数a和正整数b的最小公倍数是指能被a和b整除的最小的正整数值,设计一个算法,求输入a和b的最小公倍数。 

输入

输入两个正整数a和b。

输出

输出a和b的最小公倍数

样例输入

5 3

样例输出

15

源代码

#include <iostream>
using namespace std;
int gcd(int a,int b)
{
    int c;
    do
    {
        c = a % b;
        if(c == 0)return b;
        a = b;
        b = c;
    }while(c != 0);
}
int lcd(int a,int b)
{
    int mul = a * b;
    return (mul)/(gcd(a,b));
}
int main()
{
    int a,b;
    cin>>a>>b;
    int ans = lcd(a,b);
    cout<<ans;
    return 0;
}

1061: 九九乘法表(一) 

题目描述

我们小时候学过的九九乘法表也许终生难忘,现在让我们重温这个美好的记忆,请编程输出九九乘法表.  

输入

输出

1*1=1
2*1=2   2*2=4
3*1=3   3*2=6   3*3=9
4*1=4   4*2=8   4*3=12  4*4=16
5*1=5   5*2=10  5*3=15  5*4=20  5*5=25
6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36
7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49
8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64
9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81

样例输入

样例输出

1*1=1
2*1=2   2*2=4
3*1=3   3*2=6   3*3=9
4*1=4   4*2=8   4*3=12  4*4=16
5*1=5   5*2=10  5*3=15  5*4=20  5*5=25
6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36
7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49
8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64
9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81

源代码

#include <iostream>
using namespace std;
int main()
{
    for(int i = 1;i <= 9;i ++ )
    {
        for(int j = 1;j <= i;j ++ )
        {
            printf("%d*%d=%d ",i,j,i*j);
        }
        cout<<endl;
    }
    return 0;
}

1062: 九九乘法表(二) 

题目描述

请按要求输出九九乘法表。要求所输出它的格式与平常的不同,是那种反过来的三角形,可不要看错了! 

输入

第一行有一个整数,表示有n组数据。(n<10)
接下来有n行,每行只有一个整数m(1<=m<= 9)。

输出

对应每个整数m,根据要求输出乘法表的前m行,具体格式参见输入样例.
每两组测试数据结果之间有一个空行隔开,具体如输出样例。

样例输入

3
2
1
5

样例输出

1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9   
2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18   
    
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9    
     
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9    
2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18   
3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27   
4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36   
5*5=25 5*6=30 5*7=35 5*8=40 5*9=45  

源代码

#include <iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i = 1;i <= n;i ++ )
        {
            for(int j = i;j <= 9;j ++ )
            {
                printf("%d*%d=%d ",i,j,i*j);
            }
            cout<<endl;
        }
        cout<<endl;
    }
    return 0;
}

1063: 字符统计(一) 

题目描述

输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数,分行输出该结果。

输入

一行字符,可以包含字母、数字、空格、标点等符号

输出

分行输出大小写英文字母、空格、数字和其他字符的个数。

如:

characters=字母个数

spaces=空格个数

numbers=数字个数

others=其他字符个数

样例输入

My input123 @%chars.

样例输出

characters=12
spaces=2
numbers=3
others=3

源代码

#include <iostream>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    int a = 0,b = 0,c = 0,d = 0;
    for(int i = 0;i < s.size();i ++ )
    {
        if((s[i] >= 'a'&&s[i] <= 'z')||(s[i] >= 'A'&&s[i] <= 'Z'))a ++ ;
        else if(s[i] == ' ')b ++ ;
        else if(s[i] >= '0'&&s[i] <= '9')c ++ ;
        else d ++ ;
    }
    cout<<"characters="<<a<<endl;
    cout<<"spaces="<<b<<endl;
    cout<<"numbers="<<c<<endl;
    cout<<"others="<<d<<endl;
    return 0;
}

1064: 连续阶乘求和 

题目描述

输入一个正整数N,求1!+2!+3!+...+N!,即求
 
 
提示:由于>10的整数的阶乘非常大,已超出整数的表示范围,故求阶乘时,需要将变量定义为double类型。 

输入

输出

1~N连续阶乘的和,1!+2!+3!+...+N!的和,末尾换行。

样例输入

10

样例输出

4037913

源代码

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    double ans = 0;
    for(int i = 1;i <= n;i ++ )
    {
        double mul = 1;
        for(int j = 1;j <= i;j ++ )
        {
            mul = mul * j;
        }
        ans = ans + mul;
    }
    printf("%.0lf",ans);
    return 0;
}

1065: 求式子的和 

题目描述

求如下式子的和

请将结果定义为double类型。

注意求平方,不要用C数学库中提供的函数pow。

输入

输出

小数点后保留6位小数,末尾换行。 

样例输入

样例输出

47977.928968

源代码

#include <iostream>
using namespace std;
int main()
{
    double ans = 0;
    for(int i = 1;i <= 100;i ++ )ans += i;
    for(int i = 1;i <= 50;i ++ )ans += i*i;
    for(int i = 1;i <= 10;i ++ )ans += (1.0)/(i * 1.0);
    printf("%.6lf",ans);
    return 0;
}

1066: 水仙花数 

题目描述

春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,它是这样定义的:“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+33。请输出所有的“水仙花数”。 

输入

输出

每行输出一个水仙花数。

样例输入

样例输出

153
370
371
407

源代码

#include <iostream>
using namespace std;
bool check(int n)
{
    int t = n; 
    int sum = 0;
    while(t > 0)
    {
        int num = t % 10;
        sum += num * num * num;
        t /= 10;
    }
    if(sum == n)return true;
    else return false;
}
int main()
{
    for(int i = 100;i < 1000;i ++ )
    {
        if(check(i))cout<<i<<endl;
    }
    return 0;
}

1067: 童年生活二三事 

题目描述

Redraiment小时候走路喜欢蹦蹦跳跳,他最喜欢在楼梯上跳来跳去。 但年幼的他一次只能走上一阶或者一下子蹦上两阶。 现在一共有N阶台阶,请你计算一下Redraiment从第0阶到第N阶共有几种走法。 

输入

输入包括多组数据。 每组数据包括一行:N(1≤N≤40)。 输入以0结束。

输出

对应每个输入包括一个输出。 为redraiment到达第n阶不同走法的数量。

样例输入

1
2
0

样例输出

1
2

源代码

#include <iostream>
using namespace std;
int fun(int n)
{
    if(n == 1)return 1;
    if(n == 2)return 2;
    if(n >= 3)return fun(n-1)+fun(n-2); 
}
int main()
{
    int n;
    while(cin>>n,n != 0)
    {
        cout<<fun(n)<<endl;
    }
    return 0;
}

1068: 韩信点兵 

题目描述

淮安民间传说着一则故事——“韩信点兵”,相关成语“韩信点兵,多多益善”。韩信从不直接清点自己军队的人数,只要让士兵先后以三人一排、五人一排、七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了。
输入3个非负整数a,b,c ,表示每种队形排尾的人数(a<3,b<5,c<7),输出总人数的最小值(或报告无解)。已知总人数不小于10,不超过100 。 

输入

输入3个非负整数 ,表示每种队形排尾的人数(a<3,b<5,c<7)。例如,输入:2 4 5

输出

输出总人数的最小值(或报告无解,即输出No answer)。实例,输出:89 

样例输入

2 1 6

样例输出

41

源代码

#include <iostream>
using namespace std;
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    for(int i = 10;i <= 100;i ++ )
    {
        if(i % 3 == a && i % 5 == b && i % 7 == c)
        {
            cout<<i;
            return 0;
        }
    }
    cout<<"No answer";
    return 0;
}

1069: 小球蹦蹦跳 

题目描述

调皮的小明将皮球从100m的高度自由落下,每次落地后反弹回原高度的一半,再落下,再反弹。

求它在第N次落地时,共经过了多少米,第N次反弹多高。

输入

一个正整数N,表示球落地的次数。

输出

length=球第N次落地时所经过了距离

high=球第N次落地反弹的高度

小数点后保留4位小数。

注意:末尾输出换行。

样例输入

10

样例输出

length=299.6094
high=0.0977

源代码

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    int n;
    cin >> n;
    double h = 100,l = 100;
    for(int i = 1;i <= n;i ++ )
    {
        if(i != n)l = l + h;
        h = h * 0.5;
    }
    cout<<setiosflags(ios::fixed)<<setprecision(4)<<"length="<<l<<endl<<"high="<<h<<endl;
    return 0;
}

1070: 十进制转二进制 

题目描述

输入一个非负十进制整数,将其转换为二进制形式输出。 

输入

一个非负整数n (0<=n<2^31)。

输出

对应的二进制形式。

样例输入

7

样例输出

111

源代码

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    if(n == 0)
    {
        cout<<"0";
        return 0;
    }
    string s;
    while(n > 0)
    {
        int num = n % 2;
        char c = num + '0';
        s = s + c;
        n /= 2;
    }
    reverse(s.begin(),s.end());
    cout<<s;
    return 0;
}

1071: 数列有序 

题目描述

一个非递减有序的整型数组有n个元素,给定一个整数num,将num插入该序列的适当位置,使序列仍保持非递减有序。 

输入

输入有三行。第一行是一个正整数n(n<=1000)。第二行是n个整数,第三行是待插入整数num。

输出

输出非递减有序的n+1个整数,数据之间用空格隔开。输出占一行。

样例输入

5
1 2 4 5 6
3

样例输出

1 2 3 4 5 6

源代码

#include <iostream>
using namespace std;
const int N = 1000000+10;
int a[N]; 
int main()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++ )cin>>a[i];
    int num;
    cin >> num;
    for(int i = 1;i <= n;i ++ )
    {
        if(a[i] >= num)
        {
            for(int j = n + 1;j >= i + 1;j -- )
            {
                a[j] = a[j - 1];
            }
            a[i] = num;
            break;
        }
    }
    for(int i = 1;i <= n + 1;i ++ )cout<<a[i]<<' ';
    return 0;
}

1072: 数组排序 

题目描述

对一维数组按照从小到大的顺序排序。

输入

第一行输入一个整数n(1<=n<=100)表示数组有n个整数;第二行输入n个整数。

输出

对这n个整数数按照从小到大的顺序输出,数据之间用一个空格隔开。

样例输入

6
6 5 1 2 3 4

样例输出

1 2 3 4 5 6

源代码

#include <iostream>
using namespace std;
const int N = 100000+10;
int a[N]; 
int main()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++ )cin>>a[i];
    for(int i = 1;i <= n;i ++ )
    {
        for(int j = i;j <= n;j ++ )
        {
            if(a[i] > a[j])swap(a[i],a[j]);
        }
    }
    for(int i = 1;i <= n;i ++ )cout<<a[i]<<' ';
    return 0;
}

1073: 有序数组合并 

题目描述

已知数组a中有m个按升序序排列的元素,数组b中有n个降序排列的元素,编程将a与b中的所有元素按降序存入数组c中。 

输入

输入有两行,第一行首先是一个正整数m,然后是m个整数;第二行首先是一个正整数n,然后是n个整数,m, n均小于等于1000000。

输出

输出合并后的m+n个整数,数据之间用空格隔开。输出占一行。

样例输入

4 1 2 5 7
3 6 4 2

样例输出

7 6 5 4 2 2 1

源代码

#include <iostream>
using namespace std;
const int N = 100000+10;
int a[N],b[N],c[N]; 
int main()
{
    int m,n;
    cin>>m;
    for(int i = 1;i <= m;i ++ )cin>>a[i];
    cin>>n;
    for(int i = 1;i <= n;i ++ )cin>>b[i];
    int i = m,j = 1,k = 1;
    while(i >= 1 && j <= n)
    {
        if(a[i] >= b[j])c[k ++ ] = a[i -- ];
        else c[k ++ ] = b[j ++ ] ;
    }
    while(i >= 1)c[k ++ ] = a[i -- ];
    while(j <= n)c[k ++ ] = b[j ++ ];
    for(int i = 1;i <= m + n;i ++ )cout<<c[i]<<' '; 
    return 0;
}

1074: 数组最小值 

题目描述

数组a有n个元素,请输出n个元素的最小值及其下标。若最小值有多个,请输出下标最小的一个。注意,有效下标从0开始 

输入

输入分两行,第一行是一个正整数n(n<=1000),第二行是n个整数。

输出

输出占一行。输出数组的最小值及其下标,用空格隔开。

样例输入

5
8 4 5 1 2

样例输出

1 3

源代码

#include <iostream>
using namespace std;
const int N = 100000+10;
int a[N]; 
int main()
{
    int n;
    cin>>n;
    int minnum = 1e6;
    for(int i = 0;i < n;i ++ )
    {
        cin>>a[i];
        minnum = min(a[i],minnum);
    }
    for(int i = 0;i < n;i ++ )
    {
        if(a[i] == minnum)
        {
            cout<<minnum<<" "<<i;
            return 0;
        }
    }
    return 0;
}

1075: 最受欢迎歌手 

题目描述

学校推出了10名歌手,每个歌手都有唯一编号。校学生会想知道这些歌手受欢迎的程度,设了一个投票箱,让每一个同学给自己喜欢的歌手投票,同学们使用歌手编号进行投票。现在学生会找你帮忙统计一下每位歌手获得的票数,并颁发“最受欢迎歌手奖”,该奖项颁发给得票最多的歌手。若有多名歌手并列第一,则均可获奖。 

输入

输入若干个整数,表示投票的歌手编号,以一个负数作为输入结束的标志。

输出

出现次数最多的编号。若有多个则按从小到大顺序输出选手编号(用空格隔开)。

样例输入

4 5 3 1 3 4 2 7 -1

样例输出

3 4

源代码

#include <iostream>
using namespace std;
const int N = 10000+10;
int a[N]; 
int main()
{
    int num;
    while(cin >> num,num != -1)a[num] ++ ;
    int maxnum = -1e6;
    for(int i = 0;i <= 10;i ++ )
    {
        maxnum = max(maxnum,a[i]);
    }
    for(int i = 0;i <= 10;i ++ )
    {
        if(a[i] == maxnum)cout<<i<<' ';
    }
    return 0;
}

1076: 查找元素位置 

题目描述

输入从小到大排好序的n个元素,找出某元素第一次出现的位置

输入

输入分三行,第一行是一个正整数n(n<=1e6),第二行是n个整数,第三行为一个整数 x表示待查找的元素。

输出

如果 x 在序列中,则输出 x 第一次出现的位置,否则输出-1。

样例输入

5
3 5 6 6 7
6

样例输出

3

源代码

#include <iostream>
using namespace std;
const int N = 10000+10;
int a[N]; 
int main()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++ )cin>>a[i];
    int num;
    cin >> num;
    for(int i = 1;i <= n;i ++ )
    {
        if(a[i] == num)
        {
            cout<<i;
            return 0;
        }
    }
    cout<<"-1";
    return 0;
}

1077: 数字字符加倍 

题目描述

输入一个以回车结束的字符串,该字符串由数字和字母组成。请过滤掉所有非数字字符,然后将数字字符串转换成十进制整数后乘以2输出。
 

输入

输入一个以回车符结束的字符串,长度不超过100,由数字和字母组成。

输出

将转换后的整数乘以2输出,测试数据保证结果在整数范围内。

样例输入

sg987aa65t498

样例输出

197530996

源代码

#include <iostream>
using namespace std;
typedef long long ll; 
int main()
{
    string s;
    cin>>s;
    ll num = 0;
    for(int i = 0;i < s.size();i ++ )
    {
        if(s[i] >= '0' && s[i] <= '9')
        {
            num = num * 10 + (s[i] - '0');
        }
    }
    cout<<num*2;
    return 0;
}

1078: 报数字说英文 

题目描述

输入一个1到7之间的数字,表示星期一到星期日,输出相应的英文:Mon、Tue、Wed、Thur、Fri、Sat、Sun。

输入

输入一个1到7之间的数字。

输出

输出与数字对应的英文。

样例输入

6

样例输出

Sat

源代码

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    switch(n)
    {
        case 1 :cout<<"Mon";break;
        case 2 :cout<<"Tue";break;
        case 3 :cout<<"Wed";break;
        case 4 :cout<<"Thur";break;
        case 5 :cout<<"Fri";break;
        case 6 :cout<<"Sat";break;
        case 7 :cout<<"Sun";break;
    }
    return 0;
}

1079: 二进制转十六进制 

题目描述

输入一个0和1字符组成的二进制字符串,请转换成十六进制。 

输入

输入一个0/1字符串,长度小于100000。

输出

输出1行,即转换后的十六进制

样例输入

11010100101

样例输出

6A5

源代码

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int num = 0;
    int w = 1;
    for(int i = s.size() - 1;i >= 0;i -- )
    {
        num = num + (s[i] - '0') * w;
        w = w * 2;
    }
    string ans;
    while(num > 0)
    {
        int t = num % 16;
        if(t >= 0 &&t <= 9)ans += (t + '0');
        else if (t >= 10 && t <= 15)
        {
            ans += (t - 10 + 'A');
        }
        num /= 16;
    }
    reverse(ans.begin(),ans.end());
    cout<<ans;
    return 0;
}

1080: 单词统计 

题目描述

输入一行字符,统计其中有多少个单词,单词之间用空格分隔开。 

输入

输入一个以空格分隔的字符串。

输出

字符串中的单词个数。

样例输入

I love c++ programming

样例输出

4

源代码

#include <iostream>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    int head;
    int ans = 0;
    for(int i = 0;i < s.size();i ++ )
    {
        if(s[i] != ' ')
        {
            head = i;
            break;
        }
    }
    if(s[s.size() - 1] != ' ')s = s + ' ';
    for(int i = head;i < s.size();i ++ )
    {
        int j = i + 1;
        while(s[j] != ' ')j ++ ;
        ans ++ ;
        i = j;
    }
    cout<<ans;
    return 0;
}

1081: 最大字符串  

题目描述

输入三个字符串,输出其中按字典序最大的字符串。 

输入

输入三个字符串。

输出

输出其中最大的字符串。

样例输入

beijing shanghai guangzhou

样例输出

shanghai

源代码

#include <iostream>
using namespace std;
int main()
{
    string a,b,c;
    cin>>a>>b>>c;
    string ans = max(a,max(b,c));
    cout<<ans;
    return 0;
}

1082: 上三角矩阵 

题目描述

输入一个正整数n和n阶方阵a中的元素,如果a是上三角矩阵则输出“YES”,否则输出“NO”。上三角矩阵的主对角线(不包含主对角线)以下元素均为0。 

输入

输入一个正整数n(1<=n<=10)和n阶方阵a中的元素,均为整数。

输出

若a是上三角矩阵则输出“YES”,否则输出“NO”。

样例输入

4
1 2 3 4
0 5 6 7
0 0 8 9
0 0 0 10

样例输出

YES

源代码

#include <iostream>
using namespace std;
const int N = 1000;
int a[N][N];
int main()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++ )
    {
        for(int j = 1;j <= n;j ++ )
        {
            cin>>a[i][j];
        }
    }
    int flag = 1;
    for(int i = 1;i <= n;i ++ )
    {
        for(int j = i;j <= n;j ++ )
        {
            if(a[i][j] == 0)
            {
                flag = 0;
            }
        }
    }
    for(int i = 1;i <= n;i ++ )
    {
        for(int j = 1;j <= i;j ++ )
        {
            if(i == j)continue;
            else if(a[i][j] != 0)
            {
                flag = 0;
            }
        }
    }
    if(flag == 1)cout<<"YES";
    else if(flag == 0)cout<<"NO";
    return 0;
}

1083: 矩阵乘积 

题目描述

计算两个矩阵A和B的乘积

输入

第一行三个正整数m、p、n(0<=M,N,P<=10),表示矩阵A是m行p列,矩阵B是p行n列;接下来的m行是矩阵A的内容,每行 p个整数,以空格隔开;最后的p行是矩阵B的内容,每行n个整数,以空格隔开。

输出

输出乘积矩阵,输出占m行,每行n个数据,以空格隔开。

样例输入

2 3 4

1 0 1
0 0 1

1 1 1 3
4 5 6 7
8 9 1 0

样例输出

9 10 2 3
8 9 1 0

源代码

#include<stdio.h>
int main()
{
    int a[20][20],b[20][20],c[20][20];//数组开辟足够内存 
    int m,p,n,i,j,k;
    scanf("%d%d%d",&m,&p,&n);
    for(i=0;i<m;i++)
    {
        for(j=0;j<p;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    for(i=0;i<p;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            c[i][j]=0;
            for(k=0;k<p;k++)
            {
                c[i][j]=c[i][j]+a[i][k]*b[k][j];
            }
        }
    }
    for(i=0;i<m;i++)//输出格式一定要正确 
    {
        for(j=0;j<n;j++)
        {
            if(j==0)printf("%d",c[i][j]);
            else printf(" %d",c[i][j]);
        }
        printf("\n");
    }
    return 0;
}

1084: 最多字母 

题目描述

输入一个字符串,输出字符串中出现次数最多的字母

输入

输入一个只含有大小写字母和空格的字符串,长度不超过100,以回车结束。

输出

输出一个小字字母,表示该字符串中出现次数最多的字母。若有多个则只输出ASCII码最小的那个字母。

样例输入

A ab abc

样例输出

a

源代码

#include <iostream>
using namespace std;
int a[26]={0};
int main()
{
    string s;
    getline(cin,s);
    for(int i = 0;i < s.size();i ++ )
    {
        if(s[i] >= 'A' && s[i] <= 'Z')
        {
            s[i] += 32;
            int idx = s[i] - 'a';
            a[idx] ++ ;
        }
        else if(s[i] >= 'a' && s[i] <= 'z')
        {
            int idx = s[i] - 'a';
            a[idx] ++ ;
        }
    }
    int maxnum = 0;
    for(int i = 0;i < 26;i ++ )
    {
        maxnum = max(maxnum,a[i]);
    }
    for(int i = 0;i < 26;i ++ )
    {
        if(maxnum == a[i])
        {
            cout<<char(i + 'a');
            break;
        }
    }
    return 0;
}

1085: 所在年第几天  

题目描述

输入一个日期,输出该日期是所在年的第几天。 

输入

输入一个日期。

输出

输出该日期是所在年的第几天。

样例输入

2021 1 29

样例输出

29

源代码

#include <iostream>
using namespace std;
int a[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
int b[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
    int year,month,day;
    cin>>year>>month>>day;
    int ans = 0;
    if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))
    {
        for(int i = 1;i <= month - 1;i ++ )
        {
            ans = ans + a[i]; 
        }
        ans += day;
        cout<<ans;
    }
    else
    {
        for(int i = 1;i <= month - 1;i ++ )
        {
            ans = ans + b[i];
        }
        ans += day;
        cout<<ans;
    }
    return 0;
}

1086: 选美大赛 

题目描述

某地区选美大赛总共有 n 个选手(编号从1到 n ), m 个评委。每个评委只能拿到一张选票,每张选票可为编号 L 到 R 的选手加上一分。现在让您找出得分最高的选手。 

输入

某地区选美大赛总共有 n 个选手(编号从1到 n ), m 个评委。每个评委只能拿到一张选票,每张选票可为编号 L 到 R 的选手加上一分。现在让您找出得分最高的选手。

输出

得分最高的选手编号,若有多位选手得最高分则按递增顺序输出每个选手的编号(各选手编号间以空格分隔,注意不要有行末空格)。

样例输入

5 8
2 3
2 4
3 5
4 4
2 4
3 3
4 5
2 3

样例输出

3

源代码

#include <iostream>
using namespace std;
const int N = 100000+10;
int a[N],b[N];
void insert(int l,int r,int c)
{
    b[l] += c;
    b[r + 1] -= c; 
} 
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i = 1;i <= n;i ++ )insert(i,i,0);
    while(m -- ) 
    {
        int l,r;
        cin>>l>>r;
        insert(l,r,1);
    }
    for(int i = 1;i <= n;i ++ )b[i] = b[i] + b[i - 1];
    int maxnum = 0;
    for(int i = 1;i <= n;i ++ )
    {
        maxnum = max(maxnum,b[i]);
    }
    int flag = 0;
    for(int i = 1;i <= n;i ++ )
    {
        if(b[i] == maxnum && flag == 0)
        {
            flag = 1;
            cout<<i;
        }
        else if(b[i] == maxnum && flag == 1)
        {
            cout<<' '<<i;
        }
    }
    return 0;
}

1087: 杨辉三角 

题目描述

输出杨辉三角形。 

输入

第一行输入一个整数 n (1<=n<=10)。

输出

输出杨辉三角形的前n行,每个数字占8格左对齐。

样例输入

4

样例输出

1       
1       1       
1       2       1       
1       3       3       1   

源代码

#include <iostream>
using namespace std;
const int N = 1000+10;
int a[N][N];
int main()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++ )
    {
        for(int j = 1;j <= i;j ++ )
        {
            if(i == 1)a[i][j] = 1;
            else if(i == j)a[i][j] = 1;
            else
            {
                a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
            }
        }
    }
    for(int i = 1;i <= n;i ++ )
    {
        for(int j = 1;j <= i;j ++ )
        {
            printf("%-8d",a[i][j]);
        }
        cout<<endl;
    }
    return 0;
}

1088: 矩阵对角线元素之和 

题目描述

求一个3×3的整型矩阵对角线元素之和。 

输入

从键盘上输入3×3的整型矩阵,要求输入3行,每行3个整数,每个数之间以空格分隔。

输出

对角线元素之和,行尾换行。

样例输入

1 2 3
4 5 6
9 8 7

样例输出

13

源代码

#include <iostream>
using namespace std;
const int N = 1000+10;
int a[N][N];
int main()
{
    int ans = 0;
    for(int i = 1;i <= 3;i ++ )
    {
        for(int j = 1;j <= 3;j ++ )
        {
            cin >> a[i][j];
            if(i == j)ans += a[i][j];
        }
    }
    cout<<ans<<endl;
    return 0;
}

1089: 数组元素逆置 

题目描述

将一个长度为10的整型数组中的值按逆序重新存放。

如:原来的顺序为1,2,3,4,5,6,7,8,9,0,要求改为0,9,8,7,6,5,4,3,2,1

输入

从键盘上输入以空格分隔的10个整数。

输出

按相反的顺序输出这10个数。

样例输入

1 2 3 4 5 6 7 8 9 0

样例输出

0 9 8 7 6 5 4 3 2 1

源代码

#include <iostream>
using namespace std;
const int N = 100000;
int a[N];
int main()
{
    for(int i = 1;i <= 10;i ++ )cin>>a[i];
    int i = 1,j = 10;
    while(i < j)
    {
        swap(a[i ++ ],a[j -- ]);
    }
    for(int i = 1;i <= 10;i ++ )cout<<a[i]<<' ';
    return 0;
}

1090: 字符统计(二) 

题目描述

有一篇文章,共3行文字,每行不到80个字符。要求分别统计每行中字母、数字、空格以及其他字符的个数。 

输入

从键盘上输入3行字符,每行不到80个字符。

输出

输出信息占3行

每行输出4个整数,分别表示该行字符中字母、数字、空格以及其他字符的个数,数字之间以空格分隔。

样例输入

I am a student!
I'm 18 years old.
One Two Three 1 2 3!@#

样例输出

11 0 3 1
10 2 3 2
11 3 5 3

源代码

#include <iostream>
using namespace std;
int main()
{
    for(int o = 1;o <= 3;o ++ )
    {
        string s;
        getline(cin,s);
        int a = 0,b = 0,c = 0,d = 0;
        for(int i = 0;i < s.size();i ++ )
        {
            if((s[i] >= 'a'&&s[i] <= 'z')||(s[i] >= 'A'&&s[i] <= 'Z'))a ++ ;
            else if(s[i] == ' ')b ++ ;
            else if(s[i] >= '0'&&s[i] <= '9')c ++ ;
            else d ++ ;
        }
        cout<<a<<" "<<c<<" "<<b<<" "<<d<<endl;
    }
     
    return 0;
}

1091: 译密码(二) 

题目描述

有一行电文,已按如下规律译成密码:

A-->Z        a-->z

B-->Y        b-->y

C-->X        c-->x

......          ......

即第一个字母变成第26个字母,第i个字母变成第(26-i+1)个字母,非字母字符不变。要求根据密码译回原文,并输出。

输入

输入一行密文。

输出

解密后的原文,单独占一行。

样例输入

ZYX123zyx

样例输出

ABC123abc

源代码

#include <iostream>
using namespace std;
char a[27] = {' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char b[27] = {' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int main()
{
    string s;
    getline(cin,s);
    for(int i = 0;i < s.size();i ++ )
    {
        if(s[i] >= 'a' &&s[i] <= 'z')
        {
            int num = s[i] - 'a' + 1;
            s[i] = a[(26 - num + 1)];
        }
        else if(s[i] >= 'A' && s[i] <= 'Z')
        {
            int num = s[i] - 'A' + 1;
            s[i] = b[(26 - num + 1)];
        } 
        else continue;
    }
    cout<<s;
    return 0;
}

1092: The Decoder 

题目描述

Write a complete program that will correctly decode a set of characters into a valid message. Your program should read a given file of a simple coded set of characters and print the exact message that the characters contain. The code key for this simple coding is a one for one character substitution based upon a single arithmetic manipulation of the printable portion of the ASCII character set. 

输入

For example: with the input file that contains:

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

输出

your program should print the message:

*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.

样例输入

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

样例输出

*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.

源代码

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    string a;
    while(cin>>a)
    {
        if(a=="1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5")
        {
            printf("*CDC is the trademark of the Control Data Corporation.\n");
        }
        else if(a=="1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5")
        {
            printf("*IBM is a trademark of the International Business Machine Corporation.\n");
        }
        else if(a=="1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5")
        {
            printf("*DEC is the trademark of the Digital Equipment Corporation.");
        }
    }
    return 0;
}

1093: Above Average 

题目描述

It is said that 90% of frosh expect to be above average in their class. You are to provide a reality check. 

输入

The first line of standard input contains an integer C, the number of test cases. C data sets follow. Each data set begins with an integer, N, the number of people in the class (1 <= N <= 1000). N integers follow, separated by spaces or newlines, each giving the final grade (an integer between 0 and 100) of a student in the class. 

输出

For each case you are to output a line giving the percentage of students whose grade is above average, rounded to 3 decimal places.

样例输入

5
5 50 50 70 80 100
7 100 95 90 80 70 60 50
3 70 90 80
3 70 90 81
9 100 99 98 97 96 95 94 93 91

样例输出

40.000%
57.143%
33.333%
66.667%
55.556%

源代码

#include<iostream>
using namespace std;
#define N 1000 
int main()
{
    int n;
    scanf("%d",&n);
    char a='%';
    for(int i=1;i<=n;i++)
    {
        int m,a[N];
        scanf("%d",&m);
        double average=0,sum=0;
        for(int j=0;j<m;j++)
        {
            scanf("%d",&a[j]);
            sum=sum+a[j];
        }
        average=sum/m*1.0;
        double percent=0;
        for(int k=0;k<m;k++)
        {
            if(a[k]>average)percent++;
        }
        percent=percent/m*100;
        printf("%.3lf",percent);
        cout<<'%'<<endl;
    }
    return 0;
}

1094: Summing Digits 

题目描述

For a positive integer n, let f (n) denote the sum of the digits of n when represented in base 10. It is easy to see that the sequence of numbers n, f (n), f (f (n)), f (f (f (n))),... eventually becomes a single digit number that repeats forever. Let this single digit be denoted g (n).
 For example, consider n = 1234567892. Then:
    f(n) = 1+2+3+4+5+6+7+8+9+2 = 47
    f(f(n)) = 4 + 7 = 11
    f(f(f(n))) = 1 + 1 = 2
    Therefore, g(1234567892) = 2. 

输入

Each line of input contains a single positive integer n at most 2,000,000,000. Input is terminated by n = 0 which should not be processed.

输出

For each such integer, you are to output a single line containing g(n).

样例输入

2
11
47
1234567892
0

样例输出

2
2
2
2

源代码

#include <iostream>
typedef long long ll;
using namespace std;
ll f(ll n)
{
    ll sum=0,ans;
    while(n>0)
    {
        sum=sum+n%10;
        n/=10;
    }
    if(sum>=10)ans=f(sum);
    else ans=sum;
    return ans;
}
int main()
{
    ll n;
    while(~scanf("%lld",&n)&&n!=0)
    {
        ll ans=f(n);
        printf("%lld\n",ans);
    }
    return 0;
}

1095: Square Numbers 

题目描述

A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive). 

输入

The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0<a≤b≤100000). Input is terminated by a line containing two zeroes. This line should not be processed.

输出

For each line of input produce one line of output. This line contains an integer which denotes how many square numbers are there between a and b (inclusive).

样例输入

1 4
1 10
0 0

样例输出

2
3

源代码

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int a,b;
    while(~scanf("%d%d",&a,&b)&&(a!=0&&b!=0))
    {
        int num=0;
        for(int i = a;i <= b;i ++ )
        {
            if(i*i<=b)num++;
        }
        printf("%d\n",num);
    }
    return 0;
}

1096: B2-Sequence 

题目描述

A B2-Sequence is a sequence of positive integers 1≤b1<b2<b3...such that all pairwise sums bi+bj ,where i≤ j, are different.
Your task is to determine if a given sequence is a B2-Sequence or not. 

输入

Each test case starts with 2 < N < 100, the number of elements in a sequence. Next line will have N integers, representing the value of each element in the sequence. Each element bi is an integer such that bi≤ 10000. There is a blank line after each test case. The input is terminated by end of file (EOF).

输出

For each test case you must print the number of the test case, starting from 1, and a message indicating if the corresponding sequence it is a B2-Sequence or not. See the sample output below. After each test case you must print a blank line.

样例输入

4
1 2 4 8
4
3 7 10 14

样例输出

Case #1: It is a B2-Sequence.

Case #2: It is not a B2-Sequence.

源代码

#include <iostream>
using namespace std;
const int N = 1000000+10;
int a[N];
int num=1;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0;i < n;i ++ )scanf("%d",&a[i]);
        if(a[0]!=1)
        {
            printf("Case #%d: It is not a B2-Sequence.\n",num);
            printf("\n");
            num++;
        }
        else
        {
            int flag=1;
            for(int i = 0;i < n-1;i ++ )
            {
                if(a[i]>a[i+1])
                {
                    flag=0;
                    break;
                }
            }
            if(flag==0)
            {
                printf("Case #%d: It is not a B2-Sequence.\n",num);
                printf("\n");
                num++;
            }
            if(flag==1)
            {
                printf("Case #%d: It is a B2-Sequence.\n",num);
                printf("\n");
                num++;
            }
        }
    }
}

1097: Quicksum 

题目描述

A checksum is an algorithm that scans a packet of data and returns a single number. The idea is that if the packet is changed, the checksum will also change, so checksums are often used for detecting transmission errors, validating document contents, and in many other situations where it is necessary to detect undesirable changes in data.
For this problem, you will implement a checksum algorithm called Quicksum. A Quicksum packet allows only uppercase letters and spaces. It always begins and ends with an uppercase letter. Otherwise, spaces and letters can occur in any combination, including consecutive spaces.

A Quicksum is the sum of the products of each character's position in the packet times the character's value. A space has a value of zero, while letters have a value equal to their position in the alphabet. So, A=1, B=2, etc., through Z=26. Here are example Quicksum calculations for the packets "ACM" and "MID CENTRAL":

        ACM: 1*1  + 2*3 + 3*13 = 46 

MID CENTRAL: 1*13 + 2*9 + 3*4 + 4*0 + 5*3 + 6*5 + 7*14 + 8*20 + 9*18 + 10*1 + 11*12 = 650 

输入

The input consists of one or more packets followed by a line containing only # that signals the end of the input. Each packet is on a line by itself, does not begin or end with a space, and contains from 1 to 255 characters.

输出

For each packet, output its Quicksum on a separate line in the output.

样例输入

ACM
MID CENTRAL
REGIONAL PROGRAMMING CONTEST
ACN
A C M
ABC
BBC
#

样例输出

46
650
4690
49
75
14
15

源代码

#include <iostream>
#include <string.h>
using namespace std;
const int N = 10000+10;
int main()
{
    char a[N];
    while(cin.getline(a,512))
    {
        if(a[0]=='#')break;
        int sum=0;
        for(int i = 0;i < strlen(a);i ++ )
        {
            if(a[i]==' ')sum+=0;
            else sum+=(i+1)*(a[i]-'A'+1);
        }
        printf("%d\n",sum);
    }
}

1098: WERTYU 

题目描述

把手放在键盘上时,稍不注意就会往右错一位。这样,输入Q会变成输入W,输入J会变成输入K等。输入一个错位后敲出的字符串(所有字母均大写),请你对以这种方式中键入的消息进行码。

输入

输入包含若干行文本,每一行包含如数字、空格、大写字母(除Q、A和Z之外),或如上图所示的(除反引号 · 之外)。用单词标记的键(Tab键、Backspace键等)不在输入中。

输出

对于输入的每个字母或标点符号,用图所示的QWERTY键盘上左边的键的内容来替代。输入中的空格也显示在输出中。

样例输入

O S, GOMR YPFSU/

样例输出

I AM FINE TODAY.

源代码

#include <iostream>
#include <string.h>
using namespace std;
const int N = 10000+10;
int main()
{
    char a[N];
    cin.getline(a,512);
    for(int i = 0;i < strlen(a);i ++ )
    {
        switch(a[i])
        {
            case 'W':a[i]='Q';break;
            case 'S':a[i]='A';break;
            case 'X':a[i]='Z';break;
            case 'E':a[i]='W';break;
            case 'D':a[i]='S';break;
            case 'C':a[i]='X';break;
            case 'R':a[i]='E';break;
            case 'F':a[i]='D';break;
            case 'V':a[i]='C';break;
            case 'T':a[i]='R';break;
            case 'G':a[i]='F';break;
            case 'B':a[i]='V';break;
            case 'Y':a[i]='T';break;
            case 'H':a[i]='G';break;
            case 'N':a[i]='B';break;
            case 'U':a[i]='Y';break;
            case 'J':a[i]='H';break;
            case 'M':a[i]='N';break;
            case 'I':a[i]='U';break;
            case 'K':a[i]='J';break;
            case ',':a[i]='M';break;
            case 'O':a[i]='I';break;
            case 'L':a[i]='K';break;
            case 'P':a[i]='O';break;
            case ';':a[i]='L';break;
            case '[':a[i]='P';break;
            case '\'':a[i]=';';break;
            case ']':a[i]='[';break;
            case '\\':a[i]=']';break;
            case '/':a[i]='.';break;
            case '.':a[i]=',';break;
        }
        printf("%c",a[i]);
    }
}

1099: Integer Prefix 

题目描述

独特的语言数字关联(UNAL)是一种在不同文本中只保留数字的关联。事实上,数字对UNAL来说是如此重要,它的原理是:“人们只能用数字交流”。Mr. Potato Head,UNAL的头儿给你分配了一项任务来帮助他们实现协会的目标。

给定一个文本T,你必须找到最长的非空前缀组成的数字,对于UNAL这足以理解整个文本。

输入

输入一行不带空格的文本T。文本的长度是1到2*10^5

输出

如果没有这样的前缀输出“- 1”,否则打印一行包含T的最长非空的数字前缀

样例输入

23082019UNAL

样例输出

23082019

源代码

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    string a;
    cin>>a;
    int flag=a.find("UNAL")-1;
    if(!(a[flag]>='1'&&a[flag]<='9'))cout<<"-1";//UNAL的前一位不为字符类型的数字,无解 
    else
    {
        for(int i = 0;i <= flag;i ++ )cout<<a[i];
    }
    return 0;
} 

1100: 素数表(函数专题) 

题目描述

输入两个正整数m和n,输出m和n之间的所有素数。 
要求程序定义一个prime()函数和一个main()函数,prime()函数判断一个整数n是否是素数,其余功能在main()函数中实现。 
    int prime(int n) 
    { 
        //判断n是否为素数, 若n为素数,本函数返回1,否则返回0 
    } 
对于C/C++代码的提交,本题要求必须通过定义prime函数和main函数实现,否则,提交编译错误,要提交完整的程序。  

输入

输入两个正整数m和n,m<=n,且都在int范围内。 

输出

输出占一行。输出m和n之间的所有素数,每个数后有一个空格。测试数据保证m到n之间一定有素数。 

样例输入

2 6

样例输出

2 3 5 

源代码

#include <iostream>
#include <cmath>
using namespace std;
bool prime(int n)
{
    if(n == 1)return false;
    for(int i = 2;i <= sqrt(n);i ++ )
    {
        if(n % i == 0)return false;
    }
    return true;
}
int main()
{
    int m,n;
    cin >> m >>n;
    for(int i = m;i <= n;i ++ )
    {
        if(prime(i))cout<<i<<' ';
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/couchpotatoshy/article/details/125309037