| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
拜求大神,1001题,本地用sample input测试正确,为何不能AC?麻烦指教。。。大家好!我是刚入门的菜鸟,1001题我用Sample Input中的数据测试是正确的,为何submit却是Wrong Answer?麻烦各位大神了。。。 cauxiaochangyi@gmail.com
下面是源码(C语言):
//北大ACM1001题
#include<stdio.h>
#include<stdlib.h>
//错误处理函数
void Error(char*s){
printf("%s",s);
exit(1);
}//Error
//数据结构定义,用来存储运算整数、中间结果、最后结果;每位存储一个数字
struct LNode{
int data;
struct LNode *next;
};
typedef struct LNode* LinkList;
//初始化单链表,建立单链表的头结点
void InitList(LinkList &L){
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
}//InitList
//清空函数
//将单链表L清空,只剩下表头结点
void ClearList(LinkList &L){
LNode *q;
while(L->next){
q=L->next;
L->next=L->next->next;
free(q);
}
}//ClearList
//销毁单链表
void DestroyList(LinkList &L){
LNode *q;
while(L){
q=L->next;
free(L);
L=q;
}
}//DestroyList
//赋值函数
//将一个单链表L,拷贝到另一个链表(RL)中
void CopyList(LinkList &L,LinkList &RL){
LNode *q,*p,*pri;
q=L->next;
p=RL->next;
pri=RL;
while(q){
p=(LNode*)malloc(sizeof(LNode));
p->data=q->data;
pri->next=p;
p->next=NULL;
q=q->next;
pri=p;
}
}//CopyList
//链表相加函数
//将单链表(RTL)加上左移一位的单链表(TL),结果存在RTL中返回
void AddList(LinkList &RTL,LinkList &TL){
int c,temp;
int flg;
flg=0;
LNode *rq,*q,*pri;
pri=RTL;
rq=RTL->next;
q=TL->next;
c=0;temp=0;
while(q){
if(!rq){
rq=(LNode*)malloc(sizeof(LNode));
rq->next=NULL;
rq->data=0;
pri->next=rq;
}
temp=rq->data+q->data+c;
if(temp>=10){
c=temp/10;
rq->data=temp-c*10;
}
else{
c=0;
rq->data=temp;
}
pri=pri->next;
q=q->next;
rq=rq->next;
}
if(c!=0){
if(!rq){
rq=(LNode*)malloc(sizeof(LNode));
rq->next=NULL;
rq->data=0;
pri->next=rq;
}
rq->data=rq->data+c;
}
}//AddList
//子计算函数1
//计算单链表(RL)和整数(val)相乘,得到的结果放在单链表(TL)中
void ComputeOne(LinkList &RL,int val,LinkList &TL){
LNode *q,*p,*pri;
int c,temp;
c=0;temp=0;
q=RL->next;
pri=TL;
while(q){
temp=val*q->data;
temp=temp+c;
if(temp<10){
p=(LNode*)malloc(sizeof(LNode));
p->data=temp;
c=0;
p->next=NULL;
pri->next=p;
pri=pri->next;
}
else {
p=(LNode*)malloc(sizeof(LNode));
p->data=temp%10;
c=temp/10;
p->next=NULL;
pri->next=p;
pri=pri->next;
}
p=p->next;
q=q->next;
}
if(c!=0){
p=(LNode*)malloc(sizeof(LNode));
p->data=c;
p->next=NULL;
pri->next=p;
pri=pri->next;
}
}//ComputeOne
//子计算函数2
//将两个单链表(RL,L)相乘,结果用RL返回
void ComputeTwo(LinkList &RL,LinkList &L){
LinkList TL,RTL;
InitList(TL);
InitList(RTL);
RTL->next=(LNode*)malloc(sizeof(LNode));
RTL->next->next=NULL;
RTL->next->data=0;
int val;
LNode *q,*pri;
pri=RTL;
q=L->next;
while(q){
val=q->data;
ComputeOne(RL,val,TL);
AddList(pri,TL);
pri=pri->next;
q=q->next;
}
ClearList(RL);
CopyList(RTL,RL);
DestroyList(RTL); //销毁临时链表
DestroyList(TL);
}//ComputeTwo
//去尾0函数
//去除单链表(L)小数点(n)后的尾部无用0,结果仍用L返回
void DeleteZero(LinkList &L,int &n){
LNode *p;
int num;
num=n;
p=L->next;
while(p->data==0&&num>0){
L->next=p->next;
free(p);
p=L->next;
num=num-1;
}
n=num;
}//DeleteZero
//计算函数(重点)
//将整数单链表(L)的 (n) 次方计算出来,并用单链表(RL)返回
void ComputeList(LinkList L,int n,LinkList &RL){
//int c; //进位标志
int i;
CopyList(L,RL);
for(i=0;i<n-1;i++){
ComputeTwo(RL,L); //意思是:TL=TL*L
}
}//ComputeList
//输入函数
//输入字符数组,将其转变成整数单链表(L),记录开方次数(n),并记录小数点位数(pos)
void InputList(LinkList &L,int &n,int &pos){
char s;
bool flgn=false;
bool flgp=false;
n=0;
pos=0;
LNode *q;
scanf("%c",&s);
while((int)s!=13){
if(((int)s<58)&&((int)s>47)){ //如果输入的字符s为0—9,位数越低级就越靠前
if(flgn==true)pos++;
q=(LNode*)malloc(sizeof(LNode));
q->data=int(s-48);
q->next=L->next;
L->next=q;
scanf("%c",&s);
}
else if((int)s==46){ //如果输入为小数点
flgn=true;
scanf("%c",&s);
}
else if((int)s==32){ //如果输入为空格,表示下一个数为开方次数n
flgp=true;
scanf("%d",&n);
DeleteZero(L,pos);
return;
}
else Error("Wrong Input!");
}
}
//输出函数
//将结果单链表(RL)根据(n),(pos)输出数字和小数点(n*pos),
void OutPutList(LinkList &RL,int n,int pos){
LNode *q;
q=RL->next;
int i=0;
int num=n*pos;
int val[400];
bool flgP,flgO; //flgP是小数点显示记录标志,flgO是第一个非0数字显示记录标志
flgP=false;flgO=false;
while(q){
val[i]=q->data;
q=q->next;
i++;
}
for(i=i-1;i>=0;i--){
if(i==(num-1)){printf(".");flgP=true;}
if(flgP==false&&val[i]==0&&flgO==false)continue;
printf("%d",val[i]);
flgO=true;
}
}//OutputList
int main(){
int n;
int pos;
LinkList L,RL;
InitList(L);
InitList(RL);
InputList(L,n,pos);
ComputeList(L,n,RL);
OutPutList(RL,n,pos);
DestroyList(L);
DestroyList(RL);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator