zoukankan      html  css  js  c++  java
  • 杭电ACM2098--分拆素数和

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2098

    这是源码。其实我本不想拿出源码,毕竟源码很容易被复制。

    我这里刚开始出错的地方有

    0_0_12811458_22064.cpp
    0_0_12811458_22064.cpp(9) : error C2668: “sqrt” : 对重载函数的调用不明确
            includemath.h(626): 可能是“long double sqrt(long double)”
            includemath.h(578): 或       “float sqrt(float)”
            includemath.h(200): 或       “double sqrt(double)”
            试图匹配参数列表“(int)”时
    0_0_12811458_22064.cpp(12) : error C2668: “sqrt” : 对重载函数的调用不明确
            includemath.h(626): 可能是“long double sqrt(long double)”
            includemath.h(578): 或       “float sqrt(float)”
            includemath.h(200): 或       “double sqrt(double)”
            试图匹配参数列表“(int)”时

    我并不是太明白这是为什么。然后度娘给我答案,说int i,n; i<=int(sqrt(float(n)));如果是直接sqrt的话,n和i都是int型,而sqrt结果却是double型的,系统自身没有强制转化的功能。

    if(Is_Prime(i)&&Is_Prime(n-i)){
         cont++;
         if(i==n-i) cont--;
       }

    需要判断重合的那个数,我一直没想到。

    <span style="font-size:18px;">#include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include <iostream>
    using namespace std;
    int Is_Prime(int n)
    {
    	int r,i;
    	if(n<=1)
    		return 0;
    	for(i=2;i<=int(sqrt(float(n)));i++)
    		if(n%i==0)
    			break;
    		if(i>int(sqrt(float(n))))
    			return n;
    		else
    			return 0;
    }
    
    int main()
    {
    	int n,cont;
    	while(cin>>n&&n){
    		cont=0;
    		for(int i=2;i<=n/2;i++){
    			if(Is_Prime(i)&&Is_Prime(n-i)){
    					cont++;
    					if(i==n-i) cont--;
    			}
    		}
    		cout<<cont<<endl;
    	}
    	return 0;
    }
    
    /*
    int main()
    {
    	int i,j,k,n;
    	int m;
    	int a[1500];
    	while (scanf("%d",&m)!=EOF&&m!=0)
    	{
    		j = 0;
    		for (i=0;i<m;i++)
    			if (abc(i))
    			{
    				a[j] = i;
    				j++;
    			}
    		k = 0;
    		for (i=0;i<=j/2;i++)
    			for (n=0;n<j;n++)
    			if (a[i]+a[n]==m)
    			k++;
    		printf("%d
    ",k);
    	}
    	return 0;
    }  */</span>


     

  • 相关阅读:
    Centos 卸载openjdk
    Hadoop安装之Hive集成与mysql安装
    Hadoop安装-Spark Windows 环境 pycharm开发环境搭建
    Hadoop安装—Spark安装
    Hadoop安装-伪分布式
    Hadoop安装之JDK在Centos虚拟机中安装
    SQL server 数据库调用远程数据库存储过程的实现方法
    以梦为马,铸就美好人生
    kettle实战演练——批量解压有密码的rar文件,并生成xml文件
    Linux diff命令
  • 原文地址:https://www.cnblogs.com/acmwangpeng/p/5524890.html
Copyright © 2011-2022 走看看