Chinaunix首页 | 论坛 | 博客
  • 博客访问: 326532
  • 博文数量: 90
  • 博客积分: 1585
  • 博客等级: 上尉
  • 技术积分: 964
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-26 18:33
文章分类

全部博文(90)

文章存档

2011年(10)

2010年(80)

我的朋友

分类: C/C++

2010-04-07 14:24:06

1: 输出和为一个给定整数的所有组合
例如n=5
5=1+4;5=2+3(相加的数不能重复)则输出1,4;2,3。
eg:
int main (void)
{
  unsigned long int i, j, k;
  printf("please input the number:");
  scanf("%ld", &i);

  if(i % 2 == 0)
      j = i / 2;
  else
      j = i / 2 + 1;

  for(k = 1; k < j; k ++)
  {
     printf("%ld = %ld + %ld \n", i, k, i - k);
  }

  return 0;
}

===================================================================================
转:
2.【问题描述】梯有N阶,上楼可以一步上一阶,也可以一步上二阶。
   编写一个程序,计算共有多少中不同的走法
【思路】看到此题目容易想到用递归的方法来做,因为递归是一种描述和解决结构自相似问题的基本算法,而N阶楼梯问题和N-1阶、N-2阶的结构完全相同。
     解决递归问题可以分为两个部分,第一部分是一些特殊(基础)情况,用直接法解,即始基;第二部分与原问题相似,可用类似的方法解决(即递归),但比原问题的规模要小。
     定义int count(int n)函数求解N阶楼梯的走法,基于上述思想,可知:

[*]N阶楼梯问题的始基是N==1、N==2两种情况;
[*]上楼可以一步上一阶,也可以一步上二阶,当上一阶时问题规模变为N-1,当上二阶时问题规模变为N-2,所以总的情况为count(n-1)+count(n-2)。

#include
int count(int n);
/*count how many ways to climb up N steps stairs.*/
int main (int argc, char *argv[])
{
    int n, ct;
    printf("please input n:\n");
    scanf("%d", &n);
    ct = count(n);
    printf("there are %d ways to climb up N steps stairs!\n", ct);
    return 0;        
}

int count(int n)
{
    if(1 == n)
        return 1;
    else if(2 == n)
        return 2;
    else return count( n - 1) + count( n - 2);
}
for example
please input n:
5
there are 8 ways to climb up N steps stairs!

=============================================================================
 
Armstrong数具有如下特征:一个n位数等于其个位数的n次方之和。如:
153=13+53+33
1634=14+64+34+44
找出2、3、4、5位的所有Armstrong数。
【思路】看到此题我第一反应是用枚举法,给定m(10<=m<=99999),首先判断m的位数n,然后判断它是否等于各位数的n次方之和。
[*]定义函数int judgeDigit(int m),用于判断给定参数m的位数;
[*]定义函数int judgeEqual(int m,int n),其中m为给定的数,n为m的位数,用于判断m是否等于各位数的n次方之和。
#include
#include
#include

int judgeDigit(int m);
/*This function return the digit of parameter m*/

void judgeEqual(int m,int n);
/*parameter m is a integer,parameter n is the digit of m,this function is used to judge m whether is a Armstrong integer and output it*/

int main (int argc, char **argv)
{
    int i, len;
    printf("All 2 digit to 5 digit Armstrong integers are following:\n");
    for(i = 10; i <= 99999; i ++)
    {
        len = judgeDigit(i);
        judgeEqual(i, len);                
    }
    printf("\n"); 
    return 0;
}
 
 
/*This function return the digit of parameter m*/
int judgeDigit(int m)
{    
    int len=0;
    do
    {
        ++ len;
        m = m / 10;
    }while(m); 
    return len;
}
 
/*parameter m is a integer,parameter n is the digit of m,
  this function is used to judge m whether is a Armstrong integer and output it*/ 
void judgeEqual(int m, int n)
{
    int j, temp = m, sum = 0;
    for(j = 1; j <= n; j ++)
    {
        sum += (int)(pow(temp % 10, n));
        temp = temp / 10;
    }
 
    /*if m is equal to sum, that is to say m is a Armstrong integer*/
    if(m == sum) 
        printf("%d\t", m);
}
===============================================================================
给出一个单链表,不知道节点N的值,只遍历一次求中间节点
设立两个指针
比如*p,*q
p每次移动两个位置,即p=p-> next-> next;q每次移动一个位置,即q=q-> next;
当p到达最后一个结点时,q就是中

==============================================================================
用 1, 2, 5 任意组合,使他的和为100, 如,100个1的组合,5个1和19个5的组合等。

写个函数打印所以组合
#include

void fun(int one , int two, int five)
{
     int i;
     for(i = 0; i < one; i ++) printf("1 ");
     for(i = 0; i < two; i ++) printf("2 ");
     for(i = 0; i < five; i ++) printf("5 ");
     printf("\n");
}

int main(void)
{
    int i, j = 0, k = 0;
    for(i = 100; i >= 0; i --)
    {
        if(j % 2  == 0) fun(i, j / 2, 0);
        if(j % 5  == 0) fun(i, 0, j / 5);
        if(j % 10 == 0)
        {
             int h = 0;
             for(k = h / 2; k > 0; k --)
             {
                 if(h % 5 == 0) fun(i, k, h / 5);
                 h += 2;
             }
        }
        j ++;
    }
    return 0;

}
 
阅读(983) | 评论(0) | 转发(0) |
0

上一篇:dpkg总结

下一篇:数组和指针的区别

给主人留下些什么吧!~~