Connect the Cable Wires
2015-03-13 23:44
507 查看
Asif is a student of East West University and he is currently working for the EWUISP to meet his relatively high tuition fees. One day, as a part of his job, he was instructed to connect
cable wires to N houses. All the houses lie in a straight line. He wants to use only the minimum number of cable wires required to complete his task such that all the houses receive the cable service. A house can either get the connection
from the main transmission center or it can get it from a house to its immediate left or right provided the latter house is already getting the service.
You are to write a program that determines the number of different combinations of the cable wires that is possible so that every house receives the service.
Example: If there are two houses then 3 combinations are possible as shown in the figure.
Figure: circles represent the transmission center and the small rectangles represent the houses.
Input
Each line of input contains a positive integer N (N<=2000). The meaning of N is described in the above paragraph. A value of 0 for N indicates
the end of input which should not be processed.
Output
For each line of input you have to output, on a single line, the number of possible arrangements. You can safely assume that this number will have less than 1000 digits.
Sample Input
Sample Output
Problem Setter: Sohel Hafiz
注意数据的范围,此题要求不超过1000位,因此只能用字符串运算,也可用java大数运算。其中字符串运算写的有些繁琐。
cable wires to N houses. All the houses lie in a straight line. He wants to use only the minimum number of cable wires required to complete his task such that all the houses receive the cable service. A house can either get the connection
from the main transmission center or it can get it from a house to its immediate left or right provided the latter house is already getting the service.
You are to write a program that determines the number of different combinations of the cable wires that is possible so that every house receives the service.
Example: If there are two houses then 3 combinations are possible as shown in the figure.
Figure: circles represent the transmission center and the small rectangles represent the houses.
Input
Each line of input contains a positive integer N (N<=2000). The meaning of N is described in the above paragraph. A value of 0 for N indicates
the end of input which should not be processed.
Output
For each line of input you have to output, on a single line, the number of possible arrangements. You can safely assume that this number will have less than 1000 digits.
Sample Input
1 2 3 0 |
1 3 8 |
注意数据的范围,此题要求不超过1000位,因此只能用字符串运算,也可用java大数运算。其中字符串运算写的有些繁琐。
#include<iostream> #include<string> #include<vector> using namespace std; #define maxn 2001 vector<char>ve[maxn]; void pow(int a){ int flag=0,num,no,me; for(int i=0;i<ve[a].size();i++){ if(!flag){ num=(ve[a][i]-'0')*3; }else{ num=(ve[a][i]-'0')*3+flag; } flag=num/10; char ch='0'+num%10; ve[a+1].push_back(ch); } if(flag){ char ch='0'+flag; ve[a+1].push_back(ch); } vector<char>::iterator is=ve[a-1].begin(); vector<char>::iterator it=ve[a+1].begin(); flag=0; while(it!=ve[a+1].end()&&is!=ve[a-1].end()){ no=*it-'0'; me=*is-'0'; //cout<<no<<" "<<me<<endl; if(flag){ no=no+flag; if(no<me){ flag=-1; no=no+10-me; *it=no+'0'; } else{ no=no-me; *it=no+'0'; flag=0; } }else{ if(no<me){ flag=-1; no=no+10-me; *it=no+'0'; } else{ no=no-me; *it=no+'0'; flag=0; } } it++; is++; } if(it!=ve[a+1].end()){ if(flag){ int no=*it-'0'; no+=flag; *it=no+'0'; } } } int main(){ int n; ve[0].push_back(0); ve[1].push_back('1'); ve[2].push_back('3'); ve[3].push_back('8'); for(int i=4;i<=2000;i++){ pow(i-1); } while(cin>>n){ if(n==0) break; char ch='0'; int no=0; for(int i=ve .size()-1;i>=0;i--){ if(ch==ve [i]&&!no) continue; else{ no=1; cout<<ve [i]; } } cout<<endl; } return 0; }
相关文章推荐
- UVA 10862 - Connect the Cable Wires(数论 递推 高精度)
- UVA10862 - Connect the Cable Wires(递推 + java的大数)
- UVA10862 - Connect the Cable Wires(递推 + java的大数)
- Connect the Cable Wires
- UVA - 10862 Connect the Cable Wires
- UVA 10862 Connect the Cable Wires
- 10862 - Connect the Cable Wires
- uva 10862 - Connect the Cable Wires(规律)
- UVA 10862 Connect the Cable Wires
- UVa:10862 Connect the Cable Wires(斐波那契数列大数)
- kettle入门(二) 之 kettle连接oracle报的坑爹错误 Error occured while trying to connect to the database 的几种情况
- Host '主机名' is not allowed to connect to the mysql server
- Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
- Cannot connect to WMI provider.You do not have permission or the server is unreachable.Note that you can only manager SQL Server 2005 and later version with SQL Server Configuration Manager.Invalid namespace [0x8004100e]
- 【UVA 10397 Connect the Campus】& 最小生成树 & Kruskal
- ORA-12154: TNS:could not resolve the connect identifier specified
- Oracle ORA-12154: TNS:could not resolve the connect identifier specified(不积跬步,无以至千里)
- 解决虚拟机提示VMware Workstation cannot connect to the virtual machine的问题
- Failed to connect to the configuration database(Sharepoint 2013配置向导失败)
- Unable to start T-SQL Debugging. Could not connect to the computer ‘.’