【代码规范】竞赛 C/C++ 通用代码规范

前言 

此规范为通用规范,为参加竞赛队伍统一代码风格而设定。

整体规范

1. 空行:在长代码的条件下,若代码量较大,应给不同作用的代码块中间用适当的空行隔开。若代码量过大,因适当的使用函数。

2. 括号:左括号不换行。

3. 缩进:推荐用两个空格作为缩进。

5. 变量名/函数名:(1) 应使用和作用有关的英文,可由多个单字组成。

                              (2) 简单变量名(一两个字母)应作用明显,不宜过多。

                              (3) 推荐使用大驼峰命名法(帕斯卡命名法):多个单词组成的名字应每个单词首字母大写。

                               例如 a, b, c, d 此类命名不宜过多。

                               首字母大写:PrintEmployeePaychecks()   

                               下划线:print_employee_paychecks()

                               错误:Print_Employee_Paychecks()(下划线 + 首字母大写)、printemployeepaychecks() (全小写)

5. 成对书写:输入'()','[]','{}' 时应成对输入,避免疏漏。

6. 注释:注释时,'//' 应与代码行相隔两个空格,'//'后再添加一个空格。若在一行上,不宜过长。若有条件,推荐使用英文注释。

int left = 1, right = n;  // left is the leftmost index of Array;

7. 对齐:应严格按照代码格式进行缩进。'{' 不换行,且每个 '{}' 直接的代码块应增加一个缩进格式!

8. 空格:每个运算符的两旁应严格增加一个空格,由逗号分隔的多个表达式前,严格用一个空格!(第一个表达式除外)

例如:if (a == b)、sum += a;、a && b、power(a, b, c, d, c + 1)  红色下划线处均为一个空格。

细节规范

1. 头文件:引入头文件,"#include" 与 "<...>" 之间应有一个空格。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

2.  定义常量:若题目已经给定常量,应用 "const" 类型定义,并给上界适当留出空间。

const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;

3. 重命名:若题目需要使用到 long long ,应使用 "LL" 进行重命名。

typedef long long LL;

4. 定义函数:函数名字应使用英文,并以作用相关。并遵循下图规则。

     

5. 主函数:规则如下。

  

6. 判断语句:规则如下。

  

7. 循环语句:while。规则如下。

  

8. 循环语句:for。规则如下。

  

9. 输入语句:规则如下

  

10. 输出语句:规则如下。

  

参考代码片段

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int num[maxn], dp[maxn][3];

int dfs(int len, bool last6, bool limit) {
  if (len == 0) return 1;
  if (limit == false && dp[len][last6] != 0) return dp[len][last6];
  int cnt = 0, maxx = limit ? num[len] : 9;
  for (int i = 0; i <= maxx; i++) {
    if ((i == 2 && last6 == true) || i == 4) continue;
    cnt += dfs(len - 1, i == 6, limit && i == maxx);
  }
  return limit ? cnt : (dp[len][last6] = cnt);
}

int solve(int x) {
  int k = 0;
  memset(num, 0, sizeof num);
  memset(dp, 0, sizeof dp);
  while (x) {
    num[++k] = x%10;
    x /= 10;
  }
  return dfs(k, false, true);
}

int main() {
  int a, b;
  while (scanf("%d %d", &a, &b) != EOF && (a||b)) {
    printf("%d\n", solve(b) - solve(a - 1));
  }

  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章