Ad Hoc类问题

__________________________________

Ad Hoc类问题的方法:
(1)机理分析法。分析题目描述,推出算法。

(2)统计分析法。追寻最终的数学模型。

 Problem 1:

起始的奇迹之年是1960年。

 

第n年的位数k=2^(2+(y-1960)/10),能放在k位中最大的无符号整数是(2^k)-1, 那么我们就引入了对数的运算:

--------------https://vjudge.net/contest/317081#problem/A

code:

#include<stdio.h>
#include<math.h>
int main()
{
    int year;
    int endflag;
    int n;
    double sum;
    while(scanf("%d",&year),year)
    {
        endflag=1<<((year-1960)/10+2);// 1<<a==pow(2,a)
        
	n=2;
        sum=0;
        while(sum<=endflag)
        {
            sum+=log(n)/log(2);//log2(n)!
            n++;
        }
        printf("%d\n",n-2);
    }
    return 0;
}

Problem 2:

https://vjudge.net/contest/317081#problem/C

#include<iostream>
#include<cstdio>
using namespace std;
int t,d,m,n,min_,max_;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&m,&n);
		min_=max_=0;//双双赋值! 
		while(n--)
		{
			scanf("%d",&d);
			min_=max(min_,min(d,m-d));
			max_=max(max_,max(d,m-d));
		}
		printf("%d %d\n",min_,max_);
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/dragondragon/p/11297602.html
今日推荐