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 code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">这道题目还是考察循环,通过循环遍历1234~9876,然后将每个数都用算法判断其是否符合条件#include <stdio.h>
- #include <stdlib.h>
- int f4171(int num)
- {
- int count=0;
- while(num)
- {
- count++;
- num/=10;
- }
- return count;
- }
-
- // 这个函数返回位数的转换
- int f4172(int num)
- {
- int sum=0;//要返回的数字
-
- // 计算num有多少位
- int n=f4171(num);
- int Times=1;
- // 计算位数对应的最小值,比如四位是1000
- for (int i=1;i<n;i++)
- {
- Times*=10;
- }
- // 翻转
- while(num)
- {
- int temp=num%10; // temp保存当前最后一位
- sum+=temp*Times; // 将最后一位成语当前的times,累加到和
- num/=10; // num去掉一位
- Times/=10; // times也去掉一位,准备下次的循环
- }
- return sum;
- }
- void main()
- {
-
- for (int i=1234;i<9876;i++) // 遍历
- {
- if (i==4*f4172(i)) // 如果经过转换后,再乘以4等于自身,说明符合条件
- {
- printf("%4d",i);
- break;
- }
- }
- system("pause");
- }</pre><br><br></pre>
|
|