本来是水题,但作为周赛的题目,还是写一下吧。
Description
Wangpeng is good at drawing. Now he wants to say numbers like “521” to his girlfriend through the game draw something.
Wangpeng can’t write the digit directly. So he comes up a way that drawing several squares and the total area of squares is the number he wants to say.
Input all the square Wangpeng draws, what’s the number in the picture?
Wangpeng can’t write the digit directly. So he comes up a way that drawing several squares and the total area of squares is the number he wants to say.
Input all the square Wangpeng draws, what’s the number in the picture?
Input
There are multiple test cases.
For each case, the first line contains one integer N(1≤N≤100) indicating the number of squares.
Second line contains N integers ai(1≤ai≤100)represent the side length of each square. No squares will overlap.
Input ends with N = 0.
For each case, the first line contains one integer N(1≤N≤100) indicating the number of squares.
Second line contains N integers ai(1≤ai≤100)represent the side length of each square. No squares will overlap.
Input ends with N = 0.
Output
For each case, output the total area in one line.
Sample Input
4 1 2 3 4 3 3 3 3 0
Sample Output
30 27
以下是代码:
#include <iostream>
#include<cstdio>
using namespace std;
#define maxn 100+10
long long a[maxn]={0};
int main()
{
int n,i,j,k;
cin>>n;
while(n!=0)
{
long long s=0;
for(i=0;i<n;i++)
{
cin>>a[i];
s+=a[i]*a[i];
}
cout<<s<<endl;
cin>>n;
}
return 0;
}
含义自明,就不解释了。用时八分钟,有点慢。
本文介绍了一种通过绘制不同大小的正方形来表达数字的游戏算法。玩家需输入所画正方形的数量及边长,算法将计算这些正方形总面积以得出目标数字。示例展示了如何使用C++实现这一算法。
339

被折叠的 条评论
为什么被折叠?



