原题链接:蓝桥杯算法提高VIP-乘法运算
思路:

正确代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int grid[][]=new int[7][4];
int a=sc.nextInt();
int b=sc.nextInt();
for (int i = 0; i =10 && grid[1][2]==0) {
grid[i][2]=0;
}
continue;
}
if (i==4) {//第五行
if (grid[1][1]==-1) {
continue;
}
int mul=grid[1][1]*a;//十位与a相乘
grid[i]=right(mul, 1);
}
if (i==5) {//第六行 9473='-'
if (grid[i][1]==-1) {
continue;
}
grid[i][3]=-1;
grid[i][2]=9473;
grid[i][1]=9473;
grid[i][0]=9473;
}
if (i==6) {//第七行
if (grid[i][1]==-1) {
continue;
}
grid[i]=right(a*b, 0);
}
}
//输出
for (int i = 0; i <7; i++) {
if (i==4 && grid[1][1]==-1) {//输出到第五行时,如果b的十位没有数,则结束输出
break;
}
for (int j = 0; j =0; i--,m--) {
row[i]=Integer.parseInt(strN.charAt(m)+"");
}
return row;
}
}--------------------------------
题目说以竖式运算格式输出,题目中的例子格式明显不对。应该是这样的

我觉得下图右边的才对。

输出上图右边结果的代码如下:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int grid[][]=new int[7][4];
int a=sc.nextInt();
int b=sc.nextInt();
for (int i = 0; i =10 && grid[1][3]==0) {
grid[i][2]=0;
}
continue;
}
if (i==4) {//第五行
if (grid[1][2]==-1) {
continue;
}
int mul=grid[1][2]*a;//十位与a相乘
grid[i]=right(mul, 1);
}
if (i==5) {//第六行 9473='-'
if (grid[i][2]==-1) {
continue;
}
Arrays.fill(grid[i], 9473);
}
if (i==6) {//第七行
if (grid[i][2]==-1) {
continue;
}
grid[i]=right(a*b, 0);
}
}
//输出
for (int i = 0; i <7; i++) {
if (i==4 && grid[1][2]==-1) {//输出到第五行时,如果b的十位没有数,则结束输出
break;
}
for (int j = 0; j =0; i--,m--) {
row[i]=Integer.parseInt(strN.charAt(m)+"");
}
return row;
}
}还有一种更简便的方法。System.out.printf() 可以使输出右对齐,可以设置偏移的位数,十分强大。
import java.util.Scanner;
public class 测试 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.printf("%4d\n",a);
System.out.printf("%s%2d\n","×",b);
System.out.println("━━━");
if (a>=10 && b%10==0) {//第四行
System.out.printf("%4s\n","00");
}else {
System.out.printf("%4d\n",a*(b%10));
}
if (b>=10) {//第五行
System.out.printf("%3d\n",a*(b/10));
System.out.println("━━━");//第六行
System.out.printf("%4d\n",a*b);//第七行
}
}
}
0.0分
2 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复