HDU1030 找规律

1:Nx,Ny(N,M的层数)的获取,注释的是我之前写的,紧跟着的后面的两行可以代替。原来的代码需要讨论该数是不是每一行的最后一个数。

2:嗯,盗个图,

2:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <cmath>
using namespace std;
#define ll long long int  

int T;

int main()
{
	int N,M;
	while(EOF!=scanf("%d %d",&N,&M))
	{
		int Nx,Ny,Nz;
		int Mx,My,Mz;
//		if(1==N)	Nx=1;
//		else{
//			if((int)sqrt(N)*(int)sqrt(N)==N){
//				Nx=(int)sqrt(N);
//			}else{
//				Nx=(int)sqrt(N)+1;
//			}
//		}
//		if(1==M)	Mx=1;
//		else{
//			if((int)sqrt(M)*(int)sqrt(M)==M){
//				Mx=sqrt(M);
//			}else{
//				Mx=sqrt(M)+1;
//			}
//		}
		Nz=sqrt(N-1)+1;
		Mz=sqrt(M-1)+1;
		Nx=(N-(Nz-1)*(Nz-1)+1)/2;
		Mx=(M-(Mz-1)*(Mz-1)+1)/2;
		Ny=(Nz*Nz-N)/2;
		My=(Mz*Mz-M)/2;
		cout << abs(Nx-Mx)+abs(Ny-My)+abs(Nz-Mz) << endl; 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41755258/article/details/83045212