Lucky 7 in the Pocket(ZOJ-4106)
Problem Description
BaoBao loves number 7 but hates number 4, so he refers to an integer x as a "lucky integer" if x is divisible by 7 but not divisible by 4. For example, 7, 14 and 21 are lucky integers, but 1, 4 and 28 are not.
Today BaoBao has just found an integer n in his left pocket. As BaoBao dislikes large integers, he decides to find a lucky integer m such that m>=n and m is as small as possible. Please help BaoBao calculate the value of m.
Input
There are multiple test cases. The first line of the input is an integer T (about 100), indicating the number of test cases. For each test case:
The first and only line contains an integer n (1<=n<=100), indicating the integer in BaoBao's left pocket.
Output
For each test case output one line containing one integer, indicating the value of m.
Sample Input
4
1
7
20
28Sample Output
7
7
21
35
题意:t 组数据,给出一个数 n,输出一个大于等于 n 的数 m,要求 m 能被 7 整除但不能被 4 整除
思路:首先判断能否被 7 整除,然后再判断能否被 4 整除,在这个过程中设置一个变量 x 来记录数的因子,每次令因子 +1 来减少枚举次数
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n;
scanf("%d",&n);
int x;
if(n%7==0)
x=n/7;
else
x=n/7+1;
while(1){
int res=x*7;
if(res%4!=0) {
printf("%d\n",res);
break;
}
else
x++;
}
}
return 0;
}