Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2002 Accepted Submission(s): 752
Problem Description
There is a number N.You should output "YES" if N is a multiple of 2, 3 or 5,otherwise output "NO".
Input
There are multiple test cases, no more than 1000 cases.
For each case,the line contains a integer N. (0<N<1030)
For each case,the line contains a integer N. (0<N<1030)
Output
For each test case,output the answer in a line.
Sample Input
2 3 5 7
Sample Output
YES YES YES NO
Source
Recommend
hujie
这是寒假训练赛开始的一道题目,算是水题,但是由于自己还是渣渣,导致自己各种懵。。
首先是读题不仔细,没看清数据范围,导致上来就WA了。
然后是一开始没有想到简单的算法,判断是否能被2整除就要看最后一位是否为2的倍数,判断是否为5的倍数只要看最后一位是5或0,判断3的倍数要看各个位数相加和是否为三的倍数。
结合这些,就能轻易地做出这个题来。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
char a[100];
while(~scanf("%s",&a))
{
int l=strlen(a);
int sum=0;
for(int i=0;i<l;i++)
{
sum=sum+a[i]-48;
}
if(a[l-1]=='5'||a[l-1]=='0'||a[l-1]=='2'||a[l-1]=='4'||a[l-1]=='6'||a[l-1]=='8')
{
printf("YES\n");
}
else if(sum%3==0)
{
printf("YES\n");
}
else printf("NO\n");
}
}