Coursera课程笔记----C程序设计进阶----Week 8&9

面向对象入门&期末考试(Week 8&9)

面向对象

  • 什么是计算机程序
    • 现实世界的解决方案在计算机系统中的映射
  • 编程语言
    • 帮助我们实现映射的工具
  • 面向对象语言
    • 比其他语言更加先进

期末考试

Quiz1 含k个3的数

#include <iostream>
using namespace std;
int main()
{
    int m,k;
    cin >> m >> k;

    int temp = m;
    int count = 0;
    while (temp != 0)
    {
        int a = temp % 10;
        if(a == 3)
            count++;
        temp = temp / 10;
    }
    if(m % 19 == 0 && count == k)
        cout<<"YES"<<endl;
    else cout<<"NO"<<endl;

    return 0;

}

Quiz2 字符串中次数第二多的字母

//我觉得这道题我写的实在是太乱太乱了Orz
//反正OJ是过啦!但是感觉写的太弱智了Orz
#include <iostream>
using namespace std;
int main()
{
    int count[26] = {0};
    char low[26] = {'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 high[26] = {'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 sequence[26] = {''};
    char a[500] = {''};
    int counter = 0;
    cin.getline(a,500);

    for (int i = 0; a[i] != ''; i++) {
        for (int j = 0; j < 26; j++) {
            if(a[i] == low[j] || a[i] == high[j])
            {
                if(count[j] == 0)
                {
                    sequence[counter] = low[j];
                    counter++;
                }
                count[j]++;
                break;
            }
        }
    }

    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26-i-1; j++) {
            if(count[j] < count[j+1])
            {
                int temp = count[j];
                count[j] = count[j+1];
                count[j+1] = temp;
                char temp1 = low[j];
                low[j] = low[j+1];
                low[j+1] = temp1;
            }
        }
    }

    int max = count[0];
    int submax = 0;
    for (int i = 0; i < 26; i++) {
        if(count[i] < max)
        {
            submax = count[i];
            break;
        }
    }

    int flag = 0;
    char sublow[26] = {''};
    for (int i = 0; i < 26; i++) {
        if(count[i] == submax)
        {
            sublow[flag] = low[i];
            flag++;
        }
    }

    if(flag == 1)
    {
        cout<<char(sublow[0] - 32) <<'+'<<sublow[0]<<':'<<submax<<endl;
    } else if (flag > 1)
    {
        char only = '';
        int call = 0;
        for (int i = 0; sequence[i] != ''; i++) {
            for (int j = 0; j < flag; j++) {
                if(sequence[i] == sublow[j] && call == 0)
                {
                    only = sublow[j];
                    call = 1;
                }
            }
        }
        cout<<char(only - 32) <<'+'<<only<<':'<<submax<<endl;
    }

    return 0;
}

Quiz3 运算符判定

#include <iostream>
using namespace std;
int main()
{
    int a,b,c;
    char d,e;
    cin >> a >> d >> b >> e >> c;

    if(c == a + b)
        cout<<'+'<<endl;
    else if(c == a - b)
        cout<<'-'<<endl;
    else if(c == a * b)
        cout<<'*'<<endl;
    else if(c == a / b)
        cout<<'/'<<endl;
    else if(c == a % b)
        cout<<'%'<<endl;
    else cout<<"error"<<endl;

}

Quiz4 寻找平面上的极大点

#include <iostream>
using namespace std;
int main()
{
    int a[100]={0},b[100]={0};
    int a1[100]={0},b1[100]={0};
    int index = 0;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }

    for (int i = 0; i < n; i++) {
        int flag = 0;
        for (int j = 0; j < n; j++) {
            if(a[i] <= a[j] && b[i] <= b[j] && i != j )
                flag = 1;
        }
        if(flag == 0)
        {
            a1[index] = a[i];
            b1[index] = b[i];
            index++;
        }
    }

    for (int i = 0; i < index; i++) {
        for (int j = 0; j < index-i-1; j++) {
            if(a1[j] > a1[j+1])
            {
                int temp = a1[j];
                a1[j] = a1[j+1];
                a1[j+1] = temp;
                temp = b1[j];
                b1[j] = b1[j+1];
                b1[j+1] = temp;
            }
        }
    }

    for (int i = 0; i < index; i++) {
        cout<<'('<<a1[i]<<','<<b1[i]<<')';
        if(i != index-1)
            cout<<',';
    }
    return 0;
}

Quiz5 走出迷宫

//暂时没有思路,回头补
原文地址:https://www.cnblogs.com/maimai-d/p/12868691.html