又一算法题,简单又复杂,导师有特殊要求,会的教下
论文问答
1
题目是在console输出如下图所示数字排列,我导师要求主体代码不能超过5行,又把我难倒了,各位码友专家看能不能教一下,java、c#、c、vb都行 不会的也可以留言,我们一起学习共同进步
-
超过5行? 那只能这样:
string[] strs = new string[] {" 1"," 121"," 12321"," 1234321","123454321", " 1234321", " 12321" , " 121" , " 1" }; foreach (var item in strs) Console.WriteLine(item);
long item = 0, len = 9;//len只到5的话,定义为int就可以了 for (int i = 0; i < len * 2 - 1; i++) { item = i < len ? item * 10 + long.Parse("1".PadLeft(i * 2 + 1, '1')) : (item - long.Parse("1".PadLeft(((int)len * 2 - i - 1) * 2 + 1, '1'))) / 10; Console.WriteLine(" ".PadLeft((int)len - (item.ToString().Length) / 2, ' ') + item); }
-
#include<stdio.h> int main() { int i,j,l,m,pos,n; char k; scanf("%d",&n); for(pos=l=0,m=n-1; m>=0; ++l<n?pos=l:pos=--m) for(i=pos,j=0;j<=2*pos+1;i<n?i++:j++) printf("%c",i<n?' ':(j<=pos)?(k=j+1+'0'):((--k)!='0'?k:'\n')); return 0; }
-
#include <stdio.h> #include <stdlib.h> int main() { int i,j,k,n; scanf("%d",&n); for (k=j=1,i=1-n;j<2*n; i<=n-1 ? i++:(i=1-n,++j<=n?k++:k--, putchar('\n'))) printf(k > abs(i) ? "%-2d " : " ",k-abs(i)); return 0; }
-
pow((pow(10,x-n)-1 )/9 ,2)); //n=4 ,1 //n=3 ,99/9=11, 11*11 = 121 //n=2 ,999/9=111,111*111 = 12321 //n=1 ,9999/9=1111,1111*1111 = 1234321
-
for (int i = 1, x = 5, n = x - 1; i < x * 2; i++, n = Math.Abs(i - x)) { Console.WriteLine(new string(' ', n) + Math.Pow((Math.Pow(10, x - n) - 1) / 9, 2)); }
发表回复