判断一个数是否为整数

Sol_Jee 2011-04-19 10:58:26
判断是否是整数

Time Limit: 1000MS

Memory Limit: 65536KB

Submissions: 515

Accepted: 146

Description

Input

输入一个数,判断是否是整数,如果是整数则输出“Yes”,否则输出“No” 输入有多组测试数据

Output

如果是整数则输出“Yes”,否则输出“No”

Sample Input

1

2.12

3.000

Sample Output

Yes

No

Yes

附上我的代码
#include<math.h>
#include<stdio.h>
main()
{
double n,a;
while(scanf("%lf",&n))
{


n=sqrt(n);
a=(int)n;
if(a==n)
printf("Yes\n");
else
printf("No\n");
}}
还有一个#include<stdio.h>
main()
{
float a;
while(scanf("%f",&a))
{if (a!=(int)a)
printf("No\n");
else printf("Yes\n");
}
}
输出都是对的
全部都是超时
有没有更好的算法,求高手解答.................................
...全文
4282 59 打赏 收藏 转发到动态 举报
写回复
用AI写文章
59 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChunQiuKun 2013-11-27
  • 打赏
  • 举报
回复
直接用if(fabs(floor(m+0.5)-m)<=precision)
footkicker 2012-02-23
  • 打赏
  • 举报
回复
其实不用这样
用 double 型读入 乘以10 然后在 整除10
如果余数为零 那么就是一个整数
反之 则是小数。
biggerbone 2011-07-11
  • 打赏
  • 举报
回复
#include<stdio.h>
int main()
{
float x[] ={1,1.2,3.00,2.9,4.00,5.2,65.00};
int i = 0;
int size = sizeof(x)/4;
for(;i<size;i++)
{
if(x[i]/(int)(x[i]) == 1)
printf("yes\n");
else
printf("no\n");
}
return 0;
}

不知道这样是不是满足...
楼上说取模,应该也是一个好办法.但是,非整数不能取模运算呀.
用整数除,整数除自己应该等于1,但是,小数除以强制后的整数应该不等于1.
在哪试的超时,我也去试试看.是ACM还是??
qingyang_0810 2011-04-25
  • 打赏
  • 举报
回复
坐等结果~~~
koko0123 2011-04-25
  • 打赏
  • 举报
回复
转成字符串,用正则表达式匹配.就可以了。这个应该快点
deping_chen 2011-04-25
  • 打赏
  • 举报
回复
这个需要状态机判断,楼主的算法很怪异.怎么会需要1000ms,太慢了,1ms都是太多了.
nevergiveupc 2011-04-25
  • 打赏
  • 举报
回复
[Quote=引用 50 楼 ysystram 的回复:]
void JudgeInteger(const wchar_t * strPara)
{
DWORD dwStartTime = GetTickCount(), dwEndTime;
bool bPossibility = false;

int nCount = (int) wcslen(strPara), iPos;
for ( iPos = 0; iPos < nCount; i……
[/Quote]这个正解啊!
双杯献酒 2011-04-25
  • 打赏
  • 举报
回复
就这么一个判断就要Time Limit: 1000MS, 还大家都超时,别开玩笑了。

scanf这个函数会等待键盘输入,输入足够快就不会超时。
这个不是程序太慢,是你手太慢。
yujihui 2011-04-25
  • 打赏
  • 举报
回复
呃。。。前面的代码不对,换成这样:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
float f = 5.000001;

clock_t start = clock();
int yes = 0;
unsigned long * l = (unsigned long *)&f;
unsigned long flag = *l & (-(*l));
unsigned long e = ((*l & 0x7f800000) >> 23) - 127;
if(((flag << e) > 0x400000) || ((flag << e) == 0))
{
yes = 1;
}
clock_t end = clock();

printf("time elapse: %d\n", (end - start));
if(yes)
{
printf("Yes.\n");
}
else
{
printf("No.\n");
}

system("pause");
}
yujihui 2011-04-25
  • 打赏
  • 举报
回复
根据浮点数的存贮格式来判断即可,这里是一个判断 单精度 是否为整数的程序, 双精度也是相似的.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
float f = 2.000001;

clock_t start = clock();
int yes = 0;
unsigned long * l = (unsigned long *)&f;
if(((*l & 0x7f800000) > 0x3f800000) && ((*l & 0x7fffff) == 0))
{
yes = 1;
}
clock_t end = clock();

printf("time elapse: %d\n", (end - start));
if(yes)
{
printf("Yes.\n");
}
else
{
printf("No.\n");
}

system("pause");
}
i_saw_you 2011-04-25
  • 打赏
  • 举报
回复
比较向上取整和向下取整的值是不是一样就完了么,要是你乐意 还可以用fmod 函数对1取模
wuhuaguoshu 2011-04-25
  • 打赏
  • 举报
回复
费时是怎么说呢 谁来讲讲呗
yxfxidian 2011-04-25
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
int main(){
float a;
while(true){
cin >> a;
if((int)(a * 10) % 10 == 0)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
DavidGaku 2011-04-25
  • 打赏
  • 举报
回复
void JudgeInteger(const wchar_t * strPara)
{
DWORD dwStartTime = GetTickCount(), dwEndTime;
bool bPossibility = false;

int nCount = (int) wcslen(strPara), iPos;
for ( iPos = 0; iPos < nCount; iPos++)
{
if (bPossibility)
{
if (strPara[iPos] != '0')
{
cout<< "It is not an integer!"<<endl;
break;
}
}
if (strPara[iPos] == '.')
{
bPossibility = true;
}
}

if (!bPossibility || (iPos == nCount))
{
cout<< "It is an integer!"<<endl;
}

dwEndTime = GetTickCount();
cout<< "It costs " << (dwEndTime - dwStartTime) << " ms" << endl;
}
DavidGaku 2011-04-25
  • 打赏
  • 举报
回复
同意楼上, 正则
mxpgong 2011-04-24
  • 打赏
  • 举报
回复
还有一个问题,如果输入的数字精度太多(如0.00000000000000001),它会把它不能表示的舍掉,所以问题是在string中还是在double中结果也不一样。
mxpgong 2011-04-24
  • 打赏
  • 举报
回复
double在内存是根据IEEE754标准存储的,如果能根据内存的内容判断或许可以。
hewengao401 2011-04-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 happymawolf 的回复:]

从浮点到整数的类型转换是耗时的,可以读入字串,然后查找有没有'.',如果有就no,否则yes
[/Quote]
3.000是整数。单单这样不行的
Boyce_Gao 2011-04-23
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 jacklinmiao 的回复:]
用字符串接收输入的数,然后查字符串中是否有小数点。
如果没有小数点,直接输出“YES”
如果有小数点,若小数点后面的字符全是“0”,输出“YES”,否则输出“NO”。
[/Quote]

顶 这个算法试了没有? 不过还可以试下 对于有小数点的可以把小数点后面的数字全部一个一个的加起来总和看等于0不,等于0就是整数 否则NO
GarrosII 2011-04-23
  • 打赏
  • 举报
回复
楼主试试这个。。

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[1000];
int t,i,j;
int len;
while(cin>>str)
{
len=strlen(str);
for(i=0;i<len;i++)
if(str[i]=='.')
break;
if(i==len)
cout<<"Yes"<<endl;
else
{
for(j=i+1;j<len;j++)
if(str[j]!='0')
break;
if(j==len)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
return 0;
}

加载更多回复(36)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧