Accurately Say "CocaCola"!
Time Limit: 2 Seconds Memory Limit: 65536 KB
In a party held by CocaCola company, several students stand in a circle and play a game.
One of them is selected as the first, and should say the number 1. Then they continue to count number from 1 one by one (clockwise). The game is interesting in that, once someone counts a number which is a multiple of 7 (e.g. 7, 14, 28, ...) or contains the digit '7' (e.g. 7, 17, 27, ...), he shall say "CocaCola" instead of the number itself.
For example, 4 students play this game. At some time, the first one says 25, then the second should say 26. The third should say "CocaCola" because 27 contains the digit '7'. The fourth one should say "CocaCola" too, because 28 is a multiple of 7. Then the first one says 29, and the game goes on. When someone makes a mistake, the game ends.
During a game, you may hear a consecutive of p "CocaCola"s. So what is the minimum number that can make this situation happen?
For example p = 2, that means there are a consecutive of 2 "CocaCola"s. This situation happens in 27-28 as stated above. 27 is then the minimum number to make this situation happen.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 100) which is the number of test cases. And it will be followed by T consecutive test cases.
There is only one line for each case. The line contains only one integer p (1 <= p <= 99).
Output
Results should be directed to standard output. The output of each test case should be a single integer in one line, which is the minimum possible number for the first of the p "CocaCola"s stands for.
Sample Input
2
2
3
Sample Output
27
70
题意:从1开始报数,只要报的数中含7或者是7的倍数,就喊cocacola;问当从x开始连续喊出n个cocacola时,这个x时多少。即求最小的x。
思路:自己用的模拟的方法
。 如果第一次喊出了coca,就进行标记。然后接下来如果喊出coca就继续处理,否则就撤销标记。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
int go = 7;
int cnt = 0; // cnt为从开始喊coca开始一共连续喊了多少个;
int flag = 0; // 表示是否为第一个喊coca了;
for(int i = 7; ; i++)
{
if(cnt == n)
{
printf("%d\n", go);
break;
}
if(i%7 == 0)
{
cnt++;
if(!flag)
{
flag = 1;
go = i;
}
}
else
{
int mid = i;
int flag1 = 0;
while(mid)
{
if(mid % 10 == 7)
{
flag1 = 1;
break;
}
mid /= 10;
}
if(flag1)
{
cnt++;
if(!flag)
{
flag = 1;
go = i;
}
}
else
{
cnt = 0;
flag = 0;
}
}
}
}
return 0;
}
还有一种是找规律的方法, 因为题目的数据很小 1 <= p <= 99。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int testcase, p;
scanf("%d", testcase);
while (testcase--)
{
scanf("%d", &p);
switch(p)
{
case 1: cout << "7" << endl;
break;
case 2: cout << "27" << endl;
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10: cout << "70" << endl;
break;
case 11: cout << "270" << endl;//不过这个需要注意,这个很容易就忽略!
break;
default : cout << "700" << endl;
break;
}
}
}