PAT 1074 宇宙无敌加法器

原题链接:

     https://www.patest.cn/contests/pat-b-practise/1074

     最开始使用的数据结构是数组,只得了15分。

     然后参考网上代码,说是能使用数据结构堆栈来进行编程,所以就使用了c++中的stack来进行编程,在这个过程中也出现了错误,答案虽然正确,但老是提示段错误

    后来发现在我把输入数据的函数从scanf换做是cin.get()时,就出现了满分

    20分代码:

//指针对象访问结构体内部成员就用->
#include 
using namespace std;
/*void input(stack &s1){
	char a;
	while(scanf("%c"&a)!=EOF
	 s1.push(a-'0');
} */
int main(){
	stack s_jz,s_num1,s_num2,res; 
	char a;
	int jw=0,s;
	while((a=cin.get())!='\n')
	 s_jz.push(a-'0');
	 	while((a=cin.get())!='\n')
	 s_num1.push(a-'0');
	 	while((a=cin.get())!='\n')
	 s_num2.push(a-'0');
	 while(!s_num1.empty() && !s_num2.empty()){
	 	  s=s_num1.top() + s_num2.top() +jw;
	 	  s_num1.pop();
	 	  s_num2.pop();
	 	  int d=s_jz.top();
	 	  if(!d) d+=10;
	 	  res.push(s%d);
	 	  jw=s/d;
	 	  /*if(s_jz.top() == 0){
	  	    res.push(s%10);
	        jw=s/10;
	       }
	      else{
	    	res.push(s%s_jz.top());
	        jw=s/s_jz.top();
	      }*/
	      s_jz.pop();
	 }
	 while(!s_num1.empty()){
	 	  s=s_num1.top()  +jw;
	 	  s_num1.pop();
	 	  int d=s_jz.top();
	 	  if(!d) d+=10;
	 	  res.push(s%d);
	 	  jw=s/d;
	      s_jz.pop();
	 }
	 while(!s_num2.empty()){
	 	  s=s_num2.top()  +jw;
	 	  s_num2.pop();
	 	  int d=s_jz.top();
	 	  if(!d) d+=10;
	 	  res.push(s%d);
	 	  jw=s/d;
	      s_jz.pop();
	 }
	/* while(!s_jz.empty()){
	 	if(!s_num1.empty() && !s_num2.empty()){
	 	  s=s_num1.top() + s_num2.top() +jw;
	 	  s_num1.pop();
	 	  s_num2.pop();
	    }
	 	else if(!s_num1.empty()){
	 	  s=s_num1.top()  +jw;
	 	  s_num1.pop();
	 }
	 	else{
	 	  s=s_num2.top()  +jw;
	 	  s_num2.pop();
	 }
	  if(s_jz.top() == 0){
	  	 res.push(s%10);
	     jw=s/10;
	  }
	 else{
	 	res.push(s%s_jz.top());
	    jw=s/s_jz.top();
	 }
	 
	  s_jz.pop();
	 }*/
	 res.push(jw);
	while(res.size()>1&&res.top()==0)
        res.pop();
	 while(!res.empty()){
	 	cout<

来之不易的20分

  PAT 1074 宇宙无敌加法器_第1张图片

  15分代码:

 

//首次:15/20 
#include 
using namespace std;
char jinzhi[25]={0},num1[25]={0},num2[25]={0};
int sum1[25]={0},sum2[25]={0},res[25]={0},jz[25]={0};
void exchange(){
	int i,j=0;
	for(i=strlen(num1)-1;i>=0;i--){
	  sum1[j++]=num1[i]-'0';
	  //cout<=0;i--)
	  sum2[j++]=num2[i]-'0';
    for(i=strlen(jinzhi)-1,j=0;i>=0;i--)
	  jz[j++]=jinzhi[i]-'0';
}
void print(int a[]){
	for(int i=0;i>jinzhi>>num1>>num2;
	exchange();
	int i,k=0,jinwei=0;
    // cout<=0;i--)
    cout<

PAT 1074 宇宙无敌加法器_第2张图片

老是出现段错误的代码:

//指针对象访问结构体内部成员就用->
#include 
using namespace std;
int main(){
	stack s_jz,s_num1,s_num2;
	stack res; 
	char a;
	int jw=0,s;
	while(scanf("%c",&a)!=EOF)
	 s_jz.push(a-'0');
	 while(scanf("%c",&a)!=EOF)
	 s_num1.push(a-'0');
	 while(scanf("%c",&a)!=EOF)
	 s_num2.push(a-'0');
	 while(!s_jz.empty()){
	 	if(!s_num1.empty() && !s_num2.empty()){
	 	  s=s_num1.top() + s_num2.top() +jw;
	 	  s_num1.pop();
	 	  s_num2.pop();
	    }
	 	else if(!s_num1.empty()){
	 	  s=s_num1.top()  +jw;
	 	  s_num1.pop();
	 }
	 	else{
	 	  s=s_num2.top()  +jw;
	 	  s_num2.pop();
	 }
	  res.push(s%s_jz.top());
	  jw=s/s_jz.top();
	  s_jz.pop();
	 }
	 while(!res.empty()){
	 	cout<

PAT 1074 宇宙无敌加法器_第3张图片



你可能感兴趣的:(PAT 1074 宇宙无敌加法器)