拼多多笔试题第二题解。。

第二题写的好乱,但是笔试的时候根本不考虑这个了。。。

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
char[][] c = new char[n][m];
for(int i=0;i<n;i++){
String str = in.next();
c[i] = str.toCharArray();
}

    for(int i=0;i<m;i++){
        boolean f = false;
        int count1 = 0;
        int count2 = 0;
        int last = 0;
        for(int j=0;j<n;j++){
            if(c[j][i]=='x'){
                f = true;
                for(int p=0;p<count2;p++){
                    c[p+last][i] = '.';
                }
                for(int p=0;p<count1;p++){
                    c[p+last+count2][i] = 'o';
                }
                count1 = 0;
                count2 = 0;
                last = j+1;
            }else if(c[j][i]=='o'){
                count1 ++;
            }else if(c[j][i]=='.'){
                count2 ++;
            }
        }
        if(f==false){
            for(int j=0;j<n;j++)
               c[j][i] = '.';
        }
        for(int p=last;p<n;p++){
            c[p][i] = '.';
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            System.out.print(c[i][j]);
        }
        System.out.println("");
    }
}

}

#拼多多##题解#
全部评论
include <iostream> using namespace std; int main(){ int m, n; cin >> m >> n; char ** arr = new char *[m]; for (int i = 0; i < m; ++i) { arr[i] = new char[n]; } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { cin >> arr[i][j]; } } for (int i = 0; i < n; ++i) { if (arr[m - 1][i] != 'x') { arr[m - 1][i] = '.'; } } for (int i = 0; i < n; ++i)//列 { for (int j = m - 2; j >= 0; --j)//行 { if (arr[j][i] == 'o') { int k = j + 1; while (k < m) { if (arr[k][i] == 'x' || arr[k][i] == 'o') { arr[k - 1][i] = 'o'; arr[j][i] = '.'; break; } ++k; } if (k == m) { arr[j][i] = '.'; } } } } //输出 for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { cout << arr[i][j]; } cout << endl; }
点赞 回复 分享
发布于 2018-08-30 21:09
public class Main01 {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int n = sc.nextInt();         int m = sc.nextInt();         char[][] c = new char[n][m];         for(int i = 0;i<n;i++) {             for(int j = 0;j<m;j++) {                 c[i][j] = sc.next().charAt(0);             }         }         for(int j = 0;j<m;j++) {             int hasX = 0;             int count = 0;             for(int i = n-1;i >= 0;i--) {                 if(hasX == 0) {                     if(c[i][j] == 'o') {                         c[i][j] = '.';                     }                 }                 if(c[i][j] == 'x') {                     hasX = i;                     count = 0;                 }                               if(hasX != 0) {                      if(c[i][j] == 'o') {                         c[i][j] = '.';                         count += 1;                         c[hasX-count][j] = 'o';                     }                 }             }         }                  for(int i = 0;i<n;i++) {             for(int j = 0;j<m;j++) {                 System.out.print(c[i][j]+" ");             }             System.out.println();         }              } }
点赞 回复 分享
发布于 2018-08-30 21:11
有Python的思路吗
点赞 回复 分享
发布于 2018-08-30 21:13
import java.util.*; public class Main{     public static void main(String[] args){        Scanner sc = new Scanner(System.in);        int N = sc.nextInt();        int M = sc.nextInt();        char[][] arr = new char[N][M];         char[][] result = new char[N][M];        for(int i = 0; i < N; i++){            String string = sc.next();            char[] temp = string.toCharArray();            for(int j = 0; j < M; j++){                arr[i][j] = temp[j];                result[i][j] = '.';            }        }        for(int j = 0; j < M; j++){            ArrayList<Character> list = new ArrayList<Character>();            for(int i = 0; i < N; i++){                if(arr[i][j] == 'o'){                    list.add('o');                }else if(arr[i][j] == 'x'){                    result[i][j]='x';                    for(int k = i-1; k >= i-list.size(); k--){                        result[k][j] = 'o';                    }                    list.clear();                }            }        }         for(int i = 0; i < N; i++){                         for(int j = 0; j < M; j++){               System.out.print(result[i][j]);             }             System.out.println();         }     } }
点赞 回复 分享
发布于 2018-08-30 21:14
import sys line=sys.stdin.readline().strip().split() n=int(line[0]) m=int(line[1]) chess=list() for i in range(n):     lineList=list()     line=sys.stdin.readline().strip()     for j in range(m):         if line[j]=='.':             lineList.append(0)         elif line[j]=='o':             lineList.append(1)         else:             lineList.append(2)     chess.append(lineList) for j in range(len(chess[0])):     partCount=0     partBottomIndex=len(chess)     colList=[0 for col in range(len(chess))]     for i in reversed(range(len(chess))):         if chess[i][j]==1:             partCount+=1         elif chess[i][j]==0:             continue         else:             colList[i]=2             if (partBottomIndex<len(chess)):                 for index in range(partCount):                     colList[partBottomIndex-index-1]=1             partCount=0             partBottomIndex=i     if (partBottomIndex<len(chess)):               for index in range(partCount):             colList[partBottomIndex-index-1]=1              for index in range(len(chess)):         chess[index][j]=colList[index] strList=list() for i in range(len(chess)):     string=''     for j in range(len(chess[0])):         if chess[i][j]==0:             string+='.'         elif chess[i][j]==1:             string+='o'         else:             string+='x'     strList.append(string) for string in strList:     print string                                          
点赞 回复 分享
发布于 2018-08-30 21:14
if __name__ == '__main__':     N,M = [int(x) for x in input().split()]     print(N,M)     rows = []     for i in range(N):         rows.append(input())     print(rows)     for i in range(N):         rows[i] = list(rows[i])     print(rows)     for i in range(N-1,-1,-1):         for j in range(M):             if rows[i][j] == 'o' :                 k = i                 while k < N and rows[k][j] == 'o':                     if k < N-1:                         if rows[k+1][j] == '.':                             rows[k][j] = '.'                             rows[k+1][j] = 'o'                     else:                         rows[k][j] = '.'                     k += 1     new_row = []     for i in range(N):         temp_row = ''.join(rows[i])         new_row.append(temp_row)     for i in new_row:         print(i)
点赞 回复 分享
发布于 2018-08-30 21:15
#include #include #include using namespace std; int main(int argc, char const *argv[]) { int N, M; cin >> N >> M; string state_str; vectorchar>> chess; for (int i = 0; i < N; i++) { cin >> state_str; vectorchar> row; for (int j = 0; j < M; j++) { row.push_back(state_str[j]); } chess.push_back(row); } vectorchar> aug_row; for (int i = 0; i < M; i++) { aug_row.push_back('.'); } chess.push_back(aug_row); for (int i = N - 1; i > -1; i--) { for (int j = 0; j < M; j++) { if(chess[i][j] != 'o') continue; int heigh_idx = i; while (heigh_idx < N) { if (chess[heigh_idx + 1][j] == '.') { chess[heigh_idx + 1][j] = chess[heigh_idx][j]; chess[heigh_idx][j] = '.'; } else { break; } heigh_idx++; } chess[N][j] = '.'; } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cout<<chess[i][j]; } cout<<endl; } return 0; }
点赞 回复 分享
发布于 2018-08-30 21:18
第二题谁有什么例子吗?一直只能过20%,思路应该没问题的。
点赞 回复 分享
发布于 2018-08-30 21:22
有没有哪位记得题目的,当时没做出来,现在想学习各位大神的思路
点赞 回复 分享
发布于 2018-08-31 22:00

相关推荐

2024-12-29 19:48
河北科技大学 Java
没事就爱看简历:问题不在于简历:1、大学主修课程学那么多应用语言,作为计算机专业是很难理解的。 2、技能部分,每一个技能点的后半句话,说明对熟练,熟悉的标准有明显误会。 3、项目应该是校企合作的练习吧,这个项目你负责什么,取得了哪些成果都没有提及,只是列举了你认为有技术含量的点,而这些都有成熟的实现。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务