题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电

http://poj.org/problem?id=1218清华

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1350浙江

原本以为小小的一道水题不值得去总结,但是发现却有好多方法,好神奇啊

方法一:数据挺小的模拟就好

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std; int a[110];
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
memset(a, 0, sizeof(a));
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
for(int j=i; j<=n; j+=i)
{
a[j]=a[j]^1;
}
}
int ans=0;
for(int i=1; i<=n; i++)
{
if(a[i]==1)
ans++;
} printf("%d\n", ans);
}
return 0;
}

方法二:其实犯人是否能够逃跑是有规律可循的,门有开/关两种形式,只要犯人最终开和关的总次数是奇数就好;例如3 1开3关 ,开关总和为偶数; 例如4 1开2关4开,开关总和为奇数  其实就是看一个数是否有奇数个因子;

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std; int solve(int x)
{
int cnt=;
for(int i=; i<=x; i++)
{
if(x%i==)
cnt++;
}
if(cnt%==)
return ;
else
return ;
}
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
int ans=;
for(int i=; i<=n; i++)
{
if(solve(i))
ans++;
}
printf("%d\n", ans);
}
return ;
}

第三种:是看别人才知道的,真的是世界之大无奇不有;

原理是:只有完全平方数才有奇数个因子,例如3, 9,所以只需要求出n以内的完全平方数的个数即可;

n以内的 完全平方数的个数=sqrt(n);

#include<cstdio>
#include<cmath>
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",(int)sqrt(n));
}
return 0;
}

  

HDU 1337 && POJ 1218&& zju 1350 方法总结的更多相关文章

  1. Coins HDU - 2844 POJ - 1742

    Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...

  2. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)

    Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...

  3. hdu 2844 poj 1742 Coins

    hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...

  4. HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)

    Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...

  5. HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)

    Description Too worrying about the house price bubble, poor Mike sold his house and rent an apartmen ...

  6. 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]

    学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...

  7. hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

    Tunnel Warfare                                                             Time Limit: 4000/2000 MS ...

  8. Eight hdu 1043 poj 1077

    Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name ...

  9. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

随机推荐

  1. iOS-setNeedsLayout等布局方法

    列举下iOS layout的相关方法: layoutSubviews layoutIfNeeded setNeedsLayout setNeedsDisplay drawRect sizeThatFi ...

  2. servlet之模板方法和多线程

    接触了一小段时间的servlet,以下就总结一下关于servlet的两个方面的知识,一个是模板方法的应用.另外一个是servlet多线程产生的原因. 1. 模板方法设计模式 定义一个操作中的算法的骨架 ...

  3. Castle 整合.NET Remoting

    今天研究了一下Castle的Remoting Facility.记录如下: 微软以前使用COM/DCOM的技术来处理分布式系统架构,通过Client端的Proxy代理程序来呼叫远程Server机器上的 ...

  4. C语言数据类型大小

    数据类型大小是由操作系统和编译器共同决定的,但必须满足: short和int至少为16bit:long至少为32bit: short不能超过int,int不能超过long. 在主流编译器中,32位机和 ...

  5. 修改yum源为阿里云的

    将Centos的yum源更换为国内的阿里云源 author:headsen chen date:2018-04-28  13:33:41 1.备份  mv /etc/yum.repos.d/CentO ...

  6. JavaScript学习笔记-构造函数

    什么是构造函数 简单说构造函数是类函数,函数名与类名完全相同,且无返回值.构造函数是类的一个特殊成员函数. JavaScript构造函数 * 在JavaScript的世界里没有类的概念,JavaScr ...

  7. 巨蟒python全栈开发-第6天 is&==

    1.小数据池 2.id 3.decode和encode 小数据池 #小数据池:不要死磕就行#python为了简化,搞出来的一个东西 ID (1)# id()函数可以帮我们查看一个变量的内存地址# a= ...

  8. Oracle 的安全保障 commit &checkpoint

    Oracle 的安全 commit &checkpoint commit ---lgwr 事务相关的操作,保证事务的安全. commit标志着事务的结束.意味着别人对你事务操作的结果可见. c ...

  9. Virtual Private Cloud 专有网络 软件定义网络的方式 私有网络 大流量视频、直播类业务

    私有网络 VPC_云上网络空间_自定义网络 - 腾讯云 https://cloud.tencent.com/product/vpc 私有网络 VPC 简介 私有网络(Virtual Private C ...

  10. SpringMVC 返回的 json 中去除某些不必要的属性

    修改返回的Model,在对应的属性的get方法上使用 com.fasterxml.jackson.annotation.JsonIgnore 注解即可. 如 @JsonIgnore(true) pub ...