第一题
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
#include <string.h>
using namespace std;
void show(char* a, int b = 0) {
static int cnt = 0;
cnt++;
if (b == 0) {
while (*a) {
cout << *a << " ";
a++;
}
cout << endl;
}else{
char* c = a;
for (int i = 0; i < cnt; i++) {
c = a;
while (*c) {
cout << *c << " ";
c++;
}
cout << endl;
}
}
}
int main()
{
char x[] = "12312321";
show(x);
show(x, 1);
show(x, 1);
show(x, 1);
}
第五题:
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
#include <string.h>
using namespace std;
template <typename T>
T max5(T a[5]) {
T x = a[0];
for (int i = 1; i < 5; i++) {
x = max(x, a[i]);
}
return x;
}
int main()
{
int a[5] = { 1,2,3,4,5 };
double b[5] = { 7,7,8,9,0 };
cout << max5(a) << " " << max5(b) << endl;
}
第六题
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
#include <string.h>
using namespace std;
template <typename T>
T maxn(T a[], int n) {
T x = a[0];
for (int i = 1; i < n; i++) {
x = max(x, a[i]);
}
return x;
}
int main()
{
int c[] = { 1,2,3,4,5,6 };
int* a = c;
double d[] = { 7,7,8,9 };
double* b = d;
cout << maxn(a, 6) << " " << maxn(b, 4) << endl;
}
第七题:
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
#include <string.h>
using namespace std;
template <typename T>
T maxn(T a[], int n) {
T x = a[0];
for (int i = 1; i < n; i++) {
x = max(x, a[i]);
}
return x;
}
template <> char* maxn(char* a[], int n) {
int ls = -1;
int an = 0;
for (int i = 0; i < n; i++) {
int x = 0;
char* b = a[i];
while (*b) {
b++;
x++;
}
if (x > ls) {
ls = x;
an = i;
}
}
return a[an];
}
int main()
{
int c[] = { 1,2,3,4,5,6 };
int* a = c;
double d[] = { 7,7,8,9 };
double* b = d;
cout << maxn(a, 6) << " " << maxn(b, 4) << endl;
char l[] = "123";
char g[] = "456";
char gd[] = "70";
char* ac[] = {l,g,gd};
cout <<maxn(ac, 3);
}