西工大计算机考研机试题练习(2012年-2007年)

2012年

1.1322求子集重量之和

#include <bits/stdc++.h>
using namespace std;
int main()
{
   
    int n;
    vector<int> v;
    cin >> n;
    int m = n;
    while (m--)
    {
   
        int x;
        cin >> x;
        v.push_back(x);
    }
    int sum = 0;
    for (int j = 0; j < n; j++)
    {
   
        bool i;
        cin >> i;
        if (i == 1)
        {
   
            sum += v[j];
        }
    }
    cout << sum;
    system("pause");
    return 0;
}

2.1054字符串统计

#include <bits/stdc++.h>
using namespace std;
int main()
{
   
    string s;
    getline(cin, s);
    int alpha = 0, num = 0, blank = 0, others = 0;
    for (auto i : s)
    {
   
        if (isdigit(i))
        {
   
            num++;
        }
        else if (isalpha(i))
        {
   
            alpha++;
        }
        else if (i == ' ')
        {
   
            blank++;
        }
        else
        {
   
            others++;
        }
    }
    cout << alpha << " " << blank << " " << num << " " << others << endl;
    system("pause");
    return 0;
}

3.1082花生米系列

1.k任取

共有n粒,输入n,甲先取k粒,乙再取k粒如此反复,直到乙取走最后一粒。输出k,若无解则输出0.0<k<10、0<n<=1000
暴力法:k取值有10种,从k=9开始尝试,成功就停止循环,不行k=8……都不行输出0(事实上除了n=1之外都有解,我做的有错吗?)
两人是否必须取满k粒??此题搁置,在noj系统中提交时候再解

#include <bits/stdc++.h>
using namespace std;
bool isOk(int n, int k)
{
   
    if (n <= k)
        return 0;
    if (n == 2)
    {
   
        cout << n << "  1" << endl;
        return 1;
    }
    int quotient = n / k;
    int remainder = n % k;
    if (quotient % 2 == 0 and remainder == 0)
    {
   
        cout << n << "  " << k << endl;
        return 1;
    }

    return 0;
}
int main()
{
   
    for (int j = 1; j < 1000; j++)
    {
   
        int flag = 1;
        for (int i = 9; i > 0; i--)
        {
   
            if (isOk(j, i))
            {
   
                flag = 0;
                break;
            }
        }
        if (flag == 1)
            cout << j << "  0" << endl;
    }
    system("pause");
    return 0;
}

https://blog.csdn.net/qq_41727666/category_9279609.html

2.k=1或5或10

4.1091求解逆波兰表达式

#include <bits/stdc++.h>
using namespace std;

int main()
{
   
    string s;
    getline(cin, s);
    stack<int> st;
    int first, second;
    for (auto i : s)
    {
   
        if (isdigit(i))
        {
   
            st.push(i - '0');
        }
        else
        {
   
            second = st.top();
            st.pop();
            first = st.top();
            st.pop();
            switch (i)
            {
   
            case '*':
                st.push(first * second);
                break;
            case '/':
                st.push(first / second);
                break;
            case '+':
                st.push(first + second);
                break;
            case '-':
                st.push(first - second);
                break;
            }
        }
    }
    cout << st.top() << endl;
    system("pause");
    return 0;
}

5.1021柱状图

#include <bits/stdc++.h>
using namespace std;
int main()
{
   
    map<char, int> mp;
    for (int i = 0; i < 26; i++)
    {
   
        mp[char('A' + i)] = 0;
    }
    int max = -1;
    char maxc;
    for (int i = 0; i < 4; i++)
    {
   
        string s;
        getline(cin, s);
        for (auto i : s)
        {
   
            if (i >= 'A' and i <= 'Z')
            {
   
                mp[i]++;
                if (mp[i] > max)
                {
   
                    max = mp[i];
                    maxc = i;
                }
            }
        }
    }
    char show[max + 1][26];
    for (int i = 0; i < max + 1; i++)
    {
   
        for (int j = 0; j < 26; j++)
        {
   
            show[i][j] = ' ';
        }
    }
    for (auto i : mp)
    {
   
        //cout << i.first << " " << i.second << endl;
        for (int j = max; j > max - i.second; j--)
        {
   
            show[j][int(i.first - 'A')] = '*';
        }
    }
    for (int i = 1; i < max + 1; i++)
    {
   
        for (int j = 0; j < 26; j++)
        {
   
            if (j == 0)
                cout << show[i][j];
            else
                cout << " " << show[i][j];
        }
        cout << endl;
    }
    for (int i = 0; i < 25; i++)
    {
   
        cout << char(i + 'A') << " ";
    }
    cout << 'Z' << endl;
    system("pause");
    return 0;
}

6.1028判断三角形

根据三角形“两边之和大于第三边,两边之差小于第三边”来判断

7.1147木乃伊迷宫

没做。挖坑。

8.数独游戏

数独游戏规则
在9阶方阵中,包含了81个小格(九列九行),其中又再分成九个小正方形(称为宫),每宫有九小格。
游戏刚开始时,盘面上有些小格已经填了数字(称为初盘),游戏者要在空白的小格中填入1到9的数字,
使得最后每行、每列、每宫都不出现重复的数字,而且每一个游戏都只有一个唯一的解答(称为终盘)。

输入
一个9*9的矩阵,0表示该位置是空白。

输出
一个9*9的矩阵,格式与输入类似。

输入样例
900050060
020070100
300102040
703800529
000345000
516009403
050208006
007090010
030010004

输出样例
971453268
428976135
365182947
743861529
892345671
516729483
154238796
687594312
239617854
思路:DFS+剪枝,每个DFS函数处理一格子,不能填入的或全部填满则函数终止,其他情况dfs函数处理下一格子。

#include <bits/stdc++.h>
using namespace std;
int b[9][9];
bool isValid(int x, int y, int num)
{
   
    for (int i = 0; i < 9; i++)
    {
   
        if (b[i][y] == num)
            return 0;
        if (b[x][i] == num)
            return 0;
    }
    int x0 = x / 3, y0 = y / 3;
    for (int i = 3 * x0; i < 3 * x0 + 3; i++)
    {
   
        for (int j = 3 * y0; j < 3 * y0 + 3; j++)
        {
   
            if (b[i][j] == num)
                return 0;
        }
    }
    return 1;
}
void dfs(int row, int col)
{
   
    if (row == 8 and col == 8)
    {
   
        for (int i = 0; i < 9; i++)
        {
   
            for (int j = 0; j < 9; j++)
            {
   
                cout << b[i][j];
            }
            cout << endl;
        }
        return;
    }
    if (b[row][col] != 0) //不能填入的 往前走一个格
    {
   
        if (col < 8)
        {
   
            dfs(row, col + 1);
        }
        else if (col == 8)
        {
   
            dfs(row + 1, 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值