1412200906-hd-矩形A+B

本文详细解析了矩形A+B问题,介绍了如何通过双重for循环计算特定网格中矩形的数量,并提供了完整的C语言实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

矩形A + B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4511    Accepted Submission(s): 3491


Problem Description
给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格.
 

Input
第一行输入一个t, 表示有t组数据,然后每行输入n,m,分别表示网格的高和宽 ( n < 100 , m < 100).
 

Output
每行输出网格中有多少个矩形.
 

Sample Input
      
2
1 2
2 4
 

Sample Output
      
3
30
解题思路
我没想到直接用公式sum=n*(n+1)*m*(m+1)/4。我用了笨方法,双重for循环,
第一层循环是新矩形的宽,第二层循环是新矩形的长。
代码
#include<stdio.h>
int main()
{
	int n;
	int a,b;
	int sum;
	int i,j,k,x;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&a,&b);
		if(a<b)
		{
			x=a;
			a=b;
			b=x;
		}
		sum=0;
		for(i=1;i<=b;i++)//新矩形列有i个 
		    for(j=1;j<=a;j++)//新矩形横有j个 
		        sum+=(b-i+1)*(a-j+1);
		//或者直接用公式sum=a*(a+1)*b*(b+1)/4;
		printf("%d\n",sum);
	}
	return 0;
} 


 
Build target 'Target 1' compiling main.c... main.c(21): error: #20: identifier "RCC_CFGR_SWS" is undefined while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); main.c: 0 warnings, 1 error compiling dht11.c... dht11.c(42): error: #268: declaration may not appear after executable statement in block uint32_t count = 0; dht11.c: 0 warnings, 1 error compiling tft_gfx.c... tft_gfx.c(14): warning: #61-D: integer operation result is out of range GPIOA->CRL &= ~(0xFFF << 20); // ??PA5, PA6, PA7?? tft_gfx.c(88): error: #254: type name is not allowed for (uint32_t i = 0; i < 320 * 240; i++) { tft_gfx.c(88): error: #65: expected a ";" for (uint32_t i = 0; i < 320 * 240; i++) { tft_gfx.c(88): error: #20: identifier "i" is undefined for (uint32_t i = 0; i < 320 * 240; i++) { tft_gfx.c(103): warning: #223-D: function "TFT_DrawChar" declared implicitly TFT_DrawChar(x, y, *str, font, color); tft_gfx.c(100): warning: #177-D: variable "char_height" was declared but never referenced uint8_t char_height = font->height; tft_gfx.c(110): error: #159: declaration is incompatible with previous "TFT_DrawChar" (declared at line 103) void TFT_DrawChar(uint16_t x, uint16_t y, char c, FontDef *font, uint16_t color) { tft_gfx.c(114): error: #254: type name is not allowed for (uint8_t row = 0; row < font->height; row++) { tft_gfx.c(114): error: #65: expected a ";" for (uint8_t row = 0; row < font->height; row++) { tft_gfx.c(114): error: #20: identifier "row" is undefined for (uint8_t row = 0; row < font->height; row++) { tft_gfx.c(116): error: #254: type name is not allowed for (uint8_t col = 0; col < font->width; col++) { tft_gfx.c(116): error: #65: expected a ";" for (uint8_t col = 0; col < font->width; col++) { tft_gfx.c(116): error: #20: identifier "col" is undefined for (uint8_t col = 0; col < font->width; col++) { tft_gfx.c(118): warning: #223-D: function "TFT_DrawPixel" declared implicitly TFT_DrawPixel(x + col, y + row, color); tft_gfx.c(125): error: #159: declaration is incompatible with previous "TFT_DrawPixel" (declared at line 118) void TFT_DrawPixel(uint16_t x, uint16_t y, uint16_t color) { tft_gfx.c(138): error: #254: type name is not allowed for (uint32_t i = 0; i < w * h; i++) { tft_gfx.c(138): error: #65: expected a ";" for (uint32_t i = 0; i < w * h; i++) { tft_gfx.c(138): error: #20: identifier "i" is undefined for (uint32_t i = 0; i < w * h; i++) { tft_gfx.c: 4 warnings, 14 errors compiling fonts.c... ".\Objects\01.axf" - 16 Error(s), 4 Warning(s). Target not created. Build Time Elapsed: 00:00:02
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值