【计蒜客系列】挑战难题24:函数规律

来源:互联网 发布:手机cad软件下载 编辑:程序博客网 时间:2024/06/02 02:28

题目来源:计蒜客

下面我将描述一个简单的函数:
f(8)=2
f(16)=1
f(32)=0
f(2048)=3
读入一个x(1≤x≤101000),请你找出f(x)的值。
输入包括一行,仅一个数字x。
输出包括一行,仅一个数字f(x)。
提示:
f(0)=1, f(1)=0, f(2)=0, f(3)=0, f(4)=0, f(5)=0, f(6)=1, f(7)=0, f(8)=2, f(9)=1
有时候看数据猜题意也是很重要的技能,如果你看到这里还不知道题意的话,那么我来告诉你,f(x)表示x的十进制表示中有多少个圈圈。


样例1
输入:
2048
输出:
3

注:

2048 = f(8) + f(4) + f(0) + f(2) = 2 + 0 + 1 + 0 = 3

#include <iostream>using namespace std;int calcZero(const string n) {int len = n.length();int count = 0;while (len > -1) {switch (n[len - 1]) {case '1':case '2':case '3':case '4':case '5':case '7':count += 0;break;case '0':case '6':case '9':count += 1;break;case '8':count += 2;break;default:break;}len--;}return count;}int main(int argc, char **argv) {string str;cin >> str;int ans = calcZero(str);cout << ans << endl;return 0;}


0 0
原创粉丝点击