实现的几个小程序:
1.要写一个函数,传三个参数,double,int ,bool 三个参数。
作用就是:传double进来 用int来取小数位数 ,bool用来确认是否要四舍五入。
返回的为string 值。
例如:
如果传一个double的 如12345.6789
然后传一个int 进来 如 3
然后要使这个double保留3位小数 12345.679
Bool的是控制是否四舍五入。
返回的为string 的值。
方法1:
- #include<stdio.h>
- #include<iostream>
- using namespace std;
- string doubleConvert(double dval,int nlen ,bool bval)
- {
- char buf[100];
- sprintf(buf,"%lf",dval);
- int buf_len = strlen(buf);
- int point_pos = char_traits<char>::find(buf, buf_len,'.')-buf;
- int total_len = point_pos+nlen;
- if ( buf_len > total_len && bval && buf[total_len+1]>='5') buf[total_len]+=1;
- buf[total_len+1] = '/0';
- return buf;
- }
- int main()
- {
- double dval = 12345.6789;
- string str = doubleConvert(dval,3,true);
- cout << str.c_str();
- return 0;
- }
方法2:
- #include <math.h>
- #include <iostream>
- #include <string>
- using namespace std;
- string doubleConvert(double dval,int nlen ,bool bval)
- {
- double d = bval ? 0.5 : 0;
- double n = pow(10.0, nlen);
- char buf[128] = {0};
- char format [128] = {0};
- sprintf(format, "%%.%dlf", nlen);
- sprintf(buf,format,(int)(dval*n+d)/(double)n);
- return buf;
- }
- int main()
- {
- double dval = 12345.6789;
- string str = doubleConvert(dval,3,true);
- cout << str <<endl;
- return 0;
- }
2. 把一个整数的各个位相加,得出另外一个整数,然后再将此整数的各个为相加,直到得出的整数小于10 为止(用递归的方法 实现)。
如:20081029 --〉22 --〉4
- #include <iostream>
- using namespace std;
- int Get(int n)
- {
- int i = 0;
- while (n) i+=n%10,n/=10;
- return i < 10 ? i : Get(i);
- }
- int main()
- {
- cout << Get(20081029) << endl;
- return 0;
- }
3. 把任意一个整数转化为顺序相反的整数。 如:632 --〉236; 1024 --〉4201
- #include <iostream>
- using namespace std;
- int Rev(int n)
- {
- int i = 0;
- while (n) i=i*10+n%10,n/=10;
- return i;
- }
- int main()
- {
- cout << Rev(20081029) << endl;
- return 0;
- }
4. 将字符串倒序
- #include <iostream>
- using namespace std;
- void resort( char* pStr ) //字符串倒序
- {
- int nLen = strlen( pStr );
- int nLoopCount = nLen / 2;
- for ( int i = 0; i < nLoopCount; i ++ )
- {
- char c = pStr[i];
- pStr[i] = pStr[nLen-i-1];
- pStr[nLen-i-1] = c;
- }
- }
- int convert(int temp)
- {
- char str[10];
- itoa(temp,str,10);//把整数转成字符串
- resort(str);
- return atoi(str);//返回时,把字符串转成整型
- }
- int main()
- {
- int x=1023;
- cout<<convert(x)<<endl;
- return 0;
- }
5. 给定数组 A(N),找出其中相距最近的两个数。距离 R=|A[i+1]-A[i]|
即:在一个数组中找出相邻两个元素之间相差最小的,R=|A[i+1]-A[i]|,不是所有元素中,而是相邻元素中。
方法1:
template <typename T>
int near(T* a, int n)
{
int mi = 0;
T dif = a[1] - a[0];
for(int i = 1; i< n - 1; ++i){
T absv = (a[i+1]<a[i]?a[i]-a[i+1]:a[i+1]-a[i]);
if(absv < dif){
dif = absv;
mi = i;
}
}
return mi;
}
#include <iostream>
using namespace std;
int main()
{
int a[5] = {3,9,5,2,7};
int i = near(a, 5);
cout<<a[i]<<","<<a[i+1]<<endl;
return 0;
}
方法2:
#include <iostream>
#include <math.h>
using namespace std;
#define N 6
int find(int* a)
{
int min = 1000, ind = -1;
if(N <= 2){
return 0;
}
else{
for(int i = 0; i < N - 1; ++i){
if(abs(a[i]-a[i+1]) < min){
min = abs(a[i] - a[i+1]);
ind = i;
}
}
}
return ind;
}
int main()
{
int a[N] = {3,9,5,2,7,6};
int index = find(a);
cout<<"The element is:"<<a[index]<<","<<a[index+1]<<endl;
return 0;
}