Ⅰ 星號三角形(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') ;