Auxiliary Project
Anna has just nished her course project. She has a lot of seven-segment LED displays as leftovers and a small power source. Each display consumes power proportionally to the number of lit segments, e.g. ‘9’consumes twice more power than ‘7’.
Anna wonders what is the maximum possible sum of digits she is able to achieve, if her power source is able to light n segments, and she wants to light exactly n segments.
Input
The single line of the input contains one integer n —– the number of segments that should be lit(2<=n<=10^6).
Output
Output a single integer —– the maximum possible sum of digits that can be displayed simultaneously.
Sample Input
4
7
6
Sample Output
4
11
14
如圖的數字顯示方式,每個線段需要一個能量,有n個能量,在儘可能把所有能量都用上的情況下是的組成的所有的數字之和最大。
通過模擬
n | output |
---|---|
2 | 1 |
3 | 7 |
4 | 4 |
5 | 8 |
6 | 14 |
7 | 11 |
8 | 15 |
9 | 21 |
10 | 18 |
2個能量是1
3個能量是7
4個能量是4
5個能量是3+2個能量 7+1=8
6個能量是3+3—7+7=14
7個能量是3+4個能量 7+4=11
8個能量是3+3+2個能量 7+7+1=15
發現都是由2、3、4個能量爲基礎來組成的
so直接對3取模
n%3=0 全是7
n%3=1 把 3+3+3+3+…+3 中最後一個3換成4
n%3=2 最後一個是2點能量3+3+3+…..+2
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
int m = n % 3;
int c = n / 3;
if (m == 0)
{
return 0 * printf("%d\n", c * 7);
}
if (m == 1)
{
return 0 * printf("%d\n", (c - 1) * 7 + 4);
}
if (m == 2)
{
return 0 * printf("%d\n", c * 7 + 1);
}
}