博文

打印菱形(2008-03-19 23:14:00)

摘要:最早的版本:
#include <stdio.h> int main()
{
 int k1 = 5 , k2 = 1;
 int i,j;
    do
 { 
      for (i=1 ; i <= k1-1 ; i++)   printf (" ");
      for (j=1 ; j <= k2 ; j++)  printf("*");
   printf("\n");
      k1=k1-1;  //每行*号前的空格相差1个
   k2+=2;   // 每行的*号个数相差2个
 }
 while (k1 >1 );   //注释:下
 
 /* 原先之所以输出
    *
     *****
   ***
    * 
    这样,是因为将判断条件错误的写为k1<=0,于是第一次循环执行了k1=k1-1后,K值为2不满足k1<=0
    于是结束了循环  */     int k3=1, k4 =5;
 int i2 , j2;  do
 {
  for (i2=1; i2 <= k3+1 ; i2++ ) printf (" ");
  
  for (j2=1 ; j2 <= k4 ; j2++ ) printf ......

阅读全文(2460) | 评论:0

<算法 i~iv>-4.11(2008-03-09 17:13:00)

摘要://///////////////////////////////////////////////////////////
//  <算法 i~iv>
// 
//  Exercise : 4.11  , Page : 103
//
//  exercises description:
//   给定两个序列,给出算法来判断是否可以在序列中插入星号,
//  使得第一个序列可以生成第二个序列.生成规则由练习4.10来解释.
//
//         zhaoyg 2008.3.5
///////////////////////////////////////////////////////////// #include <stdio.h>
#include <stdlib.h>
#include <string.h> #define MAX 20 int main()
{
 int source[2][MAX];
 int (*p_source)[MAX]=source;  int target[MAX];
 int *p_target=target;  int lenth_between_sur_tar=0 , count_lenth_source=0 ,temp;
 int i , j , end ,count=0;  //printf("enter source string\n");  while (1)
 {
  lenth_between_sur_tar=0 , count_lenth_source=0 ;
  printf("enter source string\n");
  while((temp=getchar())!='\n')
 &......

阅读全文(1637) | 评论:0

<算法 i~iv>-3.43(2008-03-09 17:12:00)

摘要:////////////////////////////////////////////
//  <算法 i~iv>
// 
//  Exercise : 3.43  , Page : 75
//  exercise description:
//   实现一个程序,互换一个双向链表中两个给定的节点的位置.
//  
//  P.S: 对于相邻节点尚未考虑,还需几行代码即可
//  zhaoyg   2008.2.
////////////////////////////////////////////
#include <iostream> using namespace std; const int times = 10; struct node 
{
 int item;
 node *prev;
 node *next;  node (int value , node *p , node *n)
 {
  item = value;
  prev = p;
  next = n;
 }
}; void creat_node(node **head,int times);
void test_list(node *temp);
void transposition(node **head,int item_1,int item_2);
void free_list(node *head); int main()
{
 node *head = NULL;
 node *temp;
 int item_a,item_b;  cout <<"请输入需要交换节点的位置,用空格分隔\n当前链表中共有"<<times<<"个节点"&l......

阅读全文(1765) | 评论:0

<算法 i~iv>-3.41(2008-03-09 17:10:00)

摘要:////////////////////////////////////////////
//  <算法 i~iv>
// 
//  Exercise : 3.41  , Page : 75
//  exercise description:
//   实现程序3.11的一个使用头节点的版本
//  
//   P.S : 有时候不使用哑元节点确实麻烦
//  zhaoyg   2008.1.24
////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime> using namespace std; struct node
{
    int item;
    node *next;
   
    node(int x,node *t)
    {
        item=x;
        next=t;
    }
}; int main()
{
 ofstream outf;
 outf.open("result.txt");     node *head_a=NULL;
    node *a=NULL , *t;
    node *temp;  srand(......

阅读全文(2078) | 评论:0

<算法 i~iv>-3.35(2008-03-09 17:08:00)

摘要://////////////////////////////////////
//  <算法 i~iv>
// 
//  Exercise : 3.35  , Page : 75
//
//  exercises description:
//   编写一个函数,重新排列一个链表.使偶数位置的节点集中到链表后半部分,奇数位置节点集中
// 到链表前半部分,同时分别使奇数位节点和偶数位节点之间的顺序保持不变.
//
//  zhaoyg  2008.1.22
//////////////////////////////////////
#include <iostream>
using namespace std; struct list
{
    int content;
    list * next;
}; void fun (list * head); int main()
{
    list *head = NULL,*current,*temp;
    int value;
   
    cout << "enter value,q to quit\n";
   
    while (cin >> value)
    {
        if (head==NULL)
            current = head = new list;
   &nbs......

阅读全文(1830) | 评论:0