2019网安复试题

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

方法1:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
bool fun(int );

int main()
{
    for(int i = 1; i < 10000; 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 len = 1;
    int n = num;   //备份用,防止修改
    while(n/=10) //统计num个数
        len++;
    for(int j = 0; j < len; j++)
    {
        int a = num%10;
        int b = square%10;
        if(a!=b)
            return false;
        num /=  10;
        square /=  10;
    }
    return true;
}

方法2:

#include<iostream>
#include<string>

using namespace std;
//写一个函数,判断一个正整数是否为同构数,正整数范围[1,1000];
//例:376*376=14376

bool judge(int);

int main() {
	int n;
	cout << "Enter 1~20000 number: " << endl;
	cin >> n;
    if (judge(n))
		cout << "yes" << endl;
	else
		cout << "no" << endl;
	return  0;
}

bool judge(int n) {
		string m_str = to_string(n*n);
		string n_str = to_string(n);
		int mlen = m_str.length();
		int nlen = n_str.length();
		string b = m_str.substr(mlen-nlen,nlen);
		if (b == n_str)
            return true;
		return false;
 }

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. 友元函数求最长公共单词

selfString.h

#ifndef SELFSRING_H
#define SELFSRING_H
#include <fstream>

using namespace std;

class selfSring
{
    friend void fun(const selfSring &,const selfSring &);
    public:
        selfSring(ifstream &);
        virtual ~selfSring();
    private:
        char *dataStr;
        int length;
};

#endif // SELFSRING_H

selfString.cpp

#include "selfSring.h"
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const static int SIZE = 1000;

selfSring::selfSring(ifstream &input)
{
    char *temp = new char[SIZE];
    input.getline(temp,SIZE);
    length = strlen(temp);
    dataStr = new char[length];
    strcpy(dataStr,temp);
    delete[] temp;
}
 void fun(const selfSring &str1,const  selfSring &str2)
{
    char s1[100][20] = {""};   //拆分str1[]句子,分解成单词保存在s1中
    char s2[100][20] = {""};    //拆分str2[]句子,
    char tstr1[str1.length];
    char tstr2[str2.length];
    strcpy(tstr1, str1.dataStr);
    strcpy(tstr2, str2.dataStr);

    char *temp;                      //分解句子str1
    temp = strtok(tstr1," .");
    int len1 = 0;
    while(temp)
    {
        strcpy(&s1[len1++][0],temp);
        temp = strtok(NULL," .");
    }
                                    //分解句子str2
    temp = strtok(tstr2," .");
    int len2 = 0;
    while(temp)
    {
        strcpy(&s2[len2][0],temp);
        len2++;
        temp = strtok(NULL," .");
    }
                                        //保留公共单词
    size_t maxlen = 0,count = 0;
    char common[100][20] = {""};
    char s[20];
    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] << " ";
    }
}
selfSring::~selfSring()
{
    delete[] dataStr;
}

main.cpp

#include <iostream>
#include <fstream>
#include "selfSring.h"

using namespace std;

int main()
{
    ifstream input("input.txt",ios::in);
    if(!input)
    {
        cerr << "file cannot be opened!!!" << endl;
        exit(1);
    }
    selfSring str1(input);
    selfSring str2(input);
    fun(str1,str2);
    input.close();
    return 0;
}

input.txt

hello world my friend mmnnbb
yes  mmnnbb am you my father friend

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值