1042: 二哥的困惑 Ⅴ
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 497 Solved: 171
[Submit][Status][Web Board]
Description
There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Given first N elements of that sequence. You must determine amount of numbers in it that are divisible by 3.
Input
Input contains N (1<=N<=231 - 1).
Output
Write answer to the output.
Sample Input
4
Sample Output
2
HINT
【分析】
-
题意是前n个数能被3整除的个数(求有几个数的各位和是3的倍数)
-
把前几个列出来是可以发现规律的,即对3取模,结果为2或0的n都可以被3整除
-
是统计个数,所以,还要除以3,如果余数是0或1的,t乘2即可,如果余数是2的,乘2了还要加一,因为从这里开始又多了一个;注意是i进行乘除不是n;因为每3个会有2个是,所以乘2;
#include<stdio.h>
int main()
{int n,i,t,j;while(~scanf("%d",&n)){i=n/3;j=n%3;if(j==0||j==1)t=2*i;if(j==2)t=2*i+1;printf("%d\n",t);}return 0;
}