代码改变世界

c艹学习之路(2)---函数,递归,穷举

2016-11-09 19:55  roffen  阅读(281)  评论(0编辑  收藏  举报

11.9

/*#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int judge(double side1, double side2);
int main()
{
cout << "side1\t" << "side2\t" << "side3\t" << endl;
double a, b;
for (a = 3;a < 500;a++)
{
for (b = a + 1 ;sqrt(b*b + a*a) <= 500;b++)
{
int i = judge(a, b);
if (i == 1)
{
cout << left << setw(8) << a << setw(8) << b << sqrt(b*b + a*a) << endl;
break;
}
}
}
// system("pause");
}
int judge(double side1, double side2)
{
double c = sqrt(side1*side1 + side2*side2);
int side3 = static_cast<int>(c);
if (side3 == c)
return 1;
else
return 0;
}*/
/*#include<iostream>
#include<fstream>
using namespace std;
double max(double a, double b, double c)
{
if (a > b&&a > c)//比较大小
return a;
if (b > c&&b > a)
return b;
if (c > a&&c > b)
return c;
else
return -1;//边界条件
}
void main()
{
cout << max(3.2, 7.4, 4.5) << endl;
system("pause");
}*/
/*#include<iostream>
using namespace std;
double shouFei(double money)
{
if (money <= 5000)
return(money / 100);
else
return 50;
}
int main()
{
cout << "输入汇款金额";
double m;
cin >> m;
cout << "本次汇款收费:" << shouFei(m) << endl;
system("pause");
}*/
/*#include<iostream>
using namespace std;
int jiechen(int n);//引入阶乘
int pailie(int zongshu,int a);//算小朋友的顺序
int zuhe(int zongshu,int a);//算书的取法
int borrow(int a, int b);
int main()
{
int a = 5, b = 3;
cout << "共有" << borrow(a,b) << "种不同的借书方式!" << endl;
system("pause");
}
int jiechen(int n)
{
if (n == 1|n==0)
return 1;
else
return n*jiechen(n - 1);
}
int pailie(int zongshu, int a)
{
return jiechen(zongshu) / jiechen(zongshu - a);
}
int zuhe(int zongshu, int a)
{
return pailie(zongshu,a) / jiechen(a);
}
int borrow(int a, int b)
{
return pailie(b, b)*zuhe(a, b);
}
int borrow(int n)
{
int a, b, c,d=0;
for (a = 0;a < n;a++)
{
for (b = 0;b <n ;b++)
{
if (b == a)
continue;
for (c = 0;c < n;c++)
{
if (c == a || c == b)
continue;
d++;
}
}
}
return d;
}
int main()
{
int n = 5;
cout << "共有" << borrow(n) << "种不同的借书方式!" << endl;
system("pause");
}*/
#include<iostream>
using namespace std;
int mySum(int m, int n);
int main()
{
cout << mySum(1, 5) << endl;
system("pause");
return 0;
}
int mySum(int m, int n)
{
int sum1 = 0;
for (int a = m;a <= n;a++)
{
int sum2 = 1;
for (int i = 1;i <= a;i++)
{
sum2 *= i;
}
sum1 += sum2;
}
return sum1;
}

 

 

 

11.1

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<iomanip>
#include<string>
using namespace std;
/*int main()
{
int a, b,c;
cout << "找质数:";
cin >> a;
cout << "质数有:1 ";
for (b = 2;b <= a;++b)//取遍到a的所有数
{
for (c = 2;c <= b;c++)//判断是不是质数
{
if (b%c == 0)
{
break;
}
}
cout << b << " ";
}
system("pause");
}*/
/*int main()//对于给的一个数,判断是不是质数
{
int a, b;
cout << "快来给a赋值吧w";
cin >> a;
if (a == 1|a==2)
cout << "质数";
for (b = 2;b < a;++b)
{
if (a%b != 0)
continue;
if (a%b == 0)
{
cout << "这不是质数";
break;
}
}
if (b == a)
cout << "质数";
system("pause");//成功了!w
}*/
/*int main()//对于给定一个数,取遍从1到这个数的所有质数
{
int a, b, c;
cout << "找质数了w:";
cin >> a;
cout << 1 <<" ";
for (b = 2;b <= a;++b)//取遍所有数
{
for (c = 2;c < b;++c)
{
if (b%c == 0)
break;
}
if (c == b)
cout << b << " ";
}
system("pause");//成功了,但貌似其中有个地方没搞懂,2是怎么出来的?搞懂了
}
*/
/* int dettriangle(double a, double b, double c);//c++作业1
int dettriangle(double a, double b, double c)
{
double max, mid, min;//find the biggest and the smallest
if (b >= a&&c >= b)
max = c, mid = b, min = a;
if (b >= a&&c < b&&c >= a)
max = b, mid = c, min = a;
if(b>=a&&c<b&&c<a)
max = b, mid = a, min = c;
if(b<a&&c>=a)
max = c, mid = a, min = b;
if(b<a&&c<a&&c>=b)
max = a, mid = c, min = b;
if(b<a&&c<a&&c<b)
max = a, mid = b, min = c;
while (min + mid > max&&max - mid < min&&max - min < mid)
return 1;
}
int main()//determine wheather the 3 numbers could represent a triangle
{
double a, b, c;
cout << "Please input 3 nonzero double values,and the program will determine if ther can represent a triangle:";
cin >> a >> b >> c;
int result = dettriangle(a, b, c);
if (result == 1)
cout << "they could represent a triangle." << endl;
else
cout << "they could not represent a triangle." << endl;
//system("pause");
}*/
/*int main()//to encrypt a four-digit integer
{
int num,a, b, c, d;
cout << "Please input a four-digit integer, and the process will encrypt it:";
cin >> num;
a = num / 1000, b = num / 100 - 10 * a, c = num / 10 - 100 * a - 10 * b, d = num % 10;//get the 4 numbers
if (a >= 10)
cout << "it is not a four-digit number!" << endl;
else
{
a = (a + 7) % 10, b = (b + 7) % 10, c = (c + 7) % 10, d = (d + 7) % 10;//swaping
num = c * 1000 + d * 100 + a * 10 + b;
cout << "the encrypted number is :" << num << endl;
}
// system("pause");
}*/
/*double function1(double n);//求通项
double function1(double n)
{
double sum=0 ,a ;
for (a = 1;a <= n;++a)
{
sum +=4 * pow(-1, a + 1) / (2 * a - 1);
}
return (sum);
}
int main()
{
double pi,a=1000,b;
cout << "Accuracy set at :1000";
cout << left << setw(50) <<"term" << "pi"<<endl;
for (b =1;b <= a;++b)
{
pi = function1(b);
cout << left<<setw(50)<<setprecision(8)<<b <<setprecision(8) <<pi << endl;
}
// system("pause");
}*/
/*int main()
{
int a, b, d;
cout << "The process will list the 同构数 from 1 to 1000." << endl;
cout << 1 << " ";
for (a = 2; a <= 1000; ++a)
{
b = a*a;
d = b - a;
if (a / 10 == 0 && d % 10 == 0)
cout << a << " ";
if (a / 100 == 0 && d % 100 == 0)
cout << a << " ";
if (a / 1000 == 0 && d % 1000 == 0)
cout << a << " ";
}
system("pause");
}*/
//写个程序做书上那个赌博色子
/* int useforshaizi();//函数原型
int main()
{
int a = 0;
enum zhuantai{WON,LOST,CONTINUE};
zhuantai youxizhuantai;
srand(time(0));
int b = useforshaizi();
switch (b)
{
case 7:
case 11:
youxizhuantai = WON;
break;
case 2:
case 3:
case 12:
youxizhuantai = LOST;
break;
default:
youxizhuantai = CONTINUE;
a = b;
cout << "point is " << a << endl;
break;
}
while (youxizhuantai == CONTINUE)
{
b = useforshaizi();
if (b == a)
youxizhuantai = WON;
else
if(b == 7)
youxizhuantai = LOST;
}
if (youxizhuantai == WON)
cout << "行行行你赢了";
else
cout << "你输了";
system("pause");
}
int useforshaizi()
{
int a1 = 1 + rand() % 6;
int a2 = 1 + rand() % 6;
int sum = a1 + a2;
cout << "你得到了" << a1<<"+"<<a2<<"="<<sum << endl;
return sum;
}//赌博伤身啊
*/
//write a program to do the sequence
int main()
{
int a = 0;
int list[10] = { 4,5,2,3,8,6,9,67,65,10 };
for (a = 1;a <= 9;a++)
{
int tem = list[a];
a-= 1;
while (list[a] > tem&a > -1)
{
list[a + 1] = list[a];
list[a] = tem;
a -= 1;
}
}
for (int i = 0;i <= 9;++i)
cout << list[i] << " ";
system("pause");
}

 

11.30

#include<iostream>
#include<iomanip>
using namespace std;
/*int judge(int n)
{
int a, b, c;
a = n / 100, b = n / 10 - a * 10, c = n % 10;//用于判断水仙花数
if (a*a*a + b*b*b + c*c*c == n)
return 1;
else
return 0;
}
int main()
{
int num[1000];
int n;
cin >> n;
for (int i = 0;i < n;i++)
{
int a;
cin >> a;
num[i] = a;
}
for (int i = 0;i < n;i++)
{
int j=judge(num[i]);
if (j == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
// system("pause");
}*/
/*int main()
{
int a[10000] = { 0 };
int b;
cin >> b;
int i = 0;
if (b > 2000000)
cout << "超出运算范围" << endl;
else
{
for (i;b != 0;i++)
{
a[i] = b % 2;
b /= 2;
}
i -= 1;
for (i;i >= 0;i--)
{
cout << a[i];
}
}
// system("pause");
}*/
/*double tax(double m)
{
if (m <= 1200)
return 0;
if (m <= 2200)
return (m - 1200)*0.05;
if (m <= 4200)
return (m - 1200)*0.1;
if (m <= 6200)
return (m - 1200)*0.15;
if (m <= 11200)
return (m - 1200)*0.2;
else
return(m - 1200)*0.3;
}

int main()
{
double income, j = 0;
cout << "请输入个人月收入:" << endl;
cin >> income;
j = tax(income);
cout << "应征所得税:" << j << endl;
system("pause");
return 0;
}*/
/*int Count(double a[], int n)
{
//算平均值
double sum=0;
for (int i = 0;i < n;i++)
{
sum += a[i];
}
double average = sum / n;
//取元素
int m = 0;//计数
for (int i = 0;i < n;i++)
{
if (a[i] > average)
{
m++;
}
}
return m;
}
void main()
{
int c;
double a[10] = { 34,54,75,86,53,45,34,45,34,45 };
c = Count(a, 10);
cout << "数组中大于等于所有元素平均值的元素个数为:" << c;
// system("pause");
}*/
/*int main()
{
int n = 0;
int a[10] = {0};
for (int i = 1;i < 100;i++)
{
if (i % 10 == 6 && i % 3 == 0)//筛选
{
a[n] = i;
n++;
}
}
system("pause");
}*/
//选择排序
/*int main()
{
int array[10] = { 15,45,78,95,25,6,5,44,1,21 };
cout << "before:";
for (int a = 0;a < 10;a++)
{
cout << array[a] << " ";
}
int tem, num;
for (int i = 0;i < 9;i++)
{
num = i;
for (int j = i + 1;j < 10;j++)
{
if (array[j] < array[num])
{
num = j;
}
}
tem = array[i];
array[i] = array[num];
array[num] = tem;
}
cout << endl << "later:";
for (int a = 0;a < 10;a++)
{
cout << array[a] << " ";
}
system("pause");
}*/
//冒泡排序
/*int main()
{
int array[10] = { 15,45,78,95,25,6,5,44,1,21 };
cout << "before:";
for (int a = 0;a < 10;a++)
{
cout << array[a] << " ";
}
for (int i = 0;i < 10;i++)
{
int n = 0;
for (int j = i + 1;j < 10;j++)
{
int max;
if (array[j] < array[i])
{
max = array[i];
array[i] = array[j];
array[j] = max;
++n;
}
}
if (n == 0)//尝试做优化
break;
}
cout <<endl<< "later:";
for (int a = 0;a < 10;a++)
{
cout << array[a] << " ";
}
system("pause");
}*/
//插入排序
/*int main()
{
int array[10] = { 15,45,78,95,25,6,5,44,1,21 };
cout << "before:";
for (int a=0;a < 10;a++)
{
cout << array[a] << " ";
}
int tem;
for (int i = 0;i < 10;i++)
{
for (int j = i;j >= 0;j--)
{
if (array[j - 1] > array[j])
{
tem = array[j];
array[j] = array[j - 1];
array[j - 1] = tem;
}
}
}
cout << endl << "later:";
for (int a = 0;a < 10;a++)
{
cout << array[a] << " ";
}
system("pause");
}*/
/*int main()
{
int a;
int arry[100][100] = { 0 };
cin >> a;
for ( int k=1;k <= a*a;k++)
{
if((k-1)/a)
}
system("pause");
}*/
//mooc homework
/*int main()
{
double a[3][5];
double at[5][3];
double b[9];
for (int i = 0;i < 3;i++)//输入15 个数
{
for (int j = 0;j < 5;j++)
{
double tem;
cin >> tem;
a[i][j]=tem;
}
}
for (int i = 0;i < 3;i++)//转置
{
for (int j = 0;j < 5;j++)
{
at[j][i] = a[i][j];
}
}
int c=0;
for (int k = 0;k < 3;k++)//矩阵乘法
{
for (int i = 0;i < 3;i++)
{
double sum = 0;
for (int j = 0;j < 5;j++)
{
sum += a[k][j] * at[j][i];
}
b[c] = sum;
c++;
}
}
for (int i = 0;i < 9;i++)//输出并控制格式
{
cout << setw(15) << b[i];
if (i % 3 == 2)
cout << endl;
}
system("pause");
}*/
//折半查找
/*int f(double a[], int n, int m, double b);
int main()
{
double a[10];
cout << "请输入由大到小顺序的呃十个数:";
for (int i = 0;i < 10;i++)
{
cin >> a[i];
}
cout << "好请输入一个数,我可以告诉你它在数组(即数组的index大小)里的位置:";
double b;
cin >> b;
//循环法
int j = 5,m=0,n=9,k=0;
for (int i = 0;i < 10;i++)
{
if (a[i]-b == 0)
{
k = 1;
break;
}
}
if (k == 0)
{
cout << "无此数";
}
else
{
while (a[j] != b)
{
if (a[j] > b)
{
m = j;
j = (j + n) / 2;
}
if (a[j] < b)
{
n = j;
j = (j + m) / 2;
}
if (m == 8)
j = 9;
}
cout << j;
}
system("pause");
}
//递归法
f(a, 0, 9, b);
system("pause");
}
int f(double a[], int n,int m, double b)//9 8 7 6 5 4 3 2 1 0
{
int j = (n + m) / 2;
if (n < m)
{
if (a[j] > b)
{
f(a, j + 1, m, b);
}
if (a[j] < b)
{
f(a, n, j, b);
}
if (a[j] == b)
cout<< j;
}
else if (n == m)
{
if (a[j] == b)
cout<< j;
else
cout<< "无此数";
}
return 0;
}*/
//转换大小写
/*int main()
{
char a;
while (cin >> a)
{
if (a >= 97 && a <= 122)//通过ascll码表中小写和大写相差32
{
a -= 32;
}
cout << a;
}
}*/
//比大小
/*int f1(int * a);//得出要比较的数
int compare(int *a, int *b);//比大小
int main()
{
int result, i;
int a[10], b[10];

cout << "请输入第一个整数的各位数字:";
for (i = 0; i < 10; i++)
{
cin >> a[i];
}
cout << "请输入第二个整数的各位数字:";
for (i = 0; i < 10; i++)
{
cin >> b[i];
}
result = compare(a, b);
if (result == 1) cout << "第一个整数较大";
else if (result == 0) cout << "两个整数相等";
else if (result == -1) cout << "第二个整数较大" << endl;

system("pause");
return 0;
}
int f1(int *a)
{
int i = 0, j = 9;
for ( i ;a[i] == 0;i++)
{
}
for (j;a[j] == 0;j--)
{
}
int k = j-i+1;
for (int m = 0;m < k;m++)
{
a[m] = a[i];
i++;
}
return k;
}
int compare(int *a, int *b)
{
int size1 = f1(a), size2 = f1(b);
if (size1 > size2)
{
return 1;
}
else if (size1 < size2)
{
return -1;
}
else
{
int z = 0;
for (int i = 0;i < size1;i++)
{
if (a[i] > b[i])
{
z = 1;
break;
}
else if (a[i] < b[i])
{
z = -1;
break;
}
}
return z;
}
}*/
//求靶点
/*void gfkd(int s1[], int s2[], int num);

int find(int a[][5], int s1[], int s2[])
{
int f=0, index = 0;//f为返回值,index为s1,s2的下标
for (int j = 0;j < 5;j++)//控制行
{
int max = 0,is1=0;
for (int i = 0;i < 5;i++)//找到每行最大值
{
if (a[j][i] >= max)
{
max = a[j][i];
is1 = i;
}
}
int judge = 0;
for (int k = 0;k < 5;k++)//判断每行最大值是否是该列最小值
{
if (a[k][is1] < max)
{
judge = 1;
}
}
if (judge == 0)//找到值
{
s2[index] = is1;
s1[index] = j;
index++;
f++;
}
}
return f;
}

void gfkd(int s1[], int s2[], int num)
{
for (int i = 0; i<num; i++)
{
cout << s1[i] << ends;
cout << s2[i] << endl;
}
}

int main()
{
int a[5][5], count;
int s1[5], s2[5];

for (int i = 0; i<5; i++)
for (int j = 0; j<5; j++)
cin >> a[i][j];
count = find(a, s1, s2); //count中为鞍点个数
gfkd(s1, s2, count);
system("pause");
return 0;
}*/

11.20

//关于递归

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<iomanip>
#include<string>
#include<cstring>
using namespace std;
//写个汉诺塔吧
/*int hanoi(int n, char a, char b, char c)
{
if (n == 0)
return 0;
else
{
hanoi(n - 1, a,c ,b);
cout << a << "=>" << c << endl;
hanoi(n - 1, b, a, c);
}
}
int main()
{
int m;
cout << "input the number of diskes:" << endl;
cin >> m;
hanoi(m, 'A', 'B', 'C');
system("pause");
}*///成功了
//勒让德多项式
/*double P(int n, int x)
{
if (n == 0)
return 1;
else if (n == 1)
return x;
else
return ((2 * n - 1)*x*P(n - 1, x) - (n - 1)*P(n - 2, x)) / n;
}
int main()
{
int n, x;
cout << "勒让德多项式\nplease input n:";
cin >> n;
cout << "please input x:";
cin >> x;
cout << "the result is"<<P(n, x) << endl;
system("pause");
}*/
/*int Acm(int m, int n)
{
if (m == 0)
return n + 1;
else if (m > 0 && n == 0)
return Acm(m - 1, 1);
else if (m > 0 && n > 0)
return Acm(m - 1, Acm(m, n - 1));
}
int main()
{
int m, n, a;
cout << "Ackman\nplease input m:";
cin >> m;
cout << "please input n:";
cin >> n;
a = Acm(m, n);
printf("Acm(%d,%d)=%d\n", m, n, a);
//cout << "the result is " << Acm(m, n) << endl;
system("pause");
}*/
/*int reverse()
{
int n;
cin >> n;
if (n == -1)
return 0;
else
{
reverse();
cout << n << " " << endl;
}
}
int main()
{
reverse();
system("pause");
}*/
/*int intreverse(int n)
{
int b=0;
static int a = 0;//计数器
a++;
if (a > 100)//不知道如何设置边界条件
cout<<"error!";
else
{
if (n == 0)
return 0;
else
{
b = n % 10;
n /= 10;
cout << b ;
intreverse(n);
}
}
}
int main()
{
int a;
cin >> a;
intreverse(a);
system("pause");
}*/
//the second type
/*int main()
{
char intreverse[100];
cin.getline(intreverse, 100);
int i;
for (i = strlen(intreverse) - 1;i >= 0;i--)
cout << intreverse[i];
system("pause");
}*/
char chareverse()
{
char n;
static int a ;
cin >> n;
if (n == '#')
return 0;
else if (a >= 99)
{
cout << "error!"<<endl;//成功,小问题是abcd#时输出为空,解除,把5改成4
a++;
}
else
{
a++;
chareverse();
if (a >= 100)
cout << "";
else
{
cout << n;
}
}
}
int main()
{
chareverse();
system("pause");
}