Ⅰ 星号三角形(c语言)
优化了你的程序,你理解一下
#include <stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
//for(i=0;i<n-1;i++) 少一行
for(i=0;i<n;i++)
{
//for(j=0;j<2*n-2;j++) //少一行
for(j=0;j<2*n-1;j++)
{
//if((j>=2*n-2-2*i)&&(j<=2*n-2)) //少一列 j<=2*n-2没用,for循环就控制了J
if( j>2*n-2-2*i-1 )
{
if(j%2!=0)
{
printf(" ");
}
else
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
system("pause");
return 0;
}
Ⅱ 用C语言编写5行`杨辉三角~~急````
#include "stdlib.h"
//声明队列类型
typedef struct node
{
int data[21];
int head,rear;
}sequeue;
int num;//杨辉三角的层数
//队列的初始化
void initial(sequeue *sq)
{
sq->head=-1;
sq->rear=-1;
}
//进队操作
void ensequeue (sequeue *sq,int data1,int data2)
{
sq->rear++;
sq->data[sq->rear]=data1+data2;
}
//出队操作
int desequeue(sequeue *sq)
{
return sq->data[++sq->head];
}
//求扬辉三角的第n行,将其存入队列sq2指向的结点中
sequeue * fun(int n,sequeue *sq1)
{
int data1,data2=0,i;
sequeue *sq2;
sq2=(sequeue *)malloc(sizeof(sequeue));
initial(sq2);
//输出每行前的空格以形成三角格式
for (i=0;i<num-n;i++)
printf(" ");
//输出存储在队列中的某行元素,并计算下一行数
while(sq1->head!=sq1->rear)
{
data1=desequeue(sq1);
//处理两树之间的间隔距离
if (data1<10) printf(" %d",data1);
else if (data1<100) printf(" %d",data1);
else if (data1<1000) printf(" %d",data1);
else printf(" %d",data1);
ensequeue(sq2,data1,data2);
data2=data1;
}
sq2->data[++sq2->rear]=1;
printf("\n");
free(sq1);
return sq2;
}
int main(int argc,char *argv[])
{
int i;
sequeue *sq=(sequeue *)malloc (sizeof(sequeue));
initial(sq);
sq->rear++;
sq->data[sq->rear]=1; //将第一行存入队列中,第一行只有1一个元素
printf("please put in a integer number(no more then 15)!\n\t");
scanf("%d",&num);
//fun()函数实现输出一行元素并计算出下一行各个元素,经过num次调用fun函数输出num层杨辉三角
for (i=0;i<num;i++)
sq=fun(i,sq);
return 0;
}
Ⅲ 求大神,C语言,下面程序打印一个如下图所示的5行的下三角形,把程序补充完整:
main( )
{ int i;
for(i=1;___i<=5___; i++)
_____printstar(i)__________;
}
printstar(____int i____)
{int j,k;
char space=' ';
for(j=1;j<6-i;j++)
printf("%c", space);
for(k=1; ____k<=i+i-1____; k++)
printf("%c", '*');
____printf("\n")_______;
}
Ⅳ C语言编程求助!“使用循环语句打印出星号三角形”(如下图)
对于这种题目,可以看作是 * 之前有若干个空格。
关键点在于抓住每行的空格数及 * 数与本行的行号的关系。祝你好运!!
#include <stdio.h>
#include <iostream>
int main()
{
for (int i=1;i<=5;i++) //循环控制变量 i ,用于遍历每一行
{
for (int j=5;j>=i;j--) //循环控制变量 j ,用于控制输出当前行的空格的个数
printf(" ");
for (int m=1;m<=(4*i-2);m+=2) //循环控制变量 m ,用于控制输出当前行的 * 的个数
printf("*");
printf("\n"); //当本行输出空格及 * 完毕后,换行,开始下一行的循环
}
system("pause");
}
Ⅳ c语言程序设计-跳动的三角形
clear all
close all
%channel system order
sysorder = 5 ;
% Number of system points
N=2000;
inp = randn(N,1);
n = randn(N,1);
[b,a] = butter(2,0.25);
Gz = tf(b,a,-1);
%This function is submitted to make inverse Z-transform (Matlab central file exchange)
%The first sysorder weight value
%h=ldiv(b,a,sysorder)';
% if you use ldiv this will give h :filter weights to be
h= [0.0976;
0.2873;
0.3360;
0.2210;
0.0964;];
y = lsim(Gz,inp);
%add some noise
n = n * std(y)/(10*std(n));
d = y + n;
totallength=size(d,1);
%Take 60 points for training
N=60 ;
%begin of algorithm
w = zeros ( sysorder , 1 ) ;
for n = sysorder : N
u = inp(n:-1:n-sysorder+1) ;
y(n)= w' * u;
e(n) = d(n) - y(n) ;
% Start with big mu for speeding the convergence then slow down to reach the correct weights
if n < 20
mu=0.32;
else
mu=0.15;
end
w = w + mu * u * e(n) ;
end
%check of results
for n = N+1 : totallength
u = inp(n:-1:n-sysorder+1) ;
y(n) = w' * u ;
e(n) = d(n) - y(n) ;
end
hold on
plot(d)
plot(y,'r');
title('System output') ;
xlabel('Samples')
ylabel('True and estimated output')
figure
semilogy((abs(e))) ;
title('Error curve') ;
xlabel('Samples')
ylabel('Error value')
figure
plot(h, 'k+')
hold on
plot(w, 'r*')
legend('Actual weights','Estimated weights')
title('Comparison of the actual weights and the estimated weights') ;
axis([0 6 0.05 0.35])
% RLS 算法
randn('seed', 0) ;
rand('seed', 0) ;
NoOfData = 8000 ; % Set no of data points used for training
Order = 32 ; % Set the adaptive filter order
Lambda = 0.98 ; % Set the forgetting factor
Delta = 0.001 ; % R initialized to Delta*I
x = randn(NoOfData, 1) ;% Input assumed to be white
h = rand(Order, 1) ; % System picked randomly
d = filter(h, 1, x) ; % Generate output (desired signal)
% Initialize RLS
P = Delta * eye ( Order, Order ) ;
w = zeros ( Order, 1 ) ;
% RLS Adaptation
for n = Order : NoOfData ;
u = x(n:-1:n-Order+1) ;
pi_ = u' * P ;
k = Lambda + pi_ * u ;
K = pi_'/k;
e(n) = d(n) - w' * u ;
w = w + K * e(n) ;
PPrime = K * pi_ ;
P = ( P - PPrime ) / Lambda ;
w_err(n) = norm(h - w) ;
end ;
% Plot results
figure ;
plot(20*log10(abs(e))) ;
title('Learning Curve') ;
xlabel('Iteration Number') ;
ylabel('Output Estimation Error in dB') ;
figure ;
semilogy(w_err) ;
title('Weight Estimation Error') ;
xlabel('Iteration Number') ;
ylabel('Weight Error in dB') ;