具体就是这样,这一题要求输出浮点数,可是假如除数是100的时候,答案会变成0(相当于一个小于除数的被除数相除得到0),而且后面不是字符串了直接变NULL了,换成100.0既有正确浮点数又有字符串
不应该啊,应该同时都存在字符串才对啊?
不应该啊,应该同时都存在字符串才对啊?
#include <iostream>
#include <algorithm>
#include <functional>
#include <math.h>
using namespace std;
struct _set
{
bool dir;
double pos;
char name[260];
bool operator < (const _set&x)const
{
return pos < x.pos;
}
}inhabitants[32001], exchange[32001];
int Search_Name(const int, const int);
void Merge_Sort(const int, const int);
void Merge(const int, const int, const int);
int main(void)
{
//最大距离位置距离方向有关
double length, rate, max_time;
int sum_people, max_pos, last_stop_pos;
char tmp_dir[2];
while (~scanf("%d", &sum_people))
{
if (sum_people == 0) break;
scanf("%lf%lf", &length, &rate);
getchar();
for (int i = 0; i < sum_people; i++)
{
scanf("%s %lf %s", tmp_dir, &inhabitants[i].pos, inhabitants[i].name);
inhabitants[i].dir = tmp_dir[0] == ""p"" || tmp_dir[0] == ""P"" ? 1 : 0;
}
sort(inhabitants, inhabitants + sum_people);
//Merge_Sort(0, sum_people - 1);
max_time = -1;
for (int i = 0; i < sum_people; i++)//找到最大的位置
{
if (inhabitants[i].dir == 1)
{
if ((length - inhabitants[i].pos) / rate> max_time)//正方向
{
max_time = (length - inhabitants[i].pos) / rate;
max_pos = i;
}
}
else
{
if (inhabitants[i].pos / rate > max_time)//反方向
{
max_time = inhabitants[i].pos / rate;
max_pos = i;
}
}
}
last_stop_pos = Search_Name(max_pos, sum_people);
printf("%13.2lf %s\n", int(100 * max_time) / 100.0, inhabitants[last_stop_pos].name);
}
return EXIT_SUCCESS;
}
int Search_Name(const int max_pos,const int sum_people)
{
int dir_count = 0;
if (inhabitants[max_pos].dir == 1)
{
for (int i = max_pos + 1; i < sum_people; i++)
if (inhabitants[i].dir == 0)
dir_count++;
return max_pos + dir_count;
}
else
{
for (int i = 0; i < max_pos; i++)
if (inhabitants[i].dir == 1)
dir_count++;
return max_pos - dir_count;
}
}
测试数据
4
10 1
p 1 Helga
n 3 Joanna
p 5 Venus
n 7 Clever
解决方案:10分
你单步跟踪程序怎么运行的,一步步看是哪出了问题
解决方案:30分
常量也有类型!
C++ Integer Constants
Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.
Syntax
integer-constant :
decimal-constant integer-suffixopt
octal-constant integer-suffixopt
hexadecimal-constant integer-suffixopt
“”c-char-sequence””
decimal-constant :
nonzero-digit
decimal-constant digit
octal-constant :
0
octal-constant octal-digit
hexadecimal-constant :
0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit
nonzero-digit : one of
1 2 3 4 5 6 7 8 9
octal-digit : one of
0 1 2 3 4 5 6 7
hexadecimal-digit : one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix :
unsigned-suffix long-suffixopt
long-suffix unsigned-suffixopt
unsigned-suffix : one of
u U
long-suffix : one of
l L
64-bit integer-suffix :
i64
To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type.
To specify a decimal constant, begin the specification with a nonzero digit. For example:
int i = 157; // Decimal constant
int j = 0198; // Not a decimal number; erroneous octal constant
int k = 0365; // Leading zero specifies octal constant, not decimal
To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example:
int i = 0377; // Octal constant
int j = 0397; // Error: 9 is not an octal digit
To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:
int i = 0x3fff; // Hexadecimal constant
int j = 0X3FFF; // Equal to i
To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example:
unsigned uVal = 328u; // Unsigned value
long lVal = 0x7FFFFFL; // Long value specified
// as hex constant
unsigned long ulVal = 0776745ul; // Unsigned long value
C++ Integer Constants
Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.
Syntax
integer-constant :
decimal-constant integer-suffixopt
octal-constant integer-suffixopt
hexadecimal-constant integer-suffixopt
“”c-char-sequence””
decimal-constant :
nonzero-digit
decimal-constant digit
octal-constant :
0
octal-constant octal-digit
hexadecimal-constant :
0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit
nonzero-digit : one of
1 2 3 4 5 6 7 8 9
octal-digit : one of
0 1 2 3 4 5 6 7
hexadecimal-digit : one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix :
unsigned-suffix long-suffixopt
long-suffix unsigned-suffixopt
unsigned-suffix : one of
u U
long-suffix : one of
l L
64-bit integer-suffix :
i64
To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type.
To specify a decimal constant, begin the specification with a nonzero digit. For example:
int i = 157; // Decimal constant
int j = 0198; // Not a decimal number; erroneous octal constant
int k = 0365; // Leading zero specifies octal constant, not decimal
To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example:
int i = 0377; // Octal constant
int j = 0397; // Error: 9 is not an octal digit
To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:
int i = 0x3fff; // Hexadecimal constant
int j = 0X3FFF; // Equal to i
To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example:
unsigned uVal = 328u; // Unsigned value
long lVal = 0x7FFFFFL; // Long value specified
// as hex constant
unsigned long ulVal = 0776745ul; // Unsigned long value