C语言程序100例之C#版-029
生活随笔
收集整理的這篇文章主要介紹了
C语言程序100例之C#版-029
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C語言程序100例之C#版-029
C程序源代碼:
【程序29】
題目:給一個不多于5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。
1. 程序分析:學會分解出每一位數,如下解釋:(這里是一種簡單的算法,師專數002班趙鑫提供)
2.程序源代碼:
########################
C#語言程序:
using System; class C329 {static void Main(){long a,b,c,d,e,x;x=Convert.ToInt64(Console.ReadLine());a=x/10000;b=x%10000/1000;c=x%1000/100;d=x%100/10;e=x%10;if (a!=0) Console.Write("there are 5, {0} {1} {2} {3} {4}\n",e,d,c,b,a);else if (b!=0) Console.Write("there are 4, {0} {1} {2} {3}\n",(int)e,(int)d,(int)c,(int)b);else if (c!=0) Console.Write(" there are 3,{0} {1} {2}\n",(int)e,(int)d,(int)c);else if (d!=0) Console.Write("there are 2, {0} {1}\n",(int)e,(int)d);else if (e!=0) Console.Write(" there are 1,{0}\n",(int)e);} }
擴展1:平時寫應用是,如果純粹為了實現目標進行分解和倒序,可以考慮用字符串來處理,方便,快捷。但處理過程不是以數值計算的方式來實現的。
using System; class C329_1 {static void Main(){long x;x=Convert.ToInt64(Console.ReadLine());String y = x.ToString();Console.Write("there are" + x.ToString().Length+",");for (int i = 1; i <= y.Length; i++){Console.Write(y.Substring(y.Length - i, 1));Console.Write(" ");}} }
擴展2:保持數值計算,(原題目要求為不多余5位的正整數)并且擴展輸入數據長度(但不能超過所使用的數值類型的最大長度);
using System; using System.Collections.Generic;class C329_2 {static void Main(){long x;x=Convert.ToInt64(Console.ReadLine());String y = x.ToString();int z = x.ToString().Length;Console.Write("there are " + z+",");int a = 10;int b = 0;int c = (int)x;List<int> d = new List<int>();for (int i = y.Length-1; i >= 0; i--){b = (int)Math.Pow(a, i);d.Add(c / b);c = (int)x % b;}d.Reverse();for (int i = 0; i < d.Count;i++ ){Console.Write(d[i]);Console.Write(" ");}} }總結
以上是生活随笔為你收集整理的C语言程序100例之C#版-029的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言-字符型数据与ASCII码表
- 下一篇: C语言程序100例之C#版-019