Exercise 1-Recursion, etc.

1000 Problem A

Time limit: 40/20 MS(Java/Others) | Memory limit: 65536/32768 KB(Java/Others)

Submits : 110 | Solved : 24

Title description

Given a set of unordered values, the size of the value is between 1 and a million, and the number of values ​​is between 100,000 and 500,000. Now you need to find the 5th to 10th smallest integer.

Input requirements

A group of non-zero integers, (number>=10), 0 is the end sign.

Output requirements

The 5th to 10th smallest integers. Each output an integer wraps.

Input sample

1
2
3
4
5
6
7
8
9
10
0

Sample output

5
6
7
8
9
10

// An highlighted block
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    
    
	int a[5000];
	int i=0;
	int x;
	while(cin>>x){
    
    
		if(x==0) break;
		a[i++]=x;

	}
	sort(a,a+i-2);
	for(i=4;i<10;i++){
    
    
		cout <<a[i]<<endl;
	}
	return 0;
}

1001 Problem B

Time limit: 2000/1000 MS(Java/Others) | Memory limit: 65536/32768 KB(Java/Others)

Submits : 55 | Solved : 7

Title description

For a walkway with 2 rows and N columns. Now use 1 2 or 2 2 bricks to fill it up. Ask how many different ways (please use recursion to solve). If N is large, high-precision calculation is required. The picture below is a certain paving method of a walkway with 2 rows and 17 columns:

Insert picture description here

Input requirements

An integer N, N<=1000.

Output requirements

How many methods are there in total?

Input sample

30

Sample output

715827883

analysis

1 The addition of large integers uses string
2 The problem of laying bricks:
the situation considered when laying bricks is roughly the same, so it can be solved by recursion. According to the number of remaining columns, we divide this question into two situations:

A: The last column is left, so suppose that after removing this column, the tiled situation is the same as that of n-1, and after adding, there is only one situation, so the method is pave(n-1)

B: There are two remaining columns at the end, then remove the two columns first and then the situation is the same as n-2. After adding these two columns, there are three situations: 1 2 is placed vertically, 2 is placed horizontally, 2 2 Fill up directly. Because 1 2 is placed vertically and overlaps with A, the number of methods is pave(n-2)*2

In summary: the total number of methods=pave(n-1)+2*pave(n-2
Insert picture description here

// An highlighted block
#include<iostream>
#include <algorithm>
#include<string>
using namespace std;
//用string实现大整数加法
string myadd(string a,string b){
    
    
	reverse(a.begin(),a.end());//字符串翻转
	reverse(b.begin(),b.end());
	int len;
	if(a.length()>b.length()){
    
    
		len=a.length(); //记录长的长度
		while(b.length()==len) b+=" ";//末尾补齐
	}else{
    
    
		len=b.length(); 
		while(a.length()==len) a+=" ";
	}
	int i=0,t=0;
	string ans;
	while(i<len){
    
    
		t+=a[i]-'0'+b[i]-'0';
		ans+=(t%10+'0');//拼接
		t/=10;
		i++;
	}
	if(t>0) ans+=t+'0';//进位加上
	reverse(ans.begin(),ans.end());
	return ans;
}

int main(){
    
    
	int n;
	string dp[1200];
	dp[0]="1";
	dp[1]="1";
	dp[2]="3";
	for(int i=3;i<=1000;i++){
    
    
		dp[i]=myadd(dp[i-1],myadd(dp[i-2],dp[i-2]));
	}
	while(cin>>n){
    
    	 
		cout<<dp[n]<<endl;
	}
	return 0;
}

1002 Problem C

Time limit: 2000/1000 MS(Java/Others) | Memory limit: 65536/32768 KB(Java/Others)
Submits: 56 | Solved: 11

Title description

An N×N block, the upper left corner is [1,1], the lower right corner is [N,N], (N<100). It is now required to find the total number of paths from the upper left corner to the lower right corner, each time you can only go down or right.

There are M blocks in the path with obstacles (M<10), which cannot be passed, but it will not lead to a situation where the end point cannot be reached.

The total number of paths on each road must be 10,000 to avoid data overflow.

Input requirements

The first line: two integers N and M; respectively represent the block dimension and the number of obstacles;

The second line starts with M lines: the block where the obstacle is located.

Output requirements

Output the number of paths that meet the meaning of the question.

Input example
3 1
3 1

Sample output
5
Insert picture description here

// An highlighted block
# include<iostream>
using namespace std;
int dp[101][101];  // 保存走到每个街区的路数

int main()
{
    
    
	for(int i=0;i<=100;i++)
		for(int j=0;j<=100;j++)
			dp[i][j] = 1;
	int n,m;  // 街区的维数和障碍数
	cin >> n >> m;
	while(m--)  // 将每个有障碍的街区置为0 
	{
    
    
		int a, b;
		cin >> a >> b;
		dp[a][b] = 0;
	}
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
		{
    
    
			if(dp[i][j] != 0)  //不考虑已经被置为0的有障碍的街区 
			{
    
    
				if(i==1 && j!=1)  // 给第一行街区赋值,不包括初始位置 
					dp[i][j] = dp[i][j-1];
				else if(i!=1 && j==1)  // 给第一列街区赋值,不包括初始位置
					dp[i][j] = dp[i-1][j];
				else if(i!=1 || j!=1)  //除初始位置的其他位置 
					dp[i][j] = (dp[i][j-1] + dp[i-1][j]) % 10000 ; // 每个街区的路径数为左边街区的路径数和上方路径数之和 
			}
		}
	 
	cout << dp[n][n] << endl; 
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_40852935/article/details/109170163