TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131203_3_6116439" class="cpp" name="code">/************************************************************************/
- /*
- 18. 给出一个不多于4位的正整数,要求:① 求出它是几位数
- ② 分别打印出每一位数字 ③ 按逆序打印出各位数字。
- */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- int GetWS18(int num) // 打印位数函数
- {
- int count=0;
- while(num) // 当num还大于0时,num除与10,这样当num是0时,循环次数就是它的位数
- {
- count++; // 只要在循环体里累加就可以了
- num/=10;
- }
- return count;
- }
- void RprintNum(int num)
- {
- while(num) // 反向打印,当num大于0时,打印最后一位,打印完最后一位后,除10去除最后一位
- {
- printf("%d",num%10);
- num=num/10;
- }
- }
- void printNum(int num) // 正向打印
- { int temp=1;
- while(num/temp>=10) // 首先求出最高位对应的最小整数
- {
- temp*=10;
- }
- while(temp>=1) // 然后一位一位的打印
- {
- printf("%d ",num/temp%10);
- temp/=10; // 打印一位把最高位对应额整数除与10
- }
- }
- void main()
- {
- int num;
- printf("please input num");
- scanf_s("%d",&num); // 获取输入
- printf("\n");
- printf("%d",GetWS18(num )); // 打印位数
- printf("\n");
-
- printNum(num); // 打印每位数
- printf("\n");
- RprintNum(num); // 反向打印
-
- system("pause");
- }</pre><br><br><br></pre>
|
|