這題是數學題,給矩形的長和寬,問最多可以擺幾個直徑為1單位的圓圈。只需考慮題目提供的grid或skew兩種排列方式。
值得一記的是,這題也算是我的第一個C++程式!
TIPS
測資 "0.9 10",output是 "0 grid"
CODE
#include <iostream>
#include <cmath>
using namespace std;
int skew(float width, float height);
int main()
{
int p, count;
char c, s[20];
float side[2];
p = count = 0;
while( (c = getchar()) != EOF ){
if( (c >= '0' && c <= '9') || c == '.' )
s[p++] = c;
else if( c == ' ' ){
if( p > 0 ){
s[p] = '\0';
sscanf(s, "%f", &side[count++]);
p = 0;
}//if valid input
}//else if c == '0'
else if( c == '\n' ){
if( p > 0 ){
s[p] = '\0';
sscanf(s, "%f", &side[count]);
int max, mtype = 0, tmp;
char type[2][5] = {"grid", "skew"};
if( count == 0 ){
max = (int)side[0] * (int)side[0];
if( (tmp = skew(side[0], side[0])) > max ){
max = tmp;
mtype = 1;
}
}//if square
else{
max = (int)side[0] * (int)side[1];
if( (tmp = skew(side[0], side[1])) > max ){
max = tmp;
mtype = 1;
}
if( (tmp = skew(side[1], side[0])) > max ){
max = tmp;
mtype = 1;
}
}//if rectangle
cout << max << ' ' << type[mtype] << '\n';
p = count = 0;
}//if valid input
}//else if c == '\n'
else ;
}//while input
return 0;
}
int skew(float width, float height){
if( width < 1.0 || height < 1.0 )
return 0;
int dh, sum = 0;
float th;//height of triangle
th = sqrt(3)/2;
dh = (int)( (height-1)/th );
dh++;
sum += (int)width * dh;
if( (width - (int)width) < 0.5 )
sum -= (int)( dh/2 );
return sum;
}
No comments:
Post a Comment