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年 ...
随机推荐
- Android SDK Manager 无法更新SDK
Android SDK Manager 被墙后无法更新SDK 下载sdk时抛出错误:Failed to fetch URL http://dl-ssl.google.com/ 參考例如以下博客: ht ...
- 常用分组函数count-avg-sum-max-min
分组函数也称多行函数,用于对一组数据进行运算,针对一组数据(取自于多行记录的相同字段)只返回一个结果,例如计算公司全体员工的工资总和.最高工资.最低工资.各部门的员工平均工资(按部门分组)等.由于分组 ...
- 动软代码生成V2.74模版简介
最近发现很多人用动软代码生成,确实方便,有些经验记录下,以后查看回顾. ..\Maticsoft\Codematic2\Template\TemplateFile 为模板文件夹,直接在目录下新建文件夹 ...
- eclipse与github建立版本关联、git使用说明
1.在eclipse下建一个project :aa 2.打开git bush,进入aa目录下:cd /d/MyWorkspace/aa 3.git init 4.git add -A 5.git co ...
- 腾讯云(centos)上安装apache
安装 yun install httpd 随系统自启动 chkconfig httpd on 修改/etc/httpd/conf/httpd.conf文件 #修改WWW目录为自己的目录 Dcoumen ...
- ASP.Net动态创建GridView
1.创建Field BoundField requestitem = new BoundField();//注意Field可以是ButtonField,TemplateField,...等等GridV ...
- XML中 添加或修改时 xmlns="" 怎么删除
//创建节点时 记得加上 ---> xmldoc.DocumentElement.NamespaceURI XmlElement url = xmldoc.CreateElement(&quo ...
- c - 输出 101 至 200之间的素数.
#include <stdio.h> #include <math.h> //判断 101-200 之间有多少个素数,并输出所有素数. int main(void) { , e ...
- winform托盘时,要运行一个实例,解决办法
需求:winform应用程序,当隐藏到托盘时,再次运行exe程序,让其只运行一个实例,并且把窗口从托盘中显示出来 应用程序名可以通过下面代码,获取到: Process current = Proces ...
- 第1章 网络编程基础(2)——Socket编程原理
Socket编程原理 Socket是网络通信端点的一种抽象,它提供了一种发送和接收数据的机制. 流socket(SOCK_STREAM):双向.有序.无重复.并且无记录边界 数据报Socket(SOC ...