第1题
分析:这道题不能用float或double,因为输入数据的小数位数是不固定的,所以没法控制输出数据的精度。下面的这个程序无法得到满分:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
freopen("negative.in", "r", stdin);
freopen("negative.out", "w", stdout);
double a;
cin >> a;
if('-' == a)
{
cout << a << endl;
}
else
{
cout << '-' << a << endl;
}
return 0;
}
运行结果:
3.1415926535897
-3.14159
正确的写法:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
freopen("negative.in", "r", stdin);
freopen("negative.out", "w", stdout);
char a[1000000]; // 或写成string a;
cin >> a;
if('-' == a[0])
{
cout << a << endl;
}
else
{
cout << '-' << a << endl;
}
return 0;
}
注意:区分string.h, cstring和string这三个头文件。
第2题
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
freopen("speed.in", "r", stdin);
freopen("speed.out", "w", stdout);
int v;
cin >> v;
int level;
if(v < 7960)
{
level = 0;
}
else if(v >= 7960 && v <= 11200)
{
level = 1;
}
else if(v >= 11200 && v < 16700)
{
level = 12;
}
else if(v >= 16700 && v < 115000)
{
level = 123;
}
else if(v >= 115000 && v < 2000000)
{
level = 1234;
}
else
{
level = 5;
}
cout << v << ' ' << level << endl;
return 0;
}
第3题
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
// freopen("corn.in", "r", stdin);
// freopen("corn.out", "w", stdout);
int n, m;
cin >> n >> m;
int a[n][m];
int mn = 10000, mx = -1;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> a[i][j];
if(a[i][j] > mx)
{
mx = a[i][j];
}
if(a[i][j] < mn)
{
mn = a[i][j];
}
}
}
cout << mx - mn << endl;
return 0;
}
完整答案请加微信307591841或QQ307591841
公众号.jpg
网友评论