Spreadsheet

我发誓以后一定要提前一天看题,今天这题是什么鬼……(来自一个计算机小白的疯狂吐槽)
直接上今天的题目吧,电子表格:
Problem Description

In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first
spreadsheet application. It became a huge success and, at that time, was the
killer application for the Apple II computers. Today, spreadsheets are found on
most desktop computers.

The idea behind spreadsheets is very simple, though powerful. A spreadsheet
consists of a table where each cell contains either a number or a formula. A
formula can compute an expression that depends on the values of other cells.
Text and graphics can be added for presentation purposes.

You are to write a very simple spreadsheet application. Your program should
accept several spreadsheets. Each cell of the spreadsheet contains either a
numeric value (integers only) or a formula, which only support sums. After having
computed the values of all formulas, your program should output the resulting
spreadsheet where all formulas have been replaced by their value.

1979年,Dan Bricklin和Bob Frankston编写了VisiCalc,这是第一个电子表格应用程序。它取得了巨大的成功,在当时,它是Apple II电脑的杀手级应用程序。如今,电子表格在大多数台式电脑上都能找到。

电子表格背后的想法非常简单,尽管很强大。电子表格由一个表格组成,其中每个单元格包含一个数字或公式。公式可以计算依赖于其他单元格值的表达式。可以添加文本和图形以进行演示。

您将编写一个非常简单的电子表格应用程序。你的程序应该接受几个电子表格。电子表格的每个单元格都包含一个数值(仅限整数)或一个公式(仅支持和)。计算完所有公式的值后,程序应输出结果电子表格,其中所有公式已被其值替换。
在这里插入图片描述
Figure:Naming of the top left cells图:左上角单元格的命名

Input

The first line of the input file contains the number of
spreadsheets to follow. A spreadsheet starts with a line consisting of two
integer numbers, separated by a space, giving the number of columns and rows.
The following lines of the spreadsheet each contain a row. A row consists of
the cells of that row, separated by a single space. A cell consists either of a
numeric integer value or of a formula. A formula starts with an equal sign (=).
After that, one or more cell names follow, separated by plus signs (+). The
value of such a formula is the sum of all values found in the referenced cells.
These cells may again contain a formula. There are no spaces within a formula.
You may safely assume that there are no cyclic dependencies between cells. So
each spreadsheet can be fully computed. The name of a cell consists of one to
three letters for the column followed by a number between 1 and 999 (including)
for the row. The letters for the column form the following series: A, B, C,
…, Z, AA, AB, AC, …, AZ, BA, …, BZ, CA, …, ZZ, AAA, AAB, …, AAZ, ABA,
…, ABZ, ACA, …, ZZZ. These letters correspond to the number from 1 to
18278. The top left cell has the name A1. See figure 1.

输入文件的第一行包含要遵循的电子表格数。电子表格以两个整数组成的行开始,用空格隔开,给出列和行的数目。电子表格的以下行各包含一行。一行由该行的单元格组成,由一个空格分隔。单元格由数字整数值或公式组成。公式以等号(=)开头。之后,一个或多个单元格名称将跟随,并用加号(+)分隔。此公式的值是在引用单元格中找到的所有值的总和。这些单元格可能再次包含公式。公式中没有空格。您可以安全地假设单元之间没有循环依赖关系。所以每个电子表格都可以完全计算出来。单元格的名称由列的1到3个字母和行的1到999(包括)之间的数字组成。该列的字母组成以下系列:A、B、C、…、Z、AA、AB、AC、…、AZ、BA、…、BZ、CA、…、ZZ、AAA、AAB、…、AAZ、ABA、…、ABZ、ACA、…、ZZZ。这些字母对应于从1到18278的数字。左上角的单元格名为A1。见图。

Output

The output of your program should have the same format as the
input, except that the number of spreadsheets and the number of columns and
rows are not repeated. Furthermore, all formulas should be replaced by their
value.

程序的输出格式应与输入格式相同,只是不重复电子表格的数目以及列和行的数目。此外,所有公式都应替换为其值。

Sample Input

1

4 3

10 34 37 =A1+B1+C1
40 17 34 =A2+B2+C2
=A1+A2 =B1+B2 =C1+C2 =D1+D2

Sample Output

10 34 37 81
40 17 34 91
50 51 71 172

(个人解释:就是把上面的公式计算了一下,数字正常输出,对应标记见图,在上面。)
解析:做法可以考虑递归,具体可以参照这位兄弟的代码

链接: link.

我拿DFS做的,今天做完头都大了一圈:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
bool has[100][100];
int map[100][100],n,m;
char mapstr[100][100][100];
int GetNum(char *str)
{
    int ans=0;
    for(int i=0;i<strlen(str);i++)
 ans=ans*10+str[i]-'0';
    return ans;
}
int GetY(char *str)
{
    if(strlen(str)==1)
 return str[0]-'A'+1;
    if(strlen(str)==2)
 return (str[0]-'A'+1)*26+str[1]-'A'+1;
    if(strlen(str)==3)
 return (str[0]-'A'+1)*676+(str[1]-'A'+1)*26+str[2]-'A'+1;
}
int DFS(int x,int y)
{
    if(has[x][y])
 return map[x][y];
    int ans=0,itx=0,ity=0,len=strlen(mapstr[x][y]);
    for(int i=1;i<len;i++)
    {
 char ita[100],cou=0;
 itx=ity=0;
 while(mapstr[x][y][i]>='A')
 {
    ita[cou]=mapstr[x][y][i];
     cou++;
     i++;
 }
 ita[cou]='\0';
 ity=GetY(ita);
 while(mapstr[x][y][i]!='+'&&i<len)
 {
     itx=itx*10+mapstr[x][y][i]-'0';
     cou++;
     i++;
 }
 ans+=DFS(itx,ity);
    }
    return ans;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
 memset(has,0,sizeof(has));
 scanf("%d%d",&n,&m);
 for(int i=1;i<=m;i++)
     for(int j=1;j<=n;j++)
     {
  char str[50];
  scanf("%s",str);
  if(str[0]!='=')
  {
      map[i][j]=GetNum(str);
      has[i][j]=1;
  }
  else
      strcpy(mapstr[i][j],str);
     }
 for(int i=1;i<=m;i++)
     for(int j=1;j<=n;j++)
     {
  if(has[i][j])
      continue;
  map[i][j]=DFS(i,j);
  has[i][j]=1;
     }
 for(int i=1;i<=m;i++)
     for(int j=1;j<=n;j++)
     {
  if(j!=n)
      printf("%d ",map[i][j]);
  else
      printf("%d\n",map[i][j]);
     }
    }
    return 0;
}
大致的调试了几次,最后觉得是最简单的做法了,感谢老爸教导,不然今天真的想放弃了,代码只是表示开100*100不会RE
不敢想象学长和我说的那个十维矩阵,太可怕了,最少,目前的我应付不来。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值