为打印数的那个题通过不了啊,求解

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] str = { "66666", "6...6", "....6", "6....", "6...6" };
        int[][] num = { { 0, 1, 1, 1, 0 }, { 2, 2, 2, 2, 2 },
                { 0, 2, 0, 3, 0 }, { 0, 2, 0, 2, 0 }, { 4, 4, 0, 2, 2 },
                { 0, 3, 0, 2, 0 }, { 0, 3, 0, 1, 0 }, { 0, 2, 2, 2, 2 },
                { 0, 1, 0, 1, 0 }, { 0, 1, 0, 2, 0 } };
        int n = scanner.nextInt();
        String[] strings = new String[n];
        for (int i = 0; i < n; i++) {
            strings[i] = scanner.next();
        }
        for (int i = 0; i < n; i++) {
            String exp = strings[i];
            int num1 = 0;
            int num2 = 0;
            char sample = 0;
            for (int j = 0; j < exp.length(); j++) {
                if (exp.charAt(j) == '*' || exp.charAt(j) == '-'
                        || exp.charAt(j) == '+') {
                    num1 = Integer.valueOf(exp.substring(0, j));
                    num2 = Integer.valueOf(exp.substring(j + 1));
                    sample = exp.charAt(j);
                    break;
                }
            }
            int result = 0;
            if (sample == '-')
                result = num1 - num2;
            if (sample == '+')
                result = num1 + num2;
            if (sample == '*')
                result = num1 * num2;
            char[] charArray = String.valueOf(result).toCharArray();
            for (int j = 0; j < 5; j++) {// 层数
                for (int k = 0; k < charArray.length - 1; k++) {// 表示每一位的数字
                    String string = str[num[charArray[k] - '0'][j]]+"..";
                    System.out.print(string);
                }
                System.out
                        .println(str[num[charArray[charArray.length - 1] - '0'][j]]);
            }
        }
        scanner.close();
    }
}


求求大佬解答额。
#笔试题目##面经#
全部评论
我也是!tm输出一摸一样。。就是不给过
点赞 回复 分享
发布于 2018-03-24 21:06
没看懂你的代码,是不是忘了66,666这种情况,我一开始也是
点赞 回复 分享
发布于 2018-03-24 21:14
#include <bits/stdc++.h> using namespace std; string ss[10][5]; string s; //long long int ans; long long int st[105]; char opst[105]; int ans[105]; int anspos; void solve(string s) {     long long int now = 0;     int pos = 0;     int pos2 = 0;     for (int i = 0; i <s.length(); ++i) {         if (s[i] == '6') {             now = now * 10 + 6;         } else {             st[pos ++] = now;             now = 0;             if (pos2 > 0 && opst[pos2 - 1] == '*') {                 st[pos - 2] *= st[pos - 1];                 pos --;                 pos2 --;             }             opst[pos2 ++] = s[i];         }     }     st[pos ++] = now;     if (pos2 > 0 && opst[pos2 - 1] == '*') {         st[pos - 2] *= st[pos - 1];         pos --;         pos2 --;     }     long long rst = 0;     int ppp = 0;     for(int i = 0; i <pos - 1; ++i) {         if (opst[ppp ++] == '-') {             st[i+1] = st[i] - st[i+1];         } else {             st[i+1] = st[i] + st[i+1];         }     }     rst = st[pos - 1];     //cout<<"rst=="<<rst<<endl;     anspos = 0;     if (rst == 0) {         ans[anspos++] = 0;     }     while(rst > 0) {         ans[anspos ++] = rst % 10;         rst /= 10;     }     for (int k = 0; k < 5; ++k) {         for (int i = anspos - 1; i >= 0; --i) {             if (i != anspos - 1) {                 cout << "..";             }             cout << ss[ans[i]][k];         }         cout << endl;     } } int main() {     std::ios::sync_with_stdio(false);     //freopen("input.txt", "r", stdin);     ss[0][0] = "66666";     ss[0][1] = "6...6";     ss[0][2] = "6...6";     ss[0][3] = "6...6";     ss[0][4] = "66666";     ss[1][0] = "....6";     ss[1][1] = "....6";     ss[1][2] = "....6";     ss[1][3] = "....6";     ss[1][4] = "....6";     ss[2][0] = "66666";     ss[2][1] = "....6";     ss[2][2] = "66666";     ss[2][3] = "6....";     ss[2][4] = "66666";     ss[3][0] = "66666";     ss[3][1] = "....6";     ss[3][2] = "66666";     ss[3][3] = "....6";     ss[3][4] = "66666";     ss[4][0] = "6...6";     ss[4][1] = "6...6";     ss[4][2] = "66666";     ss[4][3] = "....6";     ss[4][4] = "....6";     ss[5][0] = "66666";     ss[5][1] = "6....";     ss[5][2] = "66666";     ss[5][3] = "....6";     ss[5][4] = "66666";     ss[6][0] = "66666";     ss[6][1] = "6....";     ss[6][2] = "66666";     ss[6][3] = "6...6";     ss[6][4] = "66666";     ss[7][0] = "66666";     ss[7][1] = "....6";     ss[7][2] = "....6";     ss[7][3] = "....6";     ss[7][4] = "....6";     ss[8][0] = "66666";     ss[8][1] = "6...6";     ss[8][2] = "66666";     ss[8][3] = "6...6";     ss[8][4] = "66666";     ss[9][0] = "66666";     ss[9][1] = "6...6";     ss[9][2] = "66666";     ss[9][3] = "....6";     ss[9][4] = "66666";     int n;     while(cin>>n) {         while(n --) {             cin >> s;             solve(s);         }     }     return 0; } 我的代码,调了一个多小时。。
点赞 回复 分享
发布于 2018-03-24 21:10
int太小,它的result范围是2^63-1
点赞 回复 分享
发布于 2018-03-24 21:12
这个题目只有一个符号的吗?
点赞 回复 分享
发布于 2018-03-24 21:14

相关推荐

点赞 评论 收藏
分享
mq2:我倒是觉得这种敞亮一点好。能接受就去不能就不去呗。 完了跟现在“正常”公司一样,hr说的天花乱坠,进去一看根本就是996核动力牛马,想走又没应届生身份了。岂不是更糟。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务