题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1881
题目大意:
给出的单词几张纸能够写下, 给出纸的行数h,每行的格数w,单词数 n,两个单词之间有空格, 一个单词要在同一行。
思路:
直接模拟,但是有几个地方需要注意:
1.我初始化行数为1,所以需要在最后判断列数,如果为零,就表示多出来一行,减下去。
2.可以使用向上取整函数,但是需要将h,w,n定义为实数,还有计算用的行数定义为实数。
3.我在c>w中,忘了将c重新赋值,一直过不了样例10
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
double h, w, n;
cin>>h>>w>>n;
char str[10010][110];
for(int i = 0; i < n; i++)
{
scanf("%s", str[i]);
}
double r = 1, c = 0;
for(int i = 0; i < n; i++)
{
c += strlen(str[i]);
if(c > w)
{
c = strlen(str[i]);
r++;
c++;
if(c >= w)
{
r++;
c = 0;
}
}
else if(c < w)
{
c++;
if(c >= w)
{
r++;
c = 0;
}
}
else
{
r++;
c = 0;
}
}
if(c == 0)
r--;
cout<<ceil(r/h)<<endl;
return 0;
}