E - Find The Multiple

本文介绍了一种寻找特定整除数的方法,该数仅由0和1组成,并提供两种实现方案:深度优先搜索(DFS)和广度优先搜索(BFS)。通过递归与迭代方式,有效地生成满足条件的数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://vjudge.net/contest/65959#problem/E

题目大意

输入一个整数,输出一个能整除它的数,这个数很特别,它只包含0和1。

思路

1:只含有0,1的数可以这样求得,利用1,给它乘以10,给它乘以10后加1.每次对一它进行这两种操作
2:有两种方法,第一种DFS,用递归来做,终止递归的判别条件是,当这个数的长度超过19时,强制跳出当前的这条路,换一条路走。缺点是一条道走到黑,错过了中途好多的风景。另一种方法是BFS,利用循环和队列来做,因为是广搜,所以可以从前往后一步一步的判断。

DFS的代码

# include <stdio.h>
int m, flag;

void DFS(int x, long long y)
{
    if(x >19 || flag ==1)//flag是用来保证当找到这个数时可以,终止这条路, 
    {
        return;
    }
    if(y%m == 0)
    {
        flag = 1;
        printf("%lld\n",y);
        return ;
    }   
    DFS(x+1, y*10);
    DFS(x+1, y*10+1);
}

int main(void)
{   
    while(~scanf("%d",&m), m)
    {
       flag = 0;
       DFS(1, 1);
    }
    return 0;
} 

BFS的代码

# include <stdio.h>
# include <queue>

using namespace std;
int m;
void BFS(long long x)
{
    queue<long long>Q;
    Q.push(x);
    while(Q.size()) 
    {
        long long  y = Q.front();
        Q.pop();
        if(y%m == 0)
        {
            printf("%lld\n",y);
            return;
        }
        Q.push(y*10);
        Q.push(y*10+1);
    }
}
int main(void)
{
    while(~scanf("%d",&m),m)
    {
        BFS(1);
    }   
    return 0;
} 
OPTIONS: --Werror - If set, changes formatting warnings to errors --assume-filename=<string> - Override filename used to determine the language. When reading from stdin, clang-format assumes this filename to determine the language. --cursor=<uint> - The position of the cursor when invoking clang-format from an editor integration --dry-run - If set, do not actually make the formatting changes --dump-config - Dump configuration options to stdout and exit. Can be used with -style option. --fallback-style=<string> - The name of the predefined style used as a fallback in case clang-format is invoked with -style=file, but can not find the .clang-format file to use. Use -fallback-style=none to skip formatting. --ferror-limit=<uint> - Set the maximum number of clang-format errors to emit before stopping (0 = no limit). Used only with --dry-run or -n --help - Display available options (--help-hidden for more) -i - Inplace edit <file>s, if specified. --length=<uint> - Format a range of this length (in bytes). Multiple ranges can be formatted by specifying several -offset and -length pairs. When only a single -offset is specified without -length, clang-format will format up to the end of the file. Can only be used with one input file. --lines=<string> - <start line>:<end line> - format a range of lines (both 1-based). Multiple ranges can be formatted by specifying several -lines arguments. Can't be used with -offset and -length. Can only be used with one input file. -n - Alias for --dry-run --offset=<uint> - Format a range starting at this byte offset. Multiple ranges can be formatted by specifying several -offset and -length pairs. Can only be used with one input file. --output-replacements-xml - Output replacements as XML. --sort-includes - If set, overrides the include sorting behavior determined by the SortIncludes style flag --style=<string> - Coding style, currently supports: LLVM, Google, Chromium, Mozilla, WebKit. Use -style=file to load style configuration from .clang-format file located in one of the parent directories of the source file (or current directory for stdin). Use -style="{key: value, ...}" to set specific parameters, e.g.: -style="{BasedOnStyle: llvm, IndentWidth: 8}" --verbose - If set, shows the list of processed files --version - Display the version of this program
最新发布
07-23
<think>我们正在讨论clang-format工具,用户希望了解其命令行选项的详细用法。根据引用[1],我们知道clang-format是一个代码格式化工具,用于自动化统一代码风格。引用[1]还提到可以通过命令行或编辑器插件使用它。引用[2]则提到可以在保存文件时自动格式化,比如在VSCode中配置。 由于用户明确要求命令行选项的详解,我们将重点介绍clang-format的命令行选项。根据Clang官方文档,clang-format的命令行选项主要包括: 1. 指定样式(-style):可以是预定义样式(如LLVM, Google, Chromium, Mozilla, WebKit)或使用特定文件(如.clang-format或_clang-format)或直接指定样式字符串。 2. 指定输出方式(-i):表示直接修改文件,而不是输出到标准输出。 3. 指定文件或目录(当指定目录时会递归处理该目录下的文件)。 4. 其他选项如-assume-filename(用于在没有打开文件时指定文件名以确定样式)等。 下面我们将详细解释这些选项。</think>### Clang-Format 命令行选项详解 Clang-Format 是一个强大的代码格式化工具,通过命令行选项可以灵活控制格式化行为。以下是常用命令行选项的详细说明: #### 1. **基本格式化选项** - **`-i`**:直接修改源文件(原地格式化) 示例:`clang-format -i main.cpp` 将直接修改 main.cpp 文件 - **`--style=<value>`**:指定代码风格 支持预定义风格(如 `LLVM`, `Google`, `Chromium`)或配置文件路径: ```bash clang-format --style=Google file.cpp # 使用预定义风格 clang-format --style=file file.cpp # 使用目录中的 .clang-format 文件 ``` - **`-fallback-style=<value>`**:当无配置文件时的回退样式(默认:`LLVM`) 示例:`clang-format -fallback-style=WebKit file.h` #### 2. **输出控制选项** - **`-output-replacements-xml`**:输出 XML 格式的替换指令(用于工具集成) 示例:`clang-format -output-replacements-xml file.c` - **`-dump-config`**:输出当前生效的配置 示例:`clang-format --style=Google -dump-config > .clang-format` - **`-n`**:仅检查是否需要格式化(不修改文件),返回 0 表示无需修改 示例:`clang-format -n --Werror *.h` 结合 `-Werror` 可集成到 CI #### 3. **范围控制选项** - **`-lines=<start>:<end>`**:仅格式化指定行范围 示例:`clang-format -lines=10:20 file.py` - **`-offset=<N>`** + **`-length=<L>`**:格式化从偏移量 N 开始的 L 个字符 示例:`clang-format -offset=100 -length=50 file.java` #### 4. **诊断与调试选项** - **`-Werror`**:将警告视为错误(适用于 CI 检查) 示例:`clang-format -i -Werror src/*.c` - **`-verbose`**:显示详细处理信息 示例:`clang-format -verbose --style=LLVM *.cpp` #### 5. **特殊场景选项** - **`-assume-filename=<name>`**:强制指定文件名(影响语言检测) 示例:`cat input | clang-format -assume-filename=test.h` - **`-sort-includes`**:启用/禁用 include 排序(需配置文件中开启) 示例:`clang-format -sort-includes=false file.cc` > 完整选项列表可通过 `clang-format --help` 查看,或参考官方文档[^1]。 ### 配置优先级说明 Clang-Format 按以下顺序确定配置: 1. 命令行 `--style` 直接指定的配置 2. 项目目录中的 `.clang-format` 或 `_clang-format` 文件 3. 用户全局配置(`~/.clang-format`) 4. `-fallback-style` 指定的回退配置 ### 典型工作流示例 ```bash # 1. 生成默认配置文件 clang-format -style=Google -dump-config > .clang-format # 2. 批量格式化整个项目 find src/ -name '*.cpp' -exec clang-format -i {} \; # 3. CI 集成检查 clang-format --Werror --dry-run src/*.c ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值