2019网安复试题

1. 求1~20000之间的同构数。

#include <iostream>
//同构数
#include <cmath>
#include <iomanip>

using namespace std;
bool fun(int );

int main()
{
    for(int i = 1; i < 20000; i ++)
        if(fun(i))
            cout << setw(8) << left << i << setw(16) << left << i*i << endl;
    return 0;
}

bool fun(int num)
{
    int square = num*num;
    int i = 1;
    int n = num;
    while(n/=10) //统计num个数
        i++;
    for(int j = 0; j < i; j++)
    {
        int a = num%10;
        int b = square%10;
        if(a!=b)
            return false;
        num /=  10;
        square /=  10;
    }
    return true;
}

2. 递归求数组中最小值

#include <iostream>

using namespace std;
template<class T>
int find_Min(const T [],int);

int main()
{
    int a[] = {4,2,3,67,7,5,3,45,1,23,56,0};

    cout << find_Min(a,3);
    return 0;
}

template<class T>   //先处理后递归,更易于理解
int find_Min(cosnt T a[],int len)
{
    static int small = a[0];
    if(len == 0)
        return small;
    if(small > a[len - 1])
    {
        small = a[len - 1];
    }
    small = find_Min(a,len-1);
    //find_Min(a, len - 1);也行
}
template<clasee T> //先处理后递归
int recursiveMinimum( const T array[], int low, int high )
{
   static int smallest = MAXRANGE;  //静态变量;
   if ( array[ low ] < smallest )
      smallest = array[ low ];
   return low == high ? smallest : recursiveMinimum( array, low + 1, high ); //是返回还是递归。不等递归,相等返回。
}
template<class T> //先递归,后处理,在返回
int selectSmallIndex( const T str[] , int n )
{
    int small = 0;  //small作为返回值,可以不用静态变量
    if(n == 1)
        small = 0;
    else
    {
        small = selectSmallIndex(str,n - 1);
        if(str[small] > str[n - 1])
            small = n - 1;
    }
    return small;
}

3. 友元函数求最长公共单词

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
//最长公共单词
using namespace std;
void lcw(char [],char []);

int main()
{
    ifstream ifs("input.txt",ios::in);
    if(!ifs)
    {
        cerr << "File is cannot opened!!!" << endl;
        exit(1);
    }
    char str1[100],str2[100];
    ifs.getline(str1,100);
    ifs.getline(str2,100);
    cout << str1 << endl << endl << str2 << endl;
    cout << "max length word is: " << endl;
    lcw(str1,str2);
    return 0;
}

void lcw(char str1[], char str2[])
{
    char s1[100][20] = {""};   //拆分str1[]句子,分解成单词保存在s1中
    char s2[100][20] = {""};    //拆分str2[]句子,
    char common[100][20] = {""}; //保留公共单词
    char s[20];   //中间
    char *temp;
    
    temp = strtok(str1," .");
    int len1 = 0;
    while(temp)
    {
        strcpy(&s1[len1][0],temp);
        len1++;
        temp = strtok(NULL," .");
    }
    temp = strtok(str2," .");
    
    int len2 = 0;
    while(temp)
    {
        strcpy(&s2[len2][0],temp);
        len2++;
        temp = strtok(NULL," .");
    }
    
    size_t maxlen = 0,count = 0;
    for(int i = 0; i <= len1; i++)
    {
        strcpy(s,&s1[i][0]);
        for(int j = 0; j <= len2; j++)
            if(strcmp(s,&s2[j][0]) == 0)
            {
                strcpy(&common[count++][0],s);
                if(maxlen < strlen(s))
                    maxlen = strlen(s);
            }
    }
    
    for(int i = 0; i <= count; i++)
    {
        if(strlen(&common[i][0]) == maxlen)
            cout << &common[i][0] << " ";
    }
}

猜你喜欢

转载自blog.csdn.net/Dcwjh/article/details/88745111