暑期多校联合比赛F题 8_24 杨辉三角的打印
来源:互联网 发布:2017淘宝搜索规则变化 编辑:程序博客网 时间:2024/05/22 03:13
F、Pascal'sTriangle of DeathTime limit: 1.000 seconds
In this problem, you are asked to generate Pascal's Triangle.
Pascal's Triangle is useful in many areas from probability to
polynomials to programming contests. It is a triangle of integers
with ``1'' on top and down the sides. Any number in the interior
equals the sum of the two numbers above it. For example, here are
the first 5 rows of the triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
In ``Pascal's Triangle of Death,'' you are to generate a left
justified Pascal's Triangle. When any number in the triangle is
exceeds or equals 10^60, your program should finish printing the
current row and exit. The output should have each row of the
triangle on a separate line with one space between each element.
The final element of each line should be directly followed by a
newline. There is no space after the last number on each line.
Sample Input
There is no input for this problem.
Sample Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
.
.
.
etc.
题目大意:打印杨辉三角直到遇到一个等于或者大于10^60的数(当然还有接着打印完这一行),一开始被这大数吓倒了,尼玛可恶啊!
思路:其实我就卡在数学的杨辉三角没学好,没算到大约205层就可以到10^60这个数了,
还有并不知道三维数组竟然可以开到那么大,我勒个去啊,对不起队友羽哥还有杨烁啊 。
program:
#include <stdio.h>
#include <string.h>
#define MAX 300
char a[MAX][MAX][MAX];
void reverse(char *from, char *to);
void call_sum(char *first, char *second, char *result);
int main()
{
char res[MAX];
int flag = 1;
int count, i, len;
strcpy(a[0][0], "1");
strcpy(a[1][0], "1");
strcpy(a[1][1], "1");
printf("1\n1 1\n");
count = 2;
while (flag)
{
strcpy(a[count][0], "1");
strcpy(a[count][count], "1");
for (i = 1; i < count; i++)
{
call_sum(a[count - 1][i - 1], a[count - 1][i], res);
len = strlen(res);
strcpy(a[count][i], res);
if (len >= 61)
flag = 0;
}
for (i = 0; i <= count; i++)
{
printf("%s", a[count][i]);
if (i < count)
printf(" "); //打印该行
else
printf("\n");
}
count++;
}
return 0;
}
void reverse(char *from, char *to)
{
int len = strlen(from);
int i;
for (i = 0; i < len; i++) {
to[i] = from[len - 1 - i];
to[len] = '\0';
}
}
void call_sum(char *first, char *second, char *result)
{
char F[MAX], S[MAX], Res[MAX];
int f, s, sum, extra, now;
f = strlen(first);
s = strlen(second);
reverse(first, F);
reverse(second, S);
for (now = 0, extra = 0; now < f && now < s; now++)
{
sum = (F[now] - '0') + (S[now] - '0') + extra;
Res[now] = sum % 10 + '0';
extra = sum / 10;
}
for (; now < f; now++)
{
sum = F[now] + extra - '0';
Res[now] = sum % 10 + '0';
extra = sum / 10;
}
for (; now < s; now++)
{
sum = S[now] + extra -'0';
Res[now] = sum % 10 + '0';
extra = sum / 10;
}
if (extra)
Res[now++] = extra + '0'; //最高位多算一次
Res[now] = '\0';//去掉前导0
if (strlen(Res) == 0)
strcpy(Res, "0");
reverse(Res, result);
}
- 暑期多校联合比赛F题 8_24 杨辉三角的打印
- 暑期多校联合比赛 8-24 K次方
- 暑期多校联合第一题,hdu4861
- 2013暑期多校联合训练\第五场 Problem F Magic Pen 6
- 杨辉三角的打印
- 杨辉三角的打印
- 杨辉三角的打印
- HDU6038(多校联合F题)
- 暑期第一次多校联合赛 I题 数学题
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印-杨辉三角
- C++的引用类型详解
- iOS5中 UIViewController新方法的使用
- 全局变量在函数中改变
- 对线性链表的完整操作(1)
- 《Effective C++》读书笔记之item42:了解typename的双重意义
- 暑期多校联合比赛F题 8_24 杨辉三角的打印
- sgu491 Game for Little Johnny
- centos 安装 C#开发环境 mono
- centos和redhat6.0后关于虚拟机克隆后无法启用网卡问题
- 分布式系统编程,你到哪一级了?
- WINFORM 中获得目录
- pku 2104划分树
- java设计模式之责任链
- Win7 开启 局域网 匿名访问共享