N诺计算机复试刷题day02

1010 排序

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
/*
输入n个数进行排序,要求先按奇偶后按从小到大的顺序排序。
从这道题我学会了vector数组的声明 vector<int> arr;
arr.push_back(num); 往数组里添加数据
sort(arr.begin(), arr.end());对数组里的数据进行从小到大排序
以及如何遍历vector数组
*/
int main() {
    int n;
    cin >> n;
    int num;
    vector<int> arr;
    //往数组里插入数据
    for (int i = 0; i < n; i++)
    {
        cin >> num;
        arr.push_back(num);
    }

    //对数组进行排序
    sort(arr.begin(), arr.end());
    //偶数数组
    vector<int> arr1;
    //奇数数组
    vector<int> arr2;

    //将排序好的数组按奇偶分别放到不同数组中
    for (auto it = arr.begin(); it !=arr.end(); it++)
    {
        if (*it % 2 == 0) {
            arr1.push_back(*it);
        }
        else {
            arr2.push_back(*it);
        }
    }

    //输出奇数
    for (auto it = arr2.begin(); it != arr2.end(); it++)
    {
        cout << *it << " ";
    }
    //输出偶数
    for (auto it = arr1.begin(); it != arr1.end(); it++)
    {
        cout << *it << " ";
    }
    return 0;
}

1012字符移动

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

int main() {
    string s;
    getline(cin, s);
    //声明vector数组
    vector<char> arr1;
    vector<char> arr2;
    //遍历字符串 将数字放入到数组arr1中,将非数字的字符放入到arr2中
    for (auto i = 0; i < s.length(); i++)
    {
        if (s[i]>='1'&&s[i]<='9')
        {
            arr1.push_back(s[i]);
        }
        else {
            arr2.push_back(s[i]);
        }
    }
    //先输出非数字字符数组
    for (auto it = arr2.begin(); it != arr2.end(); it++)
    {
        cout << *it;
    }
    //再输出字符数组
    for (auto it = arr1.begin(); it != arr1.end(); it++)
    {
        cout << *it;
    }
}

1011日期

从这道题中我第一次学到了定义map映射以及如何使用map映射

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

/*
今天是2012年4月12日星期四,编写程序,输入今天开始到12月31日之间的任意日期,输出那一天是星期几。
例如输入“5(回车)20(回车)”(5月20日),输出应为“Sunday”。
*/

int main() {
    //定义映射map 来对应月数和天数之间的关系
    map<int, int> month;
    month[4] = 30;
    month[5] = 31;
    month[6] = 30;
    month[7] = 31;
    month[8] = 31;
    month[9] = 30;
    month[10] = 31;
    month[11] = 30;
    month[12] = 31;
    //定义待输入的月数和天数
    int m, d;
    cin >> m >> d;
    //定义待输入的月数和天数与3月31日相距的天数
    int sum= 0;

    for (auto i= 4;  i< m; i++)
    {
        sum = sum + month[i];
    }
    
    map<int, string> week;
    week[0] = "Sunday";
    week[1] = "Monday";
    week[2] = "Tuesday";
    week[3] = "Wednesday";
    week[4] = "Thursday";
    week[5] = "Friday";
    week[6] = "Saturday";
    
    for (int i = 0; i < 7; i++)
    {
        if ((sum + d+ 6) % 7 == i) {
            cout << week[i];
            break;
        }
    }

    return 0;
}

1013判断素数

注意:1不是素数!!!!!!

#include <iostream>
using namespace std;

int main() {
    int num;
    cin >> num;
    int count = 0;
    for (int i = 2; i < num; i++) {
        if (num % i == 0) {
            count++;
        }
    }
    // 若count = 0 则说明num是素数 并且输出 
        
    // 注意: 1不是素数
    if (count == 0&&(num!=1)) {
        cout << num << endl;
    }
    else {
    //找到下一个素数
        for (int i = num + 1; i < 100000; i++) {
            count = 0; // 重置count
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    count++;
                }
            }
            if (count == 0) {
                cout << i << endl;
                break; // 找到素数后退出循环
            }
        }
    }
    return 0;
}

1040利润提成

#include<iostream>
using namespace std;

int main() {
    int profit;
    int bonus = 0;
    cin >> profit;
    if (profit<= 100000 )
    {
        bonus = int(0.1 * profit);
        cout << bonus << endl;
    }
    else if((profit >100000) && (profit<=200000)){
        bonus = int(0.1*100000+0.075*(profit-100000));
        cout << bonus << endl;
    }
    else if ((profit>200000)&&(profit<=400000))
    {
        bonus = int(0.1 * 100000 + 0.075 * (200000 - 100000)+0.05*(profit-200000));
        cout << bonus << endl;
    }
    else if ((profit > 400000) && (profit <= 600000))
    {
        bonus = int(0.1 * 100000 + 0.075 * (200000 - 100000) + 0.05 * (400000 - 200000)+0.03* (profit - 400000));
        cout << bonus << endl;
    }
    else if ((profit > 600000) && (profit <= 1000000)) {
        bonus = int(0.1 * 100000 + 0.075 * (200000 - 100000) + 0.05 * (400000 - 200000) + 0.03 * (600000 - 400000)+0.015*(profit-600000));
        cout << bonus << endl;
    }
    else {
        bonus = int(0.1 * 100000 + 0.075 * (200000 - 100000) + 0.05 * (400000 - 200000) + 0.03 * (600000 - 400000) + 0.015 * (1000000 - 600000)+0.01*(profit - 1000000));
        cout << bonus << endl;
    }
    return 0;
}

1014加密算法

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

int main() {
    string s;
    getline(cin, s);
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] >= 'a' && s[i] <= 'w') {
            s[i] = s[i] + 3;
        }
        else if (s[i] == 'x') {
            s[i] = 'a';
        }
        else if (s[i] == 'y')
        {
            s[i] = 'b';
        }
        else if (s[i] == 'z')
        {
            s[i] = 'c';
        }
        
        if (s[i] >= 'A' && s[i] <= 'W') {
            s[i] = s[i] + 3;
        }
        else if (s[i] == 'X') {
            s[i] = 'A';
        }
        else if (s[i] == 'Y')
        {
            s[i] = 'B';
        }
        else if (s[i] == 'Z')
        {
            s[i] = 'C';
        }
    }
    cout << s << endl;
    return 0;
}

1454反序数

在逆置数字时,可以考虑将数字转换为字符串再进行逆置. 如果提前知道数字的长度,那就可以定义一个string类型的数组使其等于"abcd"(有多长就等与多长)

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

int main(){
    int num;
    for (int num = 1000; num < 9999; num++)
    {
        string s = to_string(num);
        string temp = "abcd";
        for (int i = 0; i < s.length(); i++)
        {
            temp[i] = s[s.length() - i - 1];
        }
        int reverse_num = stoi(temp);
        if (num * 9 == reverse_num) {
            cout << num << endl;
        }
    }
    return 0;
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值