UVA 10198 Counting
Counting |
The Problem
Gustavo knows how to count, but he is now learning how write numbers. As he is a very good student, he already learned 1, 2, 3 and 4. But he didn't realize yet that 4 is different than 1, so he thinks that 4 is another way to write 1. Besides that, he is
having fun with a little game he created himself: he make numbers (with those four digits) and sum their values. For instance:
132 = 1 + 3 + 2 = 6
112314 = 1 + 1 + 2 + 3 + 1 + 1 = 9 (remember that Gustavo thinks that 4 = 1)
After making a lot of numbers in this way, Gustavo now wants to know how much numbers he can create such that their sum is a number n. For instance, for n = 2 he noticed that he can make 5 numbers: 11, 14, 41, 44 and 2 (he knows how to count them up, but he
doesn't know how to write five). However, he can't figure it out for n greater than 2. So, he asked you to help him.
The Input
Input will consist on an arbitrary number of sets. Each set will consist on an integer n such that 1 <= n <= 1000. You must read until you reach the end of file.
The Output
For each number read, you must output another number (on a line alone) stating how much numbers Gustavo can make such that the sum of their digits is equal to the given number.
Sample Input
2
3
Sample Output
5
13
题意:Gustavo数数时总是把1和4搞混,他觉得4仅仅是1的第二种写法。给出一个整数n,Gustavo想知道有多少个数的数字之和恰好为n。比如,当n=2时,有5个数:11、14、41、44、2。
故 F(n) = F(n - 1) + F(n - 2) + F(n - 3) + F(n - 4) = 2 * F(n - 1) + F(n - 2) + F(n - 3)。
边界条件: F(1) = 2, F(2) = 5。 F(3) = 13。
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; vector<string> v; string add(string a, string b)
{
string s;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int i = 0;
int m, k = 0;
while(a[i] && b[i])
{
m = a[i] - '0' + b[i] - '0' + k;
k = m / 10;
s += (m % 10 + '0');
i++;
}
if(i == a.size())
{
while(i != b.size())
{
m = k + b[i] - '0';
k = m / 10;
s += m % 10 + '0';
i++;
}
if(k) s += k + '0';
}
else if(i == b.size())
{
while(i != a.size())
{
m = k + a[i] - '0';
k = m / 10;
s += m % 10 + '0';
i++;
}
if(k) s += k + '0';
}
reverse(s.begin(), s.end());
return s;
} void solve()
{
v.push_back("0");
v.push_back("2");
v.push_back("5");
v.push_back("13");
string s;
for(int i = 4; ; i++)
{
s = add(v[i-1], v[i-1]);
s = add(v[i-2], s);
s = add(v[i-3], s);
v.push_back(s);
if(v[i].size() > 1001) break;
}
} int main()
{
solve();
int n;
int Size = v.size();
while(cin >> n)
{
cout << v[n] << endl;
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
UVA 10198 Counting的更多相关文章
- uva 1436 - Counting heaps(算)
题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,如今为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一仅仅 ...
- UVA 12075 - Counting Triangles(容斥原理计数)
题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...
- UVA - 10574 Counting Rectangles
Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...
- UVA 10574 - Counting Rectangles(枚举+计数)
10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...
- UVA 10574 - Counting Rectangles 计数
Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular ...
- UVA 12075 Counting Triangles
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVA 11174 Stand in a Line,UVA 1436 Counting heaps —— (组合数的好题)
这两个题的模型是有n个人,有若干的关系表示谁是谁的父亲,让他们进行排队,且父亲必须排在儿子前面(不一定相邻).求排列数. 我们假设s[i]是i这个节点,他们一家子的总个数(或者换句话说,等于他的子孙数 ...
- UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)
先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[ ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
随机推荐
- 锚点链接和hash属性
相信大家挺经常见过这样一个效果.有一个很长很长的页面,分成好几部分,目录中一点击,就能定位到页面某个位置. 例如:有这样一个目录,例如你点击一下“HTML”,就会直接跳转到“HTML”的页面位置 这就 ...
- 在MVC中如何愉快使用Ajax
前言: 这个故事要从我老大与客户谈需求开始说起.前几天,遇见一个逗比客户,不知道是听了哪个逗比程序员的临终遗言...让我们给他做一个手机端的Web应用出来,还说要使用MVC来做(不是App).马币,客 ...
- java.lang.NoSuchMethodError: com.google.common.collect.Maps.newConcurrentMap()Ljava/util/concurrent/ConcurrentMap;
在storm启动topo的时候,报错: java.lang.NoSuchMethodError: com.google.common.collect.Maps.newConcurrentMap()Lj ...
- shell获取文件行数
获取文件行数: echo `cat $file | wc -l` 获取文件中不重复的行数(去重后) echo `awk '{$1="";print $0;}' $file_tel ...
- Oracle物理结构与逻辑结构
有一张图能非常清晰的表示出Oracle物理结构与逻辑结构的区别:如下图: 对上图的解释:每个数据库都必须由一个或多个表空间组成.(一对多关系)每个表空间都必须由一个或多个数据文件(data fil ...
- js动态加载html,加载后的页面元素某些事件失效的解决方案
用 live 来绑定 例如: $("#items li .addToCartimg").live("click",function(){ $('.popDeta ...
- Ubuntu中安装编译并测试HTK语音识别库
1.在网上看到首先必须确保电脑上安装了g++和libx11 g++ --version //检测g++版本 sudo apt-get install libx11-dev:i386 2.然后可从HTK ...
- SQLServer分页
1.为什么要分页? 当显示数据的时候,我们不会一下子把所有的数据都显示出来,比如说表中有一万条数据,难道我们要把一万条数据都一次性的显示出来吗?!即便显示给用户了,用户也看不过来.因此,不论是从效率的 ...
- ie6里png图片不透明
ie6下img图片或背景图片为png时,图片变成了一片黑色: 图中的jquery-timepicker的两个黑方块和img就是由此原因引用的.解决方法:由Drew Diller提供,对img.back ...
- 3月23日html(四) 格式与布局
一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 二.position:absolute 1.外层没有position:absolute(或relat ...