期待给予指正与建议,愿共勉

习题3-1:  分数统计(stat)

输入一些学生的分数,那个分数出现的次数最多?如果有多个并列,从大到小输出。

任务一:分数均为不超过100的非负整数。

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

int main()
{
    int a[101], n;
    memset(a, 0, sizeof(a));
    while(cin >> n) //以空间换时间的方法
        ++a[n];
    int max = a[0];
    for(int i = 1; i < 101; ++i)
        if(a[i] > max)
            max = a[i];
    for(int i = 0; i < 101; ++i)
        if(a[i] == max)
            cout << i << " ";
    cout << "It's number is: " << max << endl;
    return 0;
}



任务二:分数为不超过100的实数,不超过两位小数。

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

bool cmp(double a, double b)
{
    return 100*a < 100*b;   //题目表明了最多保留两位小数
}
const int maxn = 100+10;
int main()
{
    double mark[maxn], b[maxn], n;
    int cnt;
    cnt = 0;
    cout << "Enter some numbers:(Press Ctr+Z to outpue)" << endl;
    while(cin >> mark[cnt++]);
    sort(mark, mark+cnt, cmp);   //排序
    memset(b, 0, sizeof(b)); 
    int count = 1;
    for(int i = 1, j = 0; i < cnt; ++i)
    {
        if(100*mark[i] == 100*mark[i-1])//最多保留两位小数
            ++count;
        else
        {
            b[i-1] = count;//b[i]同步记录mark的相同个数
            count = 1;
        }
    }
    b[cnt-1] = count;
    int max = b[0]; //找出最大个数
    for(int i = 1; i < cnt; ++i)
        if(b[i] > max)
            max = b[i];
    for(int i = 0; i < cnt; ++i)
        if(b[i] == max)
            cout << mark[i] << " "; //输出最大的分数
    cout << "It's number is: " << max << endl;
    return 0;
}


习题3-2:单词的长度(word)

输入若干个单词,输出它们的平均长度。单词只包含大写字母和小写字母,用一个或多个空格隔开。

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

int main()
{
    string line;
    int cnt, word;
    cnt = word = 0;
    while(cin >> line)
    {
        ++word;
        cnt += line.size();
    }
    cout << cnt << " " << word << " " <<  (1.0 * cnt/word) << endl;
    return 0;
}


习题3-3:成绩的末三位(product)

输入若干个整数(可以是正数、负数或者零),输出它们的成绩的末三位。这些整数中会混入一些由大写字母组成的字符串,你的程序应当忽略它们。提示:试试看,在执行scanf(“%d”)时输入一个字符串会怎样?

#include<iostream>
using namespace std;

int main()
{
    int cheng, ge, shi, bai, n;
    cheng = 1;
    while(cin >> n)
    {
        ge = cheng * (n % 10);
        shi = 10*cheng * ((n/10)%10);
        bai = 100*cheng * (n / 100);
        cheng = ge + bai + shi;
        cheng %= 1000;
        if(isalpha(n))
            cin.clear();
    }
    cout << cheng << endl;
    return 0;
}

习题3-4:计算器(calculator)

编写程序,读入一行恰好包含一个加号、减号或称号的表达式,输出它们值。这个运算符保证是二元运算符,且两个运算数均为不超过100的非负整数。运算数和运算符可以紧挨着,也可以用一个或多个空格、TAB隔开。行首末位均可以有空格。提示:选择合适的输入方法可以将问题简化。

样例输入:1+1

输出:2

样例输入:2-   5

输出:-3

样例输入:0  *1+982

输出:0

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

const int maxn = 5000+10;
int main()
{
   char word[maxn];
    int a, b, i;
    char c;
    while(1)
    {
        i = 0;
        a = 0;  b = 0;  c = NULL;
        memset(word, 0, sizeof(word));  //别忘记清空数组
        while(c = getchar(), c!= '\n')
            if(c != ' ' && c != '   ')
                word[i++] = c;
        sscanf(word, "%d%c%d", &a, &c, &b);
        cout << a << "  " << b << " " << c << endl;
        if(c == '+')
            cout << a+b;
        else if(c == '-')
            cout << a-b;
        else if(c == '*')
            cout << a*b;
        cout << endl;
    }
    return 0;
}


习题3-5:旋转(rotate)

输入一个n*n字符矩阵,把它左转90度后输出。

#include<iostream>
using namespace std;

int main()
{
    int n, a[100][100];
    cout << "Please input a number: " << endl;
    cin >> n;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            cin >> a[i][j];
    cout << "Out:" << endl;
    for(int j = n-1; j >= 0; --j)
    {
        for(int i = 0; i < n; ++i)
            cout << a[i][j] << " ";
        cout << endl;
    }
    return 0;
}

/*
5
1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
*/

习题3-6:进制转换1(base)

输入基数b(2<=b<=10)和正整数n(十进制),输出n的b进制表示。

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

const int maxn = 500 + 10;
int main()
{
    int n, b, yu[maxn], i, end;
    end = 0;
    while(cin >> n >> b)
    {
        memset(yu, 0, sizeof(yu));
        while(n != 0)
        {
            yu[end++] = n % b;
            n /=  b;
        }
      for(i = 0; yu[i] == 0 && i < end; ++i); //找到第一个不为0的位置
      int beg = i-1;
        for(i = end-1; i >= beg; --i)
            cout << yu[i];
        cout << endl;
    }
    return 0;
}

 

习题3-7:进制转化2(base2)

输入基数b(2<=b<=10)和正整数n(十进制),输出n的十进制表示。

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;

const int maxn = 5000 + 10;
int main()
{
    int a[maxn], n, b;
    while(cin >> b >> n)
    {
        int cnt = 0;
        memset(a, 0, sizeof(a));
        while(n)
        {
            a[cnt++] = n%10;
            n /= 10;
        }
        int sum = a[0];
        for(int i = 1; i < cnt; ++i)
        {
            cout << a[i] << " " << sum << endl;
            sum += a[i]*pow(b, i);
        }
        cout << sum << endl;
    }
    return 0;
}

习题3-8: 手机键盘(keyboard)

输入一个由小写字母组成的英文单词,输出用手机的默认英文输入法的敲击序列。例如要达成pig这个单词,需要按1次p,3次i,(稍作停顿后)1次i,记为p1i3i1.

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;

int main()
{
    string word;
    while(cin >> word)
    {
        for(int i = 0; i < word.size(); ++i)
        {
            int t = (word[i]-'a'+1)%3;
            if(abs(word[i] - word[i-1]) < 3 && i != 0)
                cout << word[i-1] << (t?t:3);
            else
                cout << word[i] << (t?t:3);
        }
        cout  << endl;
    }
    return 0;
}

 

期待给予指正与建议,愿共勉

相关文章: