C++经典题
·
1回型矩阵
#include<iostream>
using namespace std;
int main()
{
//1,是否越界
//2,是否赋过值
int t;
cin>>t;
while(t--)
{
int a[100][100] ={0};
a[1][1]=1;
int n;
cin>>n;
int x=1;
/*想象一下,你在一张白纸上画螺旋线。每画完一幅画,要画下一幅时,你都必须:
把笔放回左上角(x=1, y=1)
把数字重新从 1 开始写(s=2,因为 1 已经写在了左上角)
换一张全新的白纸(a 数组重新全部清零)
如果不重置,会发生什么?
第一幅画画完,笔停在了纸的某个位置,纸也被写满了数字。
第二幅画你如果直接开始,就会在脏纸上从奇怪的位置继续写旧数字,画出来的东西根本不是螺旋,乱成一团。
所以,重置就是“为新的一题换一张白纸、把笔归位、把数字归零”。
把 x=1, y=1, s=2 写在循环里面,就是每轮循环都自动做这件事。写在循环外面,就等于只在第一题前做了一次,后面全乱套。
这就像每天起床都要重新穿衣服一样,不能穿着昨天的衣服直接出门(除非你不换洗)。每一组测试数据都需要一个干净、独立的开始。
*/
int y=1;
int s=2;
while(s<=n*n)
{
//右
while(y+1<=n&&a[x][y+1]==0)//y是列
{
y++;//y+是往右
a[x][y]=s;
s++;
}
//下
while(x+1<=n&&a[x+1][y]==0)//y是列
{
x++;//x+是往下
a[x][y]=s;
s++;
}
//左
while(y-1>=1&&a[x][y-1]==0)
{
y--;
a[x][y]=s;
s++;
}
//上
while(x-1>=1&&a[x-1][y]==0)
{
x--;
a[x][y]=s;
s++;
}
}
for(int i=1;i<n+1;i++)
{
for(int j=1;j<n+1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}
2指针函数本质:想要一个函数作为另一个函数的形参
题目描述
以下代码计算[a,b)范围内,0.1为步长的三角函数(sin或cos或tan)之和。请补齐sigma函数。
输入
多组测试数据,每行数据格式为:
三角函数类型(sin或cos或tan) 起始范围a 终止范围b
输出
对每组测试数据输出[a,b)内,步长0.1的三角函数和。
#include<iostream>
#include<cmath>//sin cos
#include<cstring>//strcmp
using namespace std;
//在 C/C++ 中,函数名本身就代表这个函数的地址。
//double (*f)(double);f 是指针变量名。
//double 表示这个指针指向的函数返回 double。
//(double) 表示这个函数需要一个 double 参数。
///前面的括号 (*f) 是为了和返回指针的普通函数声明区分开。
//现在 f 就是一个可以指向 sin、cos、tan 这类函数的指针。
//想要实现sigma(sin, a, b);就要 double sigma(double (*f)(double), double a, double b)
double sigma(double(*f)(double),double a,double b)
{
double sum=0.0;
for(double x=a;x<b;x=x+0.1)
{
sum=sum+f(x);
}
return sum;
}
//main 函数:读取输入、判断三角函数类型、调用 sigma
int main()
{
char s[20];
double a,b;
while(cin>>s>>a>>b)//没有输入自动停止
{
if(strcmp(s,"sin")==0)//说明s和sin完全相同
{
cout<<sigma(sin,a,b)<<endl;
}
else if(strcmp(s,"cos")==0)//strcmp 需要两个 const char* 类型参数,但代码中写的是 strcmp(s, sin),sin 是一个函数名,类型是函数指针,不是字符串。应该传入字符串 "sin",即 strcmp(s, "sin")==0。同样 cos、tan 也要改成 "cos"、"tan"。
{
cout<<sigma(cos,a,b)<<endl;
}
else if(strcmp(s,"tan")==0)
{
cout<<sigma(tan,a,b)<<endl;
}
//strcmp只比较第一个不同的ASCII值,剩下都不管
}
return 0;
}
3引用排序:就是不用真身排序,而是用代号排序
题目描述
输入三个整数,然后按照从大到小的顺序输出数值。
要求:定义三个整数的引用,通过引用方法来对三个数进行排序。
要求:不能直接对三个整数进行排序,必须通过引用的方法。
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b,c;
cin>>a>>b>>c;
int &x=a;
int &y=b;
int &z=c;
if(x<y)//三个都要过一遍,所以是ififif
{
int tem=x;
x=y;
y=tem;
}
if(x<z)
{
int tem=x;
x=z;
z=tem;
}
if(y<z)
{
int tem=y;
y=z;
z=tem;
}
cout<<x<<" "<<y<<" "<<z<<endl;
}
return 0;
}
4引用求最值下标
题目描述
编写函数void find(int *num,int n,int &minIndex,int &maxIndex),求数组num(元素为num[0],num[1],...,num[n-1])中取最小值、最大值的元素下标minIndex,maxIndex(若有相同最值,取第一个出现的下标。)
输入n,动态分配n个整数空间,输入n个整数,调用该函数求数组的最小值、最大值下标。最后按样例格式输出结果。
改变函数find功能不计分。
#include<iostream>
using namespace std;
void find(int *num,int n,int &miniIndex,int &maxIndex)//num是数组,n是指有几个数
{
miniIndex=maxIndex=0;
for(int i=0;i<n;i++)
{
if(num[i]<num[miniIndex])
{
miniIndex=i;
}
if(num[i]>num[maxIndex])
{
maxIndex=i;
}
}
}
int main()
{
int t;
int miniIndex,maxIndex;
cin>>t;
int max,min;
while(t--)
{
int n;
cin>>n;
int* num=new int[n];
for(int i=0;i<n;i++)
{
cin>>num[i];
}
find(num,n,miniIndex,maxIndex);
max=num[maxIndex];
min=num[miniIndex];
cout<<"min="<<min<<" "<<"miniIndex="<<miniIndex<<endl
<<"max="<<max<<" "<<"maxIndex="<<maxIndex<<endl;
cout<<endl;
delete[] num;
}
return 0;
}
更多推荐

所有评论(0)