一些算法

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 void shellsort(int v[],int n)
 5 {
 6     int gap,i,j,temp;
 7     for (gap = n/2 ; gap >0 ; gap /= 2)
 8     {
 9         for (i = gap; i < n ; ++i)
10         {
11             for (j = i - gap ; j >=0 && v[j] > v[j+gap] ; j -= gap)
12             {
13                 temp = v[j];
14                 v[j] = v[j + gap];
15                 v[j+gap] = temp;
16             }
17         }
18     }
19 }
20 //倒置字符串s中各个字符的位置
21 void reverse(char s[])
22 {
23     int ch;
24     for (int i = 0,j = strlen(s) - 1;i < j ; i++,j-- )
25     {
26         ch = s[i];
27         s[i] = s[j];
28         s[j] = ch;
29     }
30 }
31 //将整数n转换为字符串并保存在s中
32 void itoa(int n ,char s[])
33 {
34     int i ,sign;
35     if ((sign = n) < 0 )
36     {
37         n = -n;
38     }
39     i = 0;
40     do 
41     {
42         s[i++] = n%10 +'0';
43     } while (( n = n/10) > 0 );
44     if (sign < 0 )
45     {
46         s[i++] = '-';
47     }
48     s[i] = '\0';
49     reverse(s);
50 }
51 
52 //将n格式化为以b为底的数,保存在s中
53 void itob(int n,char s[] ,int b)
54 {
55     int i ,sign;
56     if ((sign = n) < 0 )
57     {
58         n = -n;
59     }
60     i = 0;
61     do 
62     {
63         s[i++] = n%b + '0';
64     } while ((n = n / b) >0);
65 
66     if (sign < 0)
67     {
68         s[i++] = '-';
69     }
70 
71     s[i] = '\0';
72     reverse(s);
73 }
74 
75 //删除字符串尾部的空格、制表符、换行符
76 void trim(char s[])
77 {
78     for (int i = strlen(s) - 1 ; i >0 ; i --)
79     {
80         if (s[i] != ' ' && s[i] != '\t' && s[i] != '\n')
81         {
82             break;
83         }
84     }
85     s[i+1] = '\0';
86 }
87 
88 void main()
89 {
90     char s[10];
91     itob(8,s,17);
92     cout<<s<<endl;
93 }

 

 

 

posted @ 2013-09-16 15:31  Big.Eagle  阅读(174)  评论(0编辑  收藏  举报