sgu-105

来源:互联网 发布:win10多核优化 编辑:程序博客网 时间:2024/04/30 23:57

105. Div 3

time limit per test: 0.5 sec. 
memory limit per test: 4096 KB

 

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

acm能做的时间是短暂的,努力在有限的时间抓紧时间学习

分析

对小数据做一些观察,比如前6个数:

1,12,123,1234,12345,123456。

如果用1表示可以被3整除,0表示不能被3整除,则上述序列可表为:

0,1,1,0,1,1。

我们可以发现一个明显的模式:011,011,011,…。也就是一个不能被3整除,然后两个可以,然后又是一个不可以,循环往复。

这结论可以通过数学归纳法证明。

#include<stdio.h>int main(){    int n;    while(~scanf("%d",&n))        printf("%d\n",n/3*2+(n%3==2));    return 0;}

  

原创粉丝点击