9, the general approach to find the law of digital graphics

9, the general approach to find the law of digital graphics

(General analytical method that is listed and summarized around between several adjacent, neighboring law exists between the upper and lower number and ranks of these laws and the subject of the relationship between the specific analysis see code comments.)

Example a:

Graphical output:
Here Insert Picture Description

#include<stdio.h>
int main(){
	int i, j, r, c=1, g;
	//i=1表示第一行,j=1表示第一列,以此类推。
	//row记录每行中的数,column记录本行起始的数,gap记录行相同列相邻的两个数的差值
	
	for(i=1; i<=6; i++){  
		r = c;					//1、第一列的数同时是每一行的起始数
		g = 1+i;				//2、第一行相邻数的差值从2开始;第二行的差值从3开始...故第i行的差值从1+i开始 
		for(j=1; j<=7-i; j++){	//3、第一行有7-1=6个数,即六列;第二行有7-2=5个数,即五列... 故第i行有7-i个数
			printf("%3d", r);
			r = r+g;
			g++;				//4、左右相邻的两数差值为1
		}
		printf("\n");
		
		c = c+i;				//5、第一列中,上下相邻的两数的差值从1到2到3...故总结可得:本行行序数加上本行的起始数,即为下一行的起始数
	}
	return 0;
} 

Two examples:

Output pattern:
Here Insert Picture Description
Requirements : output data line 9, not simply by printf statement 5, the use of two algorithms (1) array method (2) do not only circular array to achieve

/*
	(1)、循环: 
#include<stdio.h>
int main(){
	int k=0, i, j;
	for(i=1; i<=9; i++){
		for(j=9-i; j>0; j--){
			printf("   ");
		}
		for(j=i; j>0; j--){
			printf("%3d", ++k);
		}
		printf("\n");
	}
	
	return 0;
}
	(2)、数组:
*/
#include<stdio.h>
int main(){
	int n, m;
	scanf("%d", &n);		
	m=n*(n+1)/2;			//实现用户自定义行数,并用等差数列求和公式得数组容量,即共m个数
	 
	int a[m];
	for(i=0; i<m; i++){
		a[i]=i+1;	
	}
	 
	int i, j, k=0;
	for(i=0; i<n; i++){
		for(j=n-1-i; j>0; j--){
			printf("   ");
		} 
		//a[i]=i+1;			//赋值语句不能糅合到此处。比如第1行输出2 3时,只有a[1]赋得了2,a[2]并未赋值 
		j=i+1;				//第0行输出1个数,第1行输出2个数...用j记录这一行要输出几个数 
		while(j>0){
			printf("%3d", a[k++]); 
			j--; 			//因为i记录着第几行,不便修改,故不用i作下标,用另一个变量k
		}
		printf("\n");
	}
	return 0;
} 

Three examples:

Title: Output monthly calendar.
Requirements: Write a program to display the monthly calendar, the user enters the total number of days and the month is May 1 this week, such as:
Input:
313
Output:
Here Insert Picture Description

#include<stdio.h>
int main(){
	int i, n, m;			//n用来存储总天数,m用来存储星期几 
	printf("输入这个月总的天数和该月1号是星期几:");
	scanf("%d %d", &n, &m);

	for(i=1; i<m; i++){
		printf("    ");		//1号是星期x,则先输出x-1个空位
	}
	for(i=1; i<=n; i++){
		printf("%-4d", i);
		if(m%7 == 0){		//当m能被7整除时,说明本周已输出完,换行输出下一周 
			printf("\n");
		}
		m++;
	}
	return 0;
} 

Published 16 original articles · won praise 0 · Views 324

Guess you like

Origin blog.csdn.net/NAU_LHT/article/details/104169120