2016hw卷练习

小结:除了数独这道题写的出了bug,交卷才发现并改正,剩下两题都很轻松。
成绩:AC两道
在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
bool rows[9][10] = { false };//rows[i][j]为true表示第i行的数字j已经被使用了
bool cols[9][10] = { false };//cols[i][j]为true表示第i列的数字j已经被使用了
bool blocks[9][10] = { false };//cols[i][j]为true表示第i个子数独中数字j已经被使用了
bool finished = false;
void DFS(vector<vector<int>>&board, int row, int col)
{
	if (row == 9)
	{
		finished = true;//说明完成了
		return;
	}
	if (board[row][col] != 0)//不需要填写
	{
		if (col == 8)
		{
			DFS(board, row + 1, 0);
		}
		else
		{
			DFS(board, row, col + 1);
		}
	}
	else//需要填写
	{
		for (int number = 1; number <= 9; number++)
		{
			if (!rows[row][number] && !cols[col][number] && !blocks[row / 3 * 3 + col / 3][number])
			{
				rows[row][number] = true;
				cols[col][number] = true;
				blocks[row / 3 * 3 + col / 3][number] = true;

				board[row][col] = number;
				if (col == 8)
				{
					DFS(board, row + 1, 0);
				}
				else
				{
					DFS(board, row, col + 1);
				}

				//回溯
				if (finished == false)
				{
					rows[row][number] = false;
					cols[col][number] = false;
					blocks[row / 3 * 3 + col / 3][number] = false;
					board[row][col] = 0;//重置为0
				}
				else
					break;
			}
		}
	}

}
void SolveSudu(vector<vector<int>>&board)
{
	for (int i = 0; i < 9; i++)
		for (int j = 0; j < 9; j++)
		{
			if (board[i][j] != 0)
			{
				rows[i][board[i][j]] = true;
				cols[j][board[i][j]] = true;
				blocks[i / 3 * 3 + j / 3][board[i][j]] = true;
			}
		}
	DFS(board, 0, 0);
}

int main()
{
	vector<vector<int>>board(9, vector<int>(9));
	for (int i = 0; i < 9; i++)
		for (int j = 0; j < 9; j++)
			cin >> board[i][j];

	SolveSudu(board);
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
			cout << board[i][j] << " ";
		cout << endl;


	}



	system("pause");
	return 0;
}

在这里插入图片描述

循环链表秒杀

#include<iostream>
using namespace std;
typedef struct ListNode
{
    ListNode*next;
    int val;
    ListNode(int val):val(val),next(NULL){}
}ListNode;
int fun(int N)
{
    ListNode*dummy=new ListNode(-1);
    ListNode*cur=dummy;
    for(int i=0;i<N;i++)
    {
        cur->next=new ListNode(i);
        cur=cur->next;
    }
    cur->next=dummy->next;//首尾相接
    
    cur=dummy->next;
    ListNode*next=NULL;
    while(cur->next!=cur)
    {
        cur=cur->next;
        next=cur->next->next;
        delete cur->next;
        cur->next=next;
        cur=cur->next;
       
    }
    return cur->val;
}
int main()
{
    int N;
    while(cin>>N)
    {
        cout<<fun(N)<<endl;
    }
    
    
    
    system("pause");
    return 0;
}

在这里插入图片描述
哈希秒杀

#include<iostream>
#include<map>
#include<string>
using namespace std;
string fun(const string&s)
{
	map<char, int>mp;
	for (int i = 0; i<s.size(); i++)
	{
		if (mp.find(s[i]) == mp.end())
		{
			mp.insert(make_pair(s[i],1));
		}
		else
		{
			mp[s[i]]++;
		}
	}
	string res;
	for (int i = 0; i<s.size(); i++)
	{
		if (mp[s[i]] >= 1)
		{
			res += s[i];
			mp[s[i]]=0;
		}
		
	}
	return res;
}
int main()
{
	string s;
	while (cin>>s)
	{
		cout << fun(s) << endl;
	}

	system("pause");
	return 0;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页