zl程序教程

您现在的位置是:首页 >  其它

当前栏目

PAT 1010的启发

PAT 启发
2023-09-14 09:13:21 时间

1.先粘上提交可通过的代码

#include <stdio.h>
#define maxSize 1000

int main(){
	int result[maxSize][2];//存放求出的结果
	 
	int a,b;//代表输入的系数与指数 	
	int i = 0;
	int count = 0;
	while(scanf("%d %d",&a,&b)!=EOF){		
		if(b!=0){//如果指数项不等于0 
			result[i][0] = a*b;
			result[i][1] = b-1;					
			i++;//项数增加			
			count ++;//表示正常的多项式数目 
		}
	}
	int j = 0;
	if(count == 0){
		printf("0 0\n");
		return 0;
	}
	while(j < i){
		if(count > 1){
			printf("%d %d ",result[j][0],result[j][1]);			
			count --;
		}
		else{
			printf("%d %d",result[j][0],result[j][1]);
			count --;
		}		
		j++; 	
	}
}
//3 4 -5 2 6 1 0 0 
//-2 0 3 4 -5 2 6 1
2.再来谈谈启发

#include <stdio.h>
#define maxSize 1000

int main(){
	int result[maxSize][2];//存放求出的结果
	 
	int a,b;//代表输入的系数与指数 	
	int i = 0;
	int count = 0;
	while(scanf("%d %d",&a,&b)!=EOF){		
		if(b!=0){//如果指数项不等于0 
			result[i][0] = a*b;
			result[i][1] = b-1;					
			i++;//项数增加			
			count ++;//表示正常的多项式数目 
		}
	}
	int j = 0;
	if(count == 0){
		printf("0 0\n");
		return 0;
	}
	while(j < i){
		if(count > 1){
			printf("%d %d ",result[j][0],result[j++][1]);			
			count --;
		}
		else{
			printf("%d %d",result[j][0],result[j++][1]);
			count --;
		}		
			
	}
}
//3 4 -5 2 6 1 0 0 
//-2 0 3 4 -5 2 6 1
跑一下程序可以知道程序2是无法得到正确结果的,为什么呢?因为这和C语言中的栈有关,相关一部分知识,明天这时候我来详谈。