[CCF自用]201912-1-报数(C++和Python)
2020-04-02 08:00
1456 查看
以前经常看CSDN,但是这还是第一次自己发,主要是为了记录一下自己CCF考试的学习过程,让自己去输出一些东西。自己接触编程还不是太久。还在学习中,有很多不足的地方,欢迎大家批评指正。晚安。
题目如上,这是一个报数游戏,4个人进行报数,每当报到含有7或者为7的倍数的数字时,就直接跳过。4个人总共要报n次数,跳过的数字不算其中。
![ccf官网评分](https://img-blog.csdnimg.cn/20200310232823102.png
最初开始自己写的答案,写完之后虽然通过了,CCF上也给了100分,但是自己都觉得很懵,很没有逻辑,代码如下:
(自己写的第一版)
`#include <iostream> using namespace std; int num_bit(int num); bool control_7(int num); void number(int times); void Print_result(); int count_Game[4] = { 0, 0, 0, 0 };//用于记录4个人分别跳过的次数 int main() { int n; //n表示需要报数几次 cin >> n; number(n); Print_result(); return 0; } void number(int times) { //报数功能,每报数一次就进行判断是否符合游戏要求。 for (int i = 1; i <= times;) { bool condition = control_7(i); if (condition) { int j = (i-1) % 4; //计算当前跳过的数字是谁报的 ++count_Game[j]; //计数器+1 ++times; //因为跳过不计入次数,但是i在自增,所以把次数times也增加1,让跳过不影响报数次数。 } ++i; } } int num_bit(int num) { //判断当前数字的位数,返回位数 if (num > 0 && num <= 9) { return 1; } else if (num > 9 && num <= 99) { return 2; } else if (num > 99 && num <= 999) { return 3; } } bool control_7(int num) { //判断当前报数到的数字中是否含有7或者能被7整除,如果符合要求返回True,否则返回False int bit_num = num_bit(num); int bit_storage; if (num % 7 == 0) { //判断是否是7的倍数 return true; } for (int i = 0; i < bit_num; ++i) { //逐位判断是否含有7 bit_storage = num % 10; num = num / 10; if (bit_storage == 7) return true; } return false; } void Print_result() { //打印结果 cout << count_Game[0] << endl; cout << count_Game[1] << endl; cout << count_Game[2] << endl; cout << count_Game[3] << endl; }`
后来看了CSDN上别人的解答,感觉豁然开朗,确实比我自己的想法好很多。也简洁很多。
(查看别人的答案后写的)
好像消耗时间长了一些,暂时没想到是什么原因,但是总体上来说,这样写的代码更加简洁。
#include <iostream> using namespace std; bool haveSeven(int n) { //判断当前报数知否含有‘7’,是则返回True,否则返回False while (n) { if (n % 10 == 7) return true; else n /= 10; } return false; } int main() { int n; //n记录报数次数 constexpr int N = 4; int counter[N] = { 0,0,0,0 }; //记录每个人跳过次数 int cnt = 1; //有效的报数次数,有7或被7整除则为无效报数 int count = 1; //当前数数的次数,既包含有效的报数也包含无效跳过的报数 cin >> n; while (cnt <= n) { //当有效报数次数cnt大于规定报数次数n时结束循环 if (count % 7 == 0 || haveSeven(count)) //如果含有7或者7的倍数,则把当前报数人跳过次数+1 ++counter[(count - 1) % 4]; else //不含7或不被7整除。有效报数次数+1 ++cnt; ++count; //当前报数过的次数+1 } for (int i = 0; i < N; ++i) { cout << counter[i] << endl; } return 0; }
(Python版本的写法)与上面C++思想一样,换成Python之后代码更加简短,但是耗时和空间占用更大。
n = int(input()) # input返回str类型,所以需要转成Int num = 1 cnt = 1 counter = [0, 0, 0, 0] while num <= n: if cnt % 7 == 0 or '7' in str(cnt): counter[(cnt - 1) % 4] += 1 else: num += 1 cnt += 1 for i in range(0, 4): print(counter[i])
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- CCF 201912-1 《报数》 C++满分题解
- CCF CSP 小明放学 c++ java python csp201812_2 100分
- CCF CSP 数据中心 c++ python csp201812_4 100分
- CCF CSP 再卖菜 c++ java python 201809_4
- Python 学习笔记(自用)(有C++基础)(连载)
- CCF计算机软件能力认证试题:201912-1 报数
- CSP CCF 201912-1 报数 (Java) 简单方法
- CCF CSP 跳一跳 c++ java python 201803_1 100分
- CCF CSP 买菜 Java python csp201809_2 100分
- LeetCode 141 — Linked List Cycle(C++ Java Python)
- Python 自用代码(某方标准类网页源代码清洗)
- C++&Python——【USACO 5.4.1】——Canada Tour
- LeetCodeOJ--Remove Duplicates from Sorted Array(c++ && python版本)
- 再探C/C++扩展Python
- 2019年c++/c,java,python,前端,数据结构,ps等资料大全
- c++ python 计算100以内的素数
- 软件编程工具书(java、C、C++、HTML、JS、SSH、Python等)
- Embedding Python in C/C++: Part I
- Python3.4安装scrapy库的时候出现Error: Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat)
- [ LeetCode ]#29. Divide Two Integers(两数相除 C++ & Python)