T222171 [NOIP1996 普及组] 乘法运算 题解

Link\text{Link}

此题本来是直接复制网上题面和代码的,不过出现了多种问题,历经波折,修改了很多次,最后终于完美了,难度得到了加强,为同学们做题时的不便感到抱歉,也恭喜全场唯一一个 AC @wumingwei !!!

分析

直接模拟即可,具体格式到这里去探索。

如果你 WA 了,不妨试试下面几组数据:

1 1
3 7
1 10
11 10
10 2
10 12
10 10
11 11
20 70
20 30
98 35
4 12
12 4

代码

标准答案

如果看不懂函数就把函数部分换成函数内容。

#include<bits/stdc++.h>
using namespace std;
void print(int v,int w){
	if(w==4) printf("%4d\n",v);
	if(w==3) printf("%3d\n",v);
	if(w==2) printf("%2d\n",v);
}
int main(){
	int t,a,b;
	cin>>a>>b;
	t=(!(a%10))+(!(b%10));
	print(a,4-(t-(!(a%10))));
	putchar('*');
	print(b,3-(t-(!(b%10))));
	puts("----");
	if(b>9&&(b%10)){
		print(b%10*a/(a%10?1:10),4-(t-(!(b%10))));
		print(b/10*a/(a%10?1:10),3-(t-(!(b%10))));
		puts("----");
	}
	print(a*b,4);
	return 0;
}

@wumingwei 答案

这个你们应该看得懂。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,y;
	cin>>x>>y;
	if(y/10!=0)
	{
		if(y%10!=0&&x%10!=0) 
		{
		printf("%4d\n*%3d\n----\n%4d\n%3d \n----\n%4d",x,y,x*(y%10),x*(y/10),x*y);	
		}
		else if(y%10==0&&x%10!=0)
		{
		printf("%3d \n*%3d \n----\n%4d",x,y,x*y);	
		}
		else if(y%10!=0&&x%10==0)
		{
			printf("%4d\n*%2d \n----\n%3d\n%2d  \n----\n%4d",x,y,(x/10)*(y%10),(x/10)*(y/10),x*y);	
		}
		else
		{
			printf("%3d \n*%2d \n----\n%4d",x,y,x*y);	
		}
	 } 
	 else
	 {
	 	
	
		if(x%10==0)
		{
			printf("%4d\n*%2d \n----\n%4d",x,y,x*y);	
		}
		else
		{
			printf("%4d \n*%3d \n----\n%4d",x,y,x*y);	
		}
	 }
	return 0;
}
posted @ 2022-01-25 00:48  luckydrawbox  阅读(18)  评论(0)    收藏  举报  来源