中级农民
- 积分
- 155
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-4-17
- 最后登录
- 1970-1-1
|
写了下第一题
- public class ScreenWords {
- public static void main(String[] args) {
- String str = "Hello World";
- System.out.println(getNumberOfWordsCanPutInScreen(2, 17, str));
- str = "Hello Worlds";
- // System.out.println(getNumberOfWordsCanPutInScreen(1, 11, str));
- }
-
- public static int getNumberOfWordsCanPutInScreen(int m, int n, String str) {
- if (str.length() > n) {
- return 0;
- }
-
- String[] words = str.split(" ");
- int i = 0;
- int j = 0;
- int count = 0;
- while (i < m) {
- int len = -1;
- while (len + words[j].length() + 1 <= n) {
- len += words[j].length() + 1;
- j++;
- if (j == words.length) {
- // System.out.println(count + " " + words.length + " " + j);
- j = 0;
- count++;
- }
-
- }
-
- i++;
- }
-
- return count;
- }
- }
复制代码 |
|