pascal读取输入错误的,过来看

首先,感谢这位老兄的共享。如果没有他的解题提示,估计还要摸索再多时间。

http://hi.baidu.com/bgfumsjefublmtd/item/25c78d89a13658efd1f8cd97

var
i,j:longint;
ca,cb:char;
begin
readln( i,j );
writeln( i,'/',j );
end.
// 3 33

当输入两个整数时,系统会自动忽略掉空白符(包括空格、Tab、换行符等)。
系统也依赖于这些空白符,作为分隔符,来取得整数。

var
ca,cb:char;
begin
readln( ca, cb );
writeln( '/',ca,'/',cb,'/' );
end.
// c c

但是,当输入两个字符时,中间不能加空格。加了空格之后,结果不是我们希望的。
读入字符时,与读取整数时不同,是连续读取的,包括读取空格。

var
i,j:longint;
ca,cb:char;
begin
readln( i,ca );
writeln( i,'/',ca,'/' );
end.
// 3 a
// 3a

当在一行中,先读整数,再读字符时,我们会陷于两难境地。
如果输入:
3 a
那么,ca中读取的是空格,而不是字符'a'。
如果输入:
3a
那么整数缺少正确的分隔符,把‘3a’作为整体读取。
当把这个字符串,转换成整数的时候,就会出现106号错误:
106 Invalid numeric format Reported when a non-numeric value is read from a text file,and a numeric value was expected.

var
i,j:longint;
ca,cb:char;
begin
readln( i,ca,ca );
writeln( i,'/',ca,'/' );
end.
// 3 a

解决方法不是木有。字符读取两次,第一次读取的是空格,第二次读取的是我们所希望的。
而整数也能够得到正确的读取。空格在这里,突显非常重要了,是不是?

var
i,j:longint;
ca,cb:char;
begin
readln( i, ca, ca, cb, cb, j );
writeln( i,'/',ca,'/',cb,'/', j );
end.
// 33 a b 35

那么,先读取整数,再读取字符、再读取字符。。。再读取整数的解决方法就有了。

本题估计pascarer被坑爹的不少。
最后说下,这题不一定要建立数组的,边读取数据,边判断,一样可以AC,还可以节省时间:)
附上AC的pascal代码。C、和C++读取数据一点问题也木有。后面把C++代码也附上了。

//fp
var
str,na:string;
qm,bj,fb,sum:longint;
c,xs,xb:char;
n,max,sum2:longint;
begin
max:=0; sum2:=0;
readln( n );
while( n<> 0 ) do begin
dec(n);
str:='';
while( true ) do begin
read(c);
if( c<>' ' ) then str+=c
else break;
end;
readln( qm, bj, xs, xs, xb, xb, fb ); //怪异、却正确的读入方法
//
sum:=0;
if( (qm>80) and (fb>=1) ) then sum+=8000;
if( (qm>85) and (bj>80) ) then sum+=4000;
if( (qm>90) ) then sum += 2000;
if( (qm>85) and (xb='Y' ) ) then sum+=1000;
if( (bj>80) and (xs='Y' ) ) then sum+=850;
sum2 += sum;
if( sum>max) then begin
max:=sum;
na:=str;
end;
end;
writeln( na );
writeln( max );
writeln( sum2 );
end.

//--------------------------------------------------------------------------

//Cpp

#include <iostream>
#include <string>
using namespace std;

string name, na;
int qm, bj, fb, sum;
char xs, xb;

int main()
{
int n,max=0,sum2=0;
cin >> n;
while( n-- ) {
sum = 0;
cin >> name >> qm >> bj >> xs >> xb >> fb;
if( qm>80 && fb>=1 ) sum += 8000;
if( qm>85 && bj>80 ) sum += 4000;
if( qm>90 ) sum += 2000;
if( qm>85 && xb=='Y' ) sum += 1000;
if( bj>80 && xs=='Y' ) sum += 850;
sum2 += sum;
if( sum > max ) {
max = sum;
na = name;
}
}
cout << na << endl;
cout << max << endl;
cout << sum2 << endl;
return 0;
}

//end.

2 条评论

  • @ 2014-08-19 15:22:52

    赞一个

  • @ 2014-05-03 16:45:08

    道理跟string一样吧。我上次试了也要在中间加一个字符。

  • 1

信息

ID
1001
难度
5
分类
模拟 点击显示
标签
递交数
39080
已通过
12716
通过率
33%
被复制
121
上传者