七、
由于一個函數不能既作為重載函數又作為預設參數的函數,是以我這裡設定的兩個函數名不同,分别為max和max2
詳細C++代碼如下:
#include<iostream>
using namespace std;
int max(int a, int b, int c)
{
return a > b ? a : (b > c ? b : c);
}
int max2(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int max(int a, int b, int c = 10);
int max2(int a, int b = 20);
cout << "please input a, b, c:";
int a, b, c;
cin >> a >> b >> c;
int tmp = max(a, b);
cout << tmp<< endl;
tmp = max2(c);
cout << tmp << endl;
return 0;
}
我的樣例運作結果如下:
八、
我這裡既用了普通引用,也用了形參引用
詳細C++代碼如下:
#include<iostream>
using namespace std;
void swap(int &a, int &b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
int main()
{
cout << "Please input a and b:";
int a, b;
cin >> a >> b;
cout << "------------------------" << endl;
int &c = a;
int &d = b;
int tmp;
if(c > d)
{
tmp = c;
c = d;
d = tmp;
}
cout << c << " " << d << endl;
cout << "------------------------" << endl;
cout << "Please input a and b:";
cin >> a >> b;
if(a > b)
swap(a, b);
cout << "------------------------" << endl;
cout << a << " " << b << endl;
return 0;
}
我的樣例運作結果如下:
九、
詳細C++代碼如下:
#include<iostream>
using namespace std;
void sort(int &a, int &b, int &c)
{
int tmp;
if(a > b)
{
tmp = a;
a = b;
b = tmp;
}
if(a > c)
{
tmp = a;
a = c;
c = tmp;
}
if(b > c)
{
tmp = b;
b = c;
c = tmp;
}
}
int main()
{
int a, b, c;
cout << "please input a, b and c:";
cin >> a >> b >> c;
sort(a, b, c);
cout << a << " "<< b << " " << c<< endl;
return 0;
}
我的樣例結果如下:
十、
詳細C++代碼如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "China";
string str2 = "America";
str1 = str1 + str2;
cout << str1 << endl;
return 0;
}
十一、
詳細C++代碼如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1;
cin >> str1;
int i;
int len = str1.length();
for(i = len - 1; i >= 0; i--)
cout << str1[i];
return 0;
}
十二、
詳細C++代碼如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str[5];
int i;
for(i = 0; i < 5; i++)
{
cout << "Please input string" << i + 1 << ":";
cin >> str[i];
cout << "----------------------"<< endl;
}
cout << "Input Stop!" << endl;
cout << "Ouput five strings:"<< endl;
for(i = 0; i < 5; i++)
{
cout << "string" << i + 1 << ":" << str[i] << endl;
}
int k;
for(i = 0; i < 4; i++)
{
k = i;
for(int j = i + 1; j < 5; j++)
{
if(str[k] > str[j])
{
k = j;
}
}
string tmp;
tmp = str[i];
str[i] = str[k];
str[k] = tmp;
}
cout << "Sorted Strings:" << endl;
for(i = 0; i < 5; i++)
cout << str[i] << " ";
return 0;
}
我的樣例結果如下:
十三、
解法一、
#include<iostream>
using namespace std;
void sort(int a[], int n)
{
int i, j, k, tmp;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(a[k] > a[j])
k = j;
}
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
void sort(float a[], int n)
{
int i, j, k;
float tmp; //Important!!!
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(a[k] > a[j])
k = j;
}
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
void sort(double a[], int n)
{
int i, j, k;
double tmp; //Important!!!
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(a[k] > a[j])
k = j;
}
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
int main()
{
int n;
cout << "please input n:";
cin >> n;
int a[n];
float b[n];
double c[n];
cout << "------------------" << endl;
cout << "please input "<< n <<" int:";
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "------------------" << endl;
sort(a, n);
cout << "output sorted int:";
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << "\n------------------" << endl;
cout << "please input "<< n <<" float:";
for(int i = 0; i < n; i++)
{
cin >> b[i];
}
cout << "------------------" << endl;
sort(b, n);
cout << "output sorted float:";
for(int i = 0; i < n; i++)
cout << b[i] << " ";
cout << "\n------------------" << endl;
cout << "please input "<< n <<" double:";
for(int i = 0; i < n; i++)
{
cin >> c[i];
}
cout << "------------------" << endl;
sort(c, n);
cout << "output sorted double:";
for(int i = 0; i < n; i++)
cout << c[i] << " ";
return 0;
}
方法二、
用指針來作為形參是因為隻有不同類型的整數指針才能區分不同類型的整數,比方說int類型的資料隻能對應int *的,float的隻能對應float *的
詳細C++代碼如下:
#include<iostream>
using namespace std;
void sort(int *a, int n)
{
int i, j, k, tmp;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(a[k] > a[j])
k = j;
}
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
void sort(float *a, int n)
{
int i, j, k;
float tmp;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(a[k] > a[j])
k = j;
}
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
void sort(double *a, int n)
{
int i, j, k;
double tmp;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(a[k] > a[j])
k = j;
}
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
int main()
{
int n;
cout << "please input n:";
cin >> n;
int a[n];
float b[n];
double c[n];
cout << "------------------" << endl;
cout << "please input "<< n <<" int:";
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "------------------" << endl;
sort(a, n);
cout << "output sorted int:";
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << "\n------------------" << endl;
cout << "please input "<< n <<" float:";
for(int i = 0; i < n; i++)
{
cin >> b[i];
}
cout << "------------------" << endl;
sort(b, n);
cout << "output sorted float:";
for(int i = 0; i < n; i++)
cout << b[i] << " ";
cout << "\n------------------" << endl;
cout << "please input "<< n <<" double:";
for(int i = 0; i < n; i++)
{
cin >> c[i];
}
cout << "------------------" << endl;
sort(c, n);
cout << "output sorted double:";
for(int i = 0; i < n; i++)
cout << c[i] << " ";
return 0;
}
十四、
詳細C++代碼如下:
#include<iostream>
using namespace std;
template<typename T>
T sort(T* a, T n)
{
int i, j, k;
T tmp;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(a[i] > a[j])
k = j;
}
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
int main()
{
int n;
cout << "please input n:";
cin >> n;
int a[n];
float b[n];
double c[n];
cout << "------------------" << endl;
cout << "please input "<< n <<" int:";
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "------------------" << endl;
sort(a, n);
cout << "output sorted int:";
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << "\n------------------" << endl;
cout << "please input "<< n <<" float:";
for(int i = 0; i < n; i++)
{
cin >> b[i];
}
cout << "------------------" << endl;
sort(b, (float)n);
cout << "output sorted float:";
for(int i = 0; i < n; i++)
cout << b[i] << " ";
cout << "\n------------------" << endl;
cout << "please input "<< n <<" double:";
for(int i = 0; i < n; i++)
{
cin >> c[i];
}
cout << "------------------" << endl;
sort(c, (double)n);
cout << "output sorted double:";
for(int i = 0; i < n; i++)
cout << c[i] << " ";
return 0;
}
對比兩個樣例測試結果
如果對3個資料進行排序是沒問題的,但是對6個卻無法正确排序
大家發現問題在哪了嗎,很簡單,在選擇排序裡
正确C++代碼如下:
#include<iostream>
using namespace std;
template<typename T>
T sort(T* a, T n)
{
int i, j, k;
T tmp;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(a[k] > a[j])
k = j;
}
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
int main()
{
int n;
cout << "please input n:";
cin >> n;
int a[n];
float b[n];
double c[n];
cout << "------------------" << endl;
cout << "please input "<< n <<" int:";
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "------------------" << endl;
sort(a, n);
cout << "output sorted int:";
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << "\n------------------" << endl;
cout << "please input "<< n <<" float:";
for(int i = 0; i < n; i++)
{
cin >> b[i];
}
cout << "------------------" << endl;
sort(b, (float)n);
cout << "output sorted float:";
for(int i = 0; i < n; i++)
cout << b[i] << " ";
cout << "\n------------------" << endl;
cout << "please input "<< n <<" double:";
for(int i = 0; i < n; i++)
{
cin >> c[i];
}
cout << "------------------" << endl;
sort(c, (double)n);
cout << "output sorted double:";
for(int i = 0; i < n; i++)
cout << c[i] << " ";
return 0;
}
我的樣例運作結果如下: