zoukankan      html  css  js  c++  java
  • 蓝桥杯-PREV3-带分数

    有人管蓝桥杯叫暴力杯,现在感觉还是挺贴切的。看到这题首先想到让i从1到n循环,首先判断i中无重复数字,再怎样判断能否用剩下的数构成n - i的假分数。之后看了题解。发现思路错了。

    总结两点:

    1、蓝桥杯的编程题大多暴力枚举,首先从这个方向想;

    2、next_permutation这个函数解决排列的问题比较方便,就是函数名长了点,要记一下;

    • 暴力枚举
      588B C++ 正确 100 296ms 940.0KB
      #include "bits/stdc++.h"
      using namespace std;
      int n, cnt;
      char s[] = "123456789";
      int parse(const char* s, int pos, int len) {
          int res = 0;
          while (len) {
              res = res * 10 + s[pos + --len] - '0';
          }
          return res;
      }
      int main() {
          scanf("%d", &n);
          do {
              for (int i = 1; i < 9; i++) {
                  int m = parse(s, 0, i);
                  if (m > n) {
                      break;
                  }
                  for (int j = 1; j < 9 - i; j++) {
                      int a = parse(s, i, j);
                      int b = parse(s, i + j, 9 - i - j);
                      if (a % b == 0 && m + a / b == n) {
                          cnt++;
                      }
                  }
              }
          } while (next_permutation(s, s + 9));
          printf("%d
      ", cnt);
          return 0;
      }
  • 相关阅读:
    leetcode笔记-1 twosum
    pythoon_interview_redit
    Python 二维列表
    py xrange
    python 垃圾回收机制
    python 链表
    Python 面试总结
    linux 目录
    Linux 文件名颜色
    实践是检验真理的唯一标准
  • 原文地址:https://www.cnblogs.com/Angel-Demon/p/10446998.html
Copyright © 2011-2022 走看看