/**************************************
*Headline: 18年瓜大机试解答
*Author: 周小枫
*Email: weijinxiong.ncu@outlook.com
*Date: 2019-1-4
*Brief: 这是一份菜鸡帮更菜的鸡写的参
考code,经测试,以下code都OK,如有错误
欢迎大佬指正,
**************************************/
1:求积
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"please enter the number of data group"<<endl;
cin>>n;
if(n<=0)
return 0;
while(n--){
int i,j;
cout<<"please enter two numbers"<<endl;
cin>>i>>j;
cout<<"the mutip of two num is :"<<i*j<<endl;
}
return 0;
2:阶乘
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"please enter the number of data group"<<endl;
cin>>n;
if(n<=0)
return 0;
while(n--){
int i,sum=1;
cin>>i;
if(i<=0)
return 0;
for(int j=1;j<=i;j++){
sum*=j;
}
cout<<"the jiecheng of i is :"<<sum<<endl;
}
return 0;
}
3:C(n,m),排列组合
#include <iostream>
using namespace std;
int main()
{
cout<<"please input two numbers"<<endl;
int i,j;
cin>>i>>j;
if(i<=0||j<=0)
return 0;
int sum1=1,sum2=1;
for(int k=i,m=j;k>=1&&m>0;k--,m--){
sum1 *= k;
sum2 *= m;
}
cout<<"The result is :"<<sum1/sum2<<endl;
return 0;
}
4:n组数,每组m个,每组数由小到大排序输出
#include <iostream>
using namespace std;
void mysort(int* a,int m){
int temp;
for(int i=0;i<m-1;i++)
for(int j=0;j<m-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
int main()
{
int n,m;
cout<<"please input the number of group and num of each member:"<<endl;
cin>>n>>m;
if(n<=0||m<=0)
return 0;
int a[n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>a[i][j];
}
for(int i=0;i<n;i++)
mysort(a[i],m); //暂时用的冒泡,后面调试下快排
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
5:字符串反转
#include <iostream>
using namespace std;
int main()
{
int n,top=0;
cout<<"please input the number of group:"<<endl;
cin>>n;
if(n<=0)
return 0;
char s[n][20];
for(int i=0;i<n;i++)
cin>>s[i];
char st[20];
for(int i=0,j=0;i<n;i++){
while(s[i][j]!='\0')
st[top++]=s[i][j++];//入栈
while(top>=0)
cout<<st[--top];
cout<<endl;
top=0,j=0;//宝贝记得要清空哦
}
return 0;
}
6:判断是否“回文”,即从字符串左右看都一样
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
cout<<"please input the number of group:"<<endl;
cin>>n;
if(n<=0)
return 0;
char s[n][20];
for(int i=0;i<=n;i++)
cin.getline(s[i],20);
for(int i=1;i<=n;i++){
int left=0;
bool flag=true;
int right=strlen(s[i])-1;
while(left<right){
if(s[i][left]!=s[i][right]){//判断两端是否一致
flag=false;
break;
}
else{
left++;
right--;
}
}
if(flag == true)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}
7:括号匹配
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
cout<<"please input the number of group:"<<endl;
cin>>n;
if(n<=0)
return 0;
char s[n][20];
for(int i=0;i<=n;i++)
cin.getline(s[i],20);//获取键盘输入
for(int i=1;i<=n;i++){
bool flag=true;
int len=strlen(s[i]);
char st[len];int top=0;//建造栈
for(int j=0;j<len;j++){
switch(s[i][j]){
case '{':
case '[':
case '(':st[top++]=s[i][j]; break;//老弟入栈了
case '}':
if(st[--top]!='{') //出栈认亲
flag=false;
break;
case ']':
if(st[--top]!='[')
flag=false;
break;
case ')':
if(st[--top]!='(')
flag=false;
break;
}
}
if((flag==true)&&top==0) //栈空且flag为true
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}