zoukankan      html  css  js  c++  java
  • #C++初学记录(acm试题#预处理)

    **C - Lucky 7 in the Pocket **

    BaoBao loves number 7 but hates number 4, so he refers to an integer as a "lucky integer" if 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 in his left pocket. As BaoBao dislikes large integers, he decides to find a lucky integer such that and is as small as possible. Please help BaoBao calculate the value of .

    Input
    There are multiple test cases. The first line of the input is an integer (about 100), indicating the number of test cases. For each test case:

    The first and only line contains an integer (), indicating the integer in BaoBao's left pocket.

    Output
    For each test case output one line containing one integer, indicating the value of .

    Sample Input
    4
    1
    7
    20
    28
    Sample Output
    7
    7
    21
    35
    正确代码

    	#include <iostream>  
    	  
    	using namespace std;  
    	  
    	int a[1000];  
    	  
    	int main(){  
    	    for (int i = 1; i < 1000; ++i) {  
    	        if(i%7==0&&i%4!=0){  
    	            a[i]=1;  
    	        }  
    	    }  
    	    int T;  
    	    cin>>T;  
    	    while (T--){  
    	        int n;  
    	        cin>>n;  
    	        for (int i = n; i < 1000; ++i) {  
    	            if(a[i]==1){  
    	                cout<<i<<endl;  
    	                break;  
    	            }  
    	        }  
    	    }  
    	    return 0;  
    	}  
    
    

    题意理解
    首先,题意规定了n的取值范围,因为取值范围足够小,仅仅在1-100之间,因此我选择使用预处理即计算1-1000之内所有可以被7整除而不能被4整除的数,接着用for循环进行查找数据得出。

  • 相关阅读:
    hdu 5696 区间的价值 单调栈+rmq
    bzoj 3039: 玉蟾宫 单调栈或者悬线法求最大子矩阵和
    bzoj 2435: [Noi2011]道路修建 dfs
    Codeforces gym 100971 D. Laying Cables 单调栈
    codeforces GYM 100971F 公式题或者三分
    UVA 10539
    BZOJ 1079: [SCOI2008]着色方案 DP
    UVA 11426
    UVA 11728
    UVA 10090
  • 原文地址:https://www.cnblogs.com/xiaofengqaq/p/10834085.html
Copyright © 2011-2022 走看看