首页 > 试题广场 >

禁忌雷炎

[编程题]禁忌雷炎
  • 热度指数:2938 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
赫柏在绝域之门击败鲁卡斯后,从鲁卡斯身上掉落了一本高级技能书,赫柏打开后惊喜地发现这是一个早已失传的上古技能---禁忌雷炎。
该技能每次发动只需扣很少的精神值,而且输出也非常高。
具体魔法描述如下:
把地图抽象为一个二维坐标,技能发动者位于(0,0)位置。以技能发动者为中心,做一个半径为r的圆,满足r^2=S,如果敌人位于这个圆上,且位置为整点坐标,这个敌人将收到该技能的输出伤害。。
例如当S=25时,将有12个敌人受到该技能的输出伤害,如下图所示:

更厉害的是,禁忌雷炎可以通过改变魔法输入来控制S的大小,因此数学好的魔法师可以通过该技能攻击到更多的敌人。
赫柏想将这个技能学会并成为自己的主技能,可数学是他的硬伤,所以他请求你为他写一个程序,帮帮他吧,没准他就把禁忌雷炎与你分享了 : )


输入描述:
多组测试数据,请处理到文件结束。
对于每组测试数据,只包含一个整数S。
保证:
1<=S<=2,000,000,000。


输出描述:
输出一个整数,代表受到禁忌雷炎伤害的敌人数量。
示例1

输入

25
3

输出

12
0
只需要按一个方向遍历一遍 利用勾股定理x*x+y*y = r*r 即可解决四分之一圆周的点 *4 即为结果
 #include<iostream>
 #include<cstring>
 #include<cstdio>
 #include<cmath>
 using namespace std;
 int n;
 int main()
 {
     while(cin>>n)
     {
         int ans = 0;
         for(int i = 0; i*i < n; i ++)
         {
             int j = n - i*i;
             int s = sqrt(j);
             if(s*s == j)ans++;
         }
         cout<<4*ans<<endl;
     }
 }
编辑于 2016-06-19 10:25:33 回复(5)
//在第一象限,从原点向外,发散地画圆
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	unsigned int x, y, n, ans;
	while (cin >> n)
	{
		ans = 0;
		for (x = 0; x*x < n; x++) {
			y = sqrt(n - x*x);
			if (x*x + y*y == n)
				ans++;
		}
		cout << 4 * ans << endl;
    }
    return 0;
}

发表于 2016-09-02 14:09:27 回复(1)
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

long long s,cnt;
/*


*/


int main(){
	long long r,x,y;
	double tmpy;
	while(cin>>s){
		r= (long long )sqrt((double)s);
		x=r;
		y=0;
		cnt=0;
		while(x>=(-r)){
			tmpy = sqrt((double)(s-(x*x)));
			y= (long long )tmpy;
			if(tmpy-y <= 0.000001){
				if(y != 0){ //点如果落在除x轴的其他地方,根据圆的对称性,点的数量要加2
					cnt += 2;
				}else{//点如果落在x轴上,点数加一就可以了
					cnt++;
				}
				
			}
			
			x--;
		}
		cout<<cnt<<endl;
	}
    return 0;
}






编辑于 2016-09-01 16:22:57 回复(0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
importjava.util.Scanner;
 
publicclassMain {
 
    publicstaticvoidmain(String[] args) {
        Scanner scanner = newScanner(System.in);
        while(scanner.hasNext()) {
            intS = scanner.nextInt();
            intk = 0;
            intn = 0;
            for(inti = 0; i <= (int) Math.sqrt(S); i++) {
                doublej = Math.sqrt(S - i * i);
                if((int) j * (int) j + i * i == S) {
                    k++;
                    if(i == 0)
                        n++;
                }
            }
            k -= n;
            System.out.println(4* k);
        }
    }
 
}
编辑于 2016-08-03 13:49:51 回复(0)
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
    {
    double S;
    while(cin>>S)
        {
        int count=0;
        if(S<0)
           return 0;
        if(S==0)
        {
            cout<<1<<endl;
         continue;
        }
        int w=sqrt((double) S);
        if(w*w!=S)
            w=w+1;
        int i=w;
        int j=0;       
        while(i>=0&&j<=w)
            {
        if((i*i+j*j)==S)
            {count=count+1;
    i=i-1;
}
        if((i*i+j*j)>S)
        {i=i-1;
         continue;
        }
        if((i*i+j*j)<S)
            {j=j+1;
            continue;
            }
        }

          /*  for(int j=w;j>=0;j--)
       { if((j*j+i*i)==S)
            count=count+1;
            if((j*j+i*i)<S)
                break;
       }*/
        count=count*4;
double ww=w*w;
if(ww==s)
            count=count-4;
        cout<<count<<endl;
    }
    return 0;
    
}
发表于 2016-06-15 16:23:49 回复(0)
 
<?php
$array=explode(' ',trim(fgets(STDIN)));
$a=0;
$r= sqrt($array[0]);
for($i=0;$i<count($array);$i++){
 
for($x=1;$x<=$r;$x++){
    for($y=0;$y<$r;$y++){
        if(($x*$x+$y*$y)==$array[0]){
            $a++;
        }
    }
}
 
    echo4*$a>0?4*$a:0;   
 
}
?>

发表于 2018-03-28 18:18:58 回复(1)
import java.util.Scanner;
public classMain {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int s = in.nextInt();
int solution =0;
//利用圆的性质 x2 + y= r2
int max_x = (int) (Math.sqrt(s));
//利用圆的对称性,只需要求第一象限的点即可
for(int i =0; i<=max_x;++i){
int candidate_y = (int) Math.sqrt(s - i * i );
if(candidate_y * candidate_y + i*i  == s){
++solution;
}
}
solution = solution * 4;
//int r = (int) Math.sqrt(s);//最大的正整数半径为r
//如果边界上的点的点即(0,y),(0-y),(x,0),(-x,0)刚好也是符合条件的点
//每个点都计算了两次,所以-4
if(max_x* max_x   ==s ){
solution -= 4;//r正好是半径
}
System.out.println(solution);
}
}
}

                                                                                    
编辑于 2017-06-04 19:11:41 回复(0)
import math

try:
	while True:
		s = raw_input()
		if s == '':
			break
		s = int(s)
		counter = 0
		x_sqrt_list = (x * x for x in range(0, int(math.sqrt(n)) + 1))
		for x_sqrt in x_sqrt_list:
			if x_sqrt == s:
				break
			y_sqrt = s - x_sqrt
			y = math.sqrt(y_sqrt)
			if y == int(y):
				counter += 1
		print counter * 4
except Exception, e:
	pass
编辑于 2017-03-20 14:28:04 回复(0)
#include<iostream>
#include<cmath>
using namespace std;

long long ShuLiang(long long S)
{
double r = sqrt(double(S));
double x = sqrt((double)S / 2);
long long Num = 0;
long long r_ub = floor(r);
long long r_lb = ceil(x);
long long temp1 = 0;
for (long long i = r_lb;i <= r_ub;i++)
{
double temp = sqrt(double(S - i * i));
if (!(temp - (long long)temp))
temp1++;
}
Num += 8 * temp1;
if (!(r - r_ub))
{
Num -= 4;
}
if (!(r_lb - x))
{
Num -= 4;
}
return Num;
}

int main()
{
long long S;
while (cin >> S)
{
cout << ShuLiang(S) << endl;
}
}
发表于 2016-12-20 20:52:38 回复(0)
import java.util.Scanner;

public class Coin {
	/**
	 * 
	 * 此题相当于给定斜边长,求可构成直角三角形的整数点坐标。
	 * 给定斜边,再遍历其中一个边的各个整数点,
	 * 运算,求出另一个边的值,判断其是否为整数。
	 * 若为整数,即符合要求。
	 * 图为圆型,只需求出一个象限的个数成4,即可解题。
	 * 
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			double c = Double.parseDouble(sc.nextLine());
			double d = Math.sqrt(c);
			int flo = (int)Math.floor(d);//整数点只能出现在小于d的整数点上,所以向下取整
			double num = 0;
			int n = 0;
			//计算第一象限的整数点个数,只包括一个数轴上的整数点。
			//若i从0开始,则是包括了连个数轴上的整数点,会出现重复的整数点。
			for(int i=1;i<=flo;i++){
				num = Math.sqrt(d*d-i*i);//b^2=r^2-a^2勾股定理
				if(num-Math.floor(num)<1e-10){//判断num是不是整数
					n++;
				}
			}
			System.out.println(n*4);
		}
	}
}

编辑于 2016-09-19 16:23:56 回复(0)
public class Attack { public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        String[] tokens; while(scn.hasNext()){ int s = scn.nextInt(); getAttackNum(s);
        }
    } private static void getAttackNum(int s) 
{ int n = (int)Math.sqrt(s); int count = 0; for (int i = 1; i <=n ; i++) { double val = Math.sqrt(s-i*i); if(Math.floor(val) == val){
                count++;
            }
        }
        System.out.println(4*count);
    }
}
编辑于 2016-09-18 13:59:23 回复(0)
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
	int num;
	while (cin >> num){
		int loop = 0;
		for (int i = 0; i <sqrt(num); i++){
			double j = sqrt(num - i*i);
			if ((int)j == j) loop++;
		}
		loop = 4 * loop;
		cout << loop << endl;
	}
}

发表于 2016-09-04 13:29:58 回复(0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
importjava.util.Scanner;
 
publicclassMain {
    publicstaticvoidmain(String args[]){
        Scanner sc = newScanner(System.in);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            if(line.equals("")) break;
            ints = Integer.parseInt(line);
            doublea = Math.sqrt(s);
            intcount = 0;
                inti=0;
                intj=(int) a;
                while(i>=0&&j>=0){
                    if(i*i+j*j<s)
                        i++;//在圆内,向右移动,即向圆外移动
                    elseif(i*i+j*j>s)
                        j--;//在圆外,向下移动,即向圆内移动
                    else
                    {
                        j--;
                        count++;
                    }
                }
                if(a==(int)a)
                    System.out.println(count*4-4);
            else
                System.out.println(count*4);
        }
        sc.close();
 
    }
}
发表于 2016-08-28 21:32:27 回复(0)
import java.io.BufferedInputStream;
import java.util.Scanner;
 
/**
 * 求一个面积为S,以原点为圆心的圆上,点的横纵坐标均为整数的点的个数
 */
public class PointOnCircle {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        while (in.hasNext()) {
            int s = in.nextInt();
            /*
             * x * x + y * y = r * r = s
             */
            int r = (int)Math.sqrt(s);
            int ans = 0;
            //因为分为4个象限,最后是第一象限的4倍即可。而第一象限不取x=0,避免坐标轴上的点都重复取两次。
            for (int x = 1; x <= r; x++) {
                int y = s - x * x;
                int d = (int)Math.sqrt(y);
                //如果y开根号后的取整数值,再平方等于y,说明在x取整数时,此时y开根号是真的整数,不是小数。
                if (d * d == y) {
                    ans++;
                }
            }
            System.out.println(4 * ans);
        }
        in.close();
    }
}
发表于 2016-08-16 20:11:33 回复(0)
#include <stdio.h>
int jn(int s)
{
int i,j;
    int sum=0;
for(i=1;i<s;i++)
for(j=1;j<s;j++)
        {
if((i*i+j*j)==(s))
            sum++;
}
    return sum*4+4;
}
int main()
{
int s;
scanf("%d",&s);
    printf("%d",jn(s));
}
x、y轴的固定四个每次都有,只要对第一象限内的枚举乘以4+4就行了;也不懂对不对,大家看看。

发表于 2016-07-21 17:11:05 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int S = in.nextInt();
       
            int count = 0;
            for(int i=0;i*i<S;i++){
                int y = (int)Math.sqrt(S-i*i);
                if((i*i + y*y) == S){
                    count += 4;
                }
            }
            
            System.out.println(count);
        }
    }
}

发表于 2016-07-20 10:30:58 回复(0)
import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedInputStream(System.in));
		while (in.hasNext()) {
			int s = in.nextInt();
			/*
			 * x * x + y * y = r * r = s
			 */
			int r = (int)Math.sqrt(s);
			int ans = 0;
			for (int x = 1; x <= r; x++) {
				int y = s - x * x;
				int d = (int)Math.sqrt(y);
				if (d * d == y) {
					ans++;
				}
			}
			System.out.println(4 * ans);
		}
		in.close();
	}
}


发表于 2016-07-06 20:16:13 回复(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<math.h>
using namespace std;
intmain(){
    intS, count, temp, temp2;
    while(cin >> S){
        count =0;
        intn = sqrt(S);
        if(n*n == S)
            count =4;
        for(inti =1; i < ceil(sqrt(S)); i++){
            temp = S-i*i;
            temp2 = sqrt(temp);
            if(temp2*temp2 == temp)
                count +=4;
        }
        cout << count << endl;
    } 
    return0;
}
发表于 2016-06-23 20:27:50 回复(0)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			int s = in.nextInt();
			int sq = (int)Math.sqrt(s);//获取半径的整数部分
			int count = 0;
                        //统计地一象限和 x轴上的点
			for (int i = 1; i <= sq; i++) {
				int d = s - i * i;
				int dq = (int) Math.sqrt(d);
				if (d == dq * dq) {
					count += 1;
				}
			}
			System.out.println(4 * count);
		}
	}
}

发表于 2016-06-16 17:10:28 回复(0)
#include<iostream>
#include<math.h>
using namespace std;
int main(){
    int N,R,a,i,j;
    while(cin>>N){
        if(N==0)
            cout<<1<<endl;
        a=sqrt(N);
        R=0;
        for(i=0,j=a;i<=a&&j>0;){
            if(i*i+j*j==N){
            	R++;
            	i++;
            	j--;
        	}
            while(i*i+j*j<N) i++;
            while(i*i+j*j>N) j--;
        }
        cout<<4*R<<endl;
    }    
    return 0;
}

发表于 2016-06-16 11:16:28 回复(0)