code-水仙花数
2009-08-11 11:17
169 查看
//打印3位的水仙花数
public class Narcissus {
public static void Main(string[] cmd){
//System.Console.WriteLine(Pow(10,0));
//System.Console.WriteLine(Pow(10,1));
DoIt(3);
}
static void DoIt(int intDigits){
int low = (int)Pow(10,intDigits-1);
int high = (int)Pow(10,intDigits);
int ix = low;
while (ix < high){
if(IsNarc(ix)){
System.Console.Write(ix + "\t");
}
ix++;
//System.Console.Write(ix +"is not "+ "\t");
}
}
//3 digits implementation
static bool IsNarc(int intNum){
int units = 0;
int tens = 0;
int hundreds = 0;
units = (intNum % (int)Pow(10,1)) / (int)Pow(10,0);
tens = (intNum % (int)Pow(10,2)) / (int)Pow(10,1);
hundreds = (intNum % (int)Pow(10,3)) / (int)Pow(10,2);
return intNum==(Pow(units,3)+Pow(tens,3)+Pow(hundreds,3));
}
//integer implementation only
static long Pow(int intBase, int intExponent){
long product = 1;
int ix = 0;
////treat 0^0 = 1, omit the handling of that condition
//if (intBase == 0){
// throw new Exception("the base can not be 0");
//}
while(ix < intExponent){
product *= intBase;
ix++;
}
return product;
}
}
//理论上,最大的水仙花数不超过34位。
//思考:如何把全部的水仙花数打印完。
//要解决的问题
//1. 数太大(考虑自己构造数据类型)
//2. IsNarc方法的通用性
public class Narcissus {
public static void Main(string[] cmd){
//System.Console.WriteLine(Pow(10,0));
//System.Console.WriteLine(Pow(10,1));
DoIt(3);
}
static void DoIt(int intDigits){
int low = (int)Pow(10,intDigits-1);
int high = (int)Pow(10,intDigits);
int ix = low;
while (ix < high){
if(IsNarc(ix)){
System.Console.Write(ix + "\t");
}
ix++;
//System.Console.Write(ix +"is not "+ "\t");
}
}
//3 digits implementation
static bool IsNarc(int intNum){
int units = 0;
int tens = 0;
int hundreds = 0;
units = (intNum % (int)Pow(10,1)) / (int)Pow(10,0);
tens = (intNum % (int)Pow(10,2)) / (int)Pow(10,1);
hundreds = (intNum % (int)Pow(10,3)) / (int)Pow(10,2);
return intNum==(Pow(units,3)+Pow(tens,3)+Pow(hundreds,3));
}
//integer implementation only
static long Pow(int intBase, int intExponent){
long product = 1;
int ix = 0;
////treat 0^0 = 1, omit the handling of that condition
//if (intBase == 0){
// throw new Exception("the base can not be 0");
//}
while(ix < intExponent){
product *= intBase;
ix++;
}
return product;
}
}
//理论上,最大的水仙花数不超过34位。
//思考:如何把全部的水仙花数打印完。
//要解决的问题
//1. 数太大(考虑自己构造数据类型)
//2. IsNarc方法的通用性
相关文章推荐
- code-还是水仙花数-这次很长的水仙花
- Use Multi-Language In App_Code
- 添加第三方类库造成的linker command failed with exit code 1 (use -v to see invocation)的错误调试
- CodeForcesGym 100641D Generalized Roman Numerals
- codeforces B. Memory and Trident
- 求100到1000的水仙花数
- Code::Block中GCC的支持中文字符
- 解决Codeforces Round 的一些问题
- [LeedCode OJ]#154 Find Minimum in Rotated Sorted Array II
- Codevs 1331 西行寺幽幽子(高精度)
- 二维码图片生成器QRCode.js
- ORA-04031: unable to allocate 4096 bytes of shared memory ("shared pool","BEGIN :EXEC_STR := SYS.DBMS...","PL/SQL MPCODE","BAMIM
- CODE 132: Candy
- [LeedCode OJ]#75 Sort Colors
- Codevs
- Lintcode 二叉树的层次遍历
- Writing clean code
- Entity Framework Code First数据库自动更新
- codecombat之边远地区的森林23-30关及地牢40\41关代码分享
- Codeforces Round #369 (Div. 2)