#include <iostream>
#include <string>
#include <cmath>
using namespace std;
/**
使用函数调用改写for循环练习
*/
// 判断一个数是否为素数
bool isPrime(int num, int value)
{
if (value == num - 1 || num == 2 || num == 1)
return true;
return (num % value == 0) ? false : isPrime(num, ++value);
}
void test_sushu()
{
cout << "please input a number:";
int num;
cin >> num;
if (isPrime(num, 2))
cout << "it is a prime" << endl;
else
cout << "it isn't a prime" << endl;
}
// 字符串分割练习改写
void split(string str, int i, char ch)
{
if(i==str.length()-1) return;
else {
if(str.at(i)!=ch) cout<<str.at(i);
else cout<<endl; split(str,i+1,ch);
}
}
// 获取一个数组的最小值以及下标改写
void getMin(int array[], int i, int size, int index, int value)
{
if (i == size - 1)
cout << "最小值的下标为 " << index << " ,值为:" << value << endl;
else
{
if (value > array[i])
{ index = i + 1;value = array[i];}
getMin(array, i + 1, size, index, value);
}
}
int main()
{
cout << "-------------------begin--------------------------\n"
<< endl;
test_sushu();
cout << "-------------------begin--------------------------\n"
<< endl;
int array[] = {10, 2, 6, 1, 4, 5, 7, 1, 9, 14};
getMin(array, 0, 10, 0, array[0]);
cout << "-------------------begin--------------------------\n"
<< endl;
string str = "Nice to meet you !";
split(str,0,' ');
cout << "\n--------------------end----------------------------" << endl;
return 0;
}
运行效果如下: