51nod 几道题

1001 数组中和等于K的数对

基准时间限制:1 秒 空间限制:131072 KB
分值: 5 难度:1级算法题
给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,
找出数组A中所有和等于K的数对。例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0,8),(2,6),(3,5)。

Input

第1行:用空格隔开的2个数,K N,N为A数组的长度。(2 <= N <= 50000,-10^9 <= K <= 10^9)
第2 - N + 1行:A数组的N个元素。(-10^9 <= A[i] <= 10^9)

Output

第1 - M行:每行2个数,要求较小的数在前面,并且这M个数对按照较小的数升序排列。
如果不存在任何一组解则输出:No Solution。

Input示例

8 9
-1
6
5
3
4
2
9
0
8

Output示例

-1 9
0 8
2 6
3 5

#include<algorithm>
#include<iostream>
#include<cstdiO>
#include<set>
using namespace std;

int n,k;
int s[100005];
set<int>str;

int main(){
	cin>>k>>n;
	int ans=0,num;
	for(int i=1;i<=n;++i){
		scanf("%d",&s[i]);
		str.insert(s[i]);
	}
	sort(s+1,s+n+1);
	for(int i=1;i<=n;++i){
		if(k-s[i]<s[i])break;
		if(k-s[i]==s[i])continue;
		if(str.count(k-s[i])){
			ans++;
			cout<<s[i]<<' '<<k-s[i]<<endl;
		}
	}
	if(!ans)cout<<"No Solution";
	return 0;
}

比较正常的做法是(O(n^2))枚举
但是会被卡
所以可以二分
因为可以与一个数配对的数有且只有一个



1002 数塔取数问题

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注
一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值。
每次只能走到下一层相邻的数上,例如从第3层的6向下走,只能走到第4层的2或9上。

5
8 4
3 6 9
7 2 9 5

例子中的最优方案是:5 + 8 + 6 + 9 = 28

Input

第1行:N,N为数塔的高度。(2 <= N <= 500)
第2 - N + 1行:每行包括1层数塔的数字,第2行1个数,第3行2个数......第k+1行k个数。数与数之间用空格分隔(0 <= A[i] <= 10^5) 。

Output

输出最大值

Input示例

4
5
8 4
3 6 9
7 2 9 5

Output示例

28

#include<iostream>
#include<cstdio>
#define N 505
using namespace std;

int n;
int s[N][N];
int f[N][N];

int main(){
	cin>>n;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=i;++j)
			cin>>s[i][j];
	for(int i=1;i<=n;++i)
		for(int j=1;j<=i;++j)
			f[i][j]=max(f[i-1][j],f[i-1][j-1])+s[i][j];
	int ans=0;
	for(int i=1;i<=n;++i)
		ans=max(ans,f[n][i]);
	cout<<ans;
	return 0;
}

水动规



1003 阶乘后面0的数量

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注
n的阶乘后面有多少个0?
6的阶乘 = 12345*6 = 720,720后面有1个0。

Input

一个数N(1 <= N <= 10^9)

Output

输出0的数量

Input示例

5

Output示例

1

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

int main(){
	int n;
	cin>>n;
	int ans=0;
	int sn=n;
	while(n>0){
		ans+=n/2;
		n/=2;
	}
	int ans1=0;
	while(sn>0){
		ans1+=sn/5;
		sn/=5;
	}
	cout<<min(ans,ans1);
	return 0;
}

找出作为因子的5的数量和2的数量
取min就好
但是不能直接求10的数量



1004 n^n的末位数字

题目来源: Author Ignatius.L (Hdu 1061)
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注
给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。

Input

一个数N(1 <= N <= 10^9)

Output

输出N^N的末位数字

Input示例

13

Output示例

3

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

int n;
int mmm[10][5]={
	{0,0,0,0},{1,1,1,1},
	{6,2,4,8},{1,3,9,7},
	{6,4,6,4},{5,5,5,5},
	{6,6,6,6},{1,7,9,3},
	{6,8,4,2},{1,9,1,9}
};

int main(){
	cin>>n;
	cout<<mmm[n%10][n%4];
	return 0;
}

发现



1009 数字1的数量

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 取消关注
给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数。
例如:n = 12,包含了5个1。1,10,12共包含3个1,11包含2个1,总共5个1。

Input

输入N(1 <= N <= 10^9)

Output

输出包含1的个数

Input示例

12

Output示例

5

#include<cstdio>
using namespace std;

int main(){
	int n;
	int t,tn;
	long long ans=0;
	int mul=1;
	scanf("%d",&n);
	tn=n;
	while(tn){
		t=tn%10;
		if(t==0)
			ans+=n/(mul*10)*mul;	
		else if(t==1){
			ans+=n/(mul*10)*mul;
			ans+=(n%mul)+1;
		}
		else		
			ans+=(n/(mul*10)+1)*mul;
		mul*=10,tn/=10;
	}
	printf ("%lld
",ans);
	return 0;
}




原文地址:https://www.cnblogs.com/qdscwyy/p/7732655.html