免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 吴秦
打印 上一主题 下一主题

[C] C语言经典题目及解题思路,持续更新中。。。 [复制链接]

论坛徽章:
0
31 [报告]
发表于 2009-08-05 13:06 |只看该作者
原帖由 aobai 于 2009-7-31 13:37 发表
非常好

第二题:
 caution:其实要判断这三个数是否由1~9组成且各个数组不相同,即这三个数的各位数是否覆盖了1~9,只要判断各位数字的积是否等于9!且各位数字的和是否等于45。

  能否解释下?
第七 ...

如果这三个数的各位数是否覆盖了1~9,则各位数字的积必定=1*2*3*4*5*6*7*8*9=9!,各位数字的和必定=1+2+3+4+5+6+7+8+9=45,反之如果没有覆盖1~9就不会有这结果。

顺便跟大家解释下,公司这段时间要做项目时间很紧所以就没有更新,以后一定会继续更新的不会太监的,望大家谅解!

论坛徽章:
0
32 [报告]
发表于 2009-09-07 14:10 |只看该作者

mark 一下

mark

论坛徽章:
0
33 [报告]
发表于 2009-09-08 08:47 |只看该作者
谢谢楼主, 收益匪浅.

论坛徽章:
0
34 [报告]
发表于 2009-09-16 11:16 |只看该作者
9、
【问题描述】用递归函数求解两个正整数的最大公约数的过程。
【思路】此题不需太多的思考就是运用欧几里得算法(Euclid’s algorithm)。
  摘自《The Art  of Computer Programming》volume 1:
  (Eu ...

As a careful reader,you can find that if m<n originally,the quotient in step1 will always be zero and the algorithm will always proceed to interchange m and n in this rather cumbersome fashion.
So we can add a new step :
Step0:[Ensure m>=n.]If m<n, exchange m and n.

Did you think why this algorithm is right when you read it ?(If you want to grasp computer programming or to be a mater you should always think this problem whenever or wherever you read a algorithm!)

Proof:
After step1,we have m=qn+r,for some integer q.
If r=0,then m is a multiple of n,and clearly in such a case n is the greatest common divisor of m and n.
If r!=0,note that any number that divides both m and n must divide m-qn=r,and any number that divides both n and r must divide qn+r=m;
So the set of common divisors of {m,n} is the same as the set of common divisors of {n,r}.
In particular ,the greatest common divisor of {m,n} is the same as the greatest common divisor of {n,r}.
Therefore  step3 does not change the answer to the original problem .

论坛徽章:
0
35 [报告]
发表于 2009-09-16 22:50 |只看该作者
mark

论坛徽章:
0
36 [报告]
发表于 2009-09-27 08:31 |只看该作者

回复 #1 吴秦 的帖子

第一题中如果输入的是0会是怎样一种情况?

论坛徽章:
0
37 [报告]
发表于 2009-10-12 12:56 |只看该作者
好的东东值得珍藏和力挺!!!

论坛徽章:
0
38 [报告]
发表于 2009-10-24 15:07 |只看该作者
加油啊,拭目以待。

第七题递归
int max(int a[],int n)
{
    if(0==n)
        return a[n];
    else   
        return (a[n]>max(a,n-1)?a[n]:max(a,n-1));   
}

改成这样才搞懂,外加还调试了几个小时。初学勿笑。

int solve(int a[],int n)
{
        if(0 == n)
                return a[n];
        else if((solve(a,n - 1) < a[n]))
                return a[n];
                else
                return solve(a, n-1);
}

[ 本帖最后由 清凉散人 于 2009-10-26 20:57 编辑 ]

论坛徽章:
0
39 [报告]
发表于 2009-10-24 22:56 |只看该作者


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void backtrack(int n);

  4. int x[8],sum=0;/*sum用于统计共有多少种方案*/
  5. /*函数功能:回朔求解第n天至第7天的解(即第n~7天分别安排和尚几)*/
  6. struct st
  7. {
  8.         int spare[8];
  9. /*存储和尚的空闲时间,spare=0表示星期i没有空闲,spare=1表示星期i空闲,其中spare[0]不用*/

  10.         int flag;
  11. /*用于标记和尚周内是否已经工作过,flag=0表示没挑过水,flag=1表示已经挑过水*/

  12. }monk[8]={
  13.                 {0, 1, 0, 1, 0, 0 ,0 },
  14.                 {1 ,0 ,0 ,0 ,0 ,1 ,0 },
  15.                 {0, 0 ,1 ,0 ,0 ,0 ,1 },
  16.                 {0, 0 ,0 ,0 ,1, 0, 0 },
  17.                 {1 ,0 ,0, 1 ,0 ,1 ,0 },
  18.                 {0 ,1 ,0 ,0 ,1 ,0 ,0 },
  19.                 {0 ,0, 1 ,0 ,0 ,1, 1 }
  20.                 };

  21. int main (int argc, char **argv)
  22. {
  23.         backtrack(0);
  24.         printf("共有%d种方案\n",sum);
  25. }

  26. void backtrack(int n)
  27. {/*函数功能:回朔求解第n天至第7天的解(即第n~7天分别安排和尚几)*/
  28.         int j;
  29.         if(n>6)
  30.         {
  31.                 sum++;
  32.                 printf("方案%d:\n",sum);
  33.                 for(j=0;j<=6;j++)
  34.                 {                        
  35.                         printf("星期%d和尚%d挑水\n",j+1,x[j]+1);
  36.                 }               
  37.                 printf("\n");
  38.         }
  39.         else
  40.         {
  41.                 for(j=0;j<=6;j++)
  42.                 {
  43.                         x[n]=j;
  44.                         if(monk[j].flag==0&&monk[j].spare[n]==1)
  45.                         {//判断和尚j是否已经挑过水及和尚星期n是否有空
  46.                                 monk[j].flag=1;        
  47.                                 backtrack(n+1);        
  48.                                 monk[j].flag=0;                                                
  49.                         }                                       
  50.                 }        
  51.                                        
  52.         }
  53. }

复制代码


和尚这样挑水,这样方便调试。。

[ 本帖最后由 清凉散人 于 2009-10-25 08:34 编辑 ]

论坛徽章:
0
40 [报告]
发表于 2009-11-11 11:11 |只看该作者

回复 #1 吴秦 的帖子

2、
【问题描述】Armstrong数具有如下特征:一个n位数等于其个位数的n次方之和。如:
153=13+53+33
1634=14+64+34+44
找出2、3、4、5位的所有Armstrong数。


  1. #include <stdio.h>
  2. #include <string.h>

  3. void find_armstrong_number()
  4. {
  5.         char number_str[16]={0};
  6.         int len;
  7.         int i,j,k,total,tmp;
  8.         for (i = 10; i < 100000; i++)
  9.         {
  10.                 sprintf (number_str,  "%d", i);
  11.                 len = strlen(number_str);
  12.                 total = 0;
  13.                 for (j=0;j<len;j++)
  14.                 {
  15.                         tmp = 1;
  16.                         for(k=0;k<len;k++) tmp *= (number_str[j]-'0');
  17.                         total += tmp;
  18.                 }
  19.                 if (total == i)
  20.                         printf("%d\n",i);
  21.         }
  22. }

  23. int main(int argc, char *argv[])
  24. {
  25.         printf("output:\n");
  26.         find_armstrong_number();
  27.         return 0;
  28. }

复制代码


打印结果如下:
output:
153
370
371
407
1634
8208
9474
54748
92727
93084
分别在vc和gcc上验证通过~

[ 本帖最后由 matthew_ye 于 2009-11-11 11:12 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP