C语言写的,不知道问题出在哪?求指教!
查看原帖
C语言写的,不知道问题出在哪?求指教!
1516187
PBtolimits楼主2024/12/25 14:37

用C语言写的,思路就是两个已知的数,思路很简单,代码有些长(繁琐。。。)

试过一些数据都正常,不知道哪里错了
恳请纠正

代码如下:

#include "stdio.h"
#include "math.h"
char ch[1000010];
int main()
{
	char chr;
	int len=0;
	while((chr=getchar())!='\n'){
		ch[len++]=chr;
	}
	int y=0,z=0,cnt=0;
	
	//locate
	int fd_ask=0,fd_plus=0,fd_minus=0,fd_eq=0;
	for(int i=0;i<len;i++){
		if(ch[i]=='?'){
			fd_ask=i;
		}
		if(ch[i]=='+'){
			fd_plus=i;
		}
		if(ch[i]=='-'){
			fd_minus=i;
		}
		if(ch[i]=='='){
			fd_eq=i;
		}
	}
	
	//determine + or -
	if(fd_minus==0){								//plus computing(+)
		if(fd_ask<fd_plus){							// ? + y = z
			for(int j=fd_eq-1;j>fd_plus;j--){		//y
				if(ch[j]!=' '){
					y+=((ch[j]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			cnt=0;
			for(int p=len-1;p>fd_eq;p--){			//z
				if(ch[p]!=' '){
					z+=((ch[p]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			printf("%d",z-y);
		}
		
		y=0,z=0,cnt=0;
		
		if(fd_ask>fd_plus&&fd_ask<fd_eq){			// y + ? = z
			for(int q=fd_plus-1;q>=0;q--){			//y
				if(ch[q]!=' '){
					y+=((ch[q]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			cnt=0;
			for(int a=len-1;a>fd_eq;a--){			//z
				if(ch[a]!=' '){
					z+=((ch[a]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			printf("%d",z-y);
		}
		
		y=0,z=0,cnt=0;
		
		if(fd_ask>fd_eq){							// y + z = ?
			for(int b=fd_plus-1;b>=0;b--){			//y
				if(ch[b]!=' '){
					y+=((ch[b]-'0')*pow(10,cnt));
					cnt++;
				} 
			}
			cnt=0;
			for(int c=fd_eq-1;c>fd_plus;c--){		//z
				if(ch[c]!=' '){
					z+=((ch[c]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			printf("%d",y+z);
		}									
	}
	
	else{
		y=0,z=0,cnt=0;								//minus computing(-)
		if(fd_ask<fd_minus){						// ? - y = z
			for(int d=fd_eq-1;d>fd_minus;d--){		//y
				if(ch[d]!=' '){
					y+=((ch[d]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			cnt=0;
			for(int e=len-1;e>fd_eq;e--){			//z
				if(ch[e]!=' '){
					z+=((ch[e]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			printf("%d",y+z);
		}
		
		y=0,z=0,cnt=0;
		
		if(fd_ask>fd_minus&&fd_ask<fd_eq){			// y - ? = z
			for(int f=fd_minus-1;f>=0;f--){			//y
				if(ch[f]!=' '){
					y+=((ch[f]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			cnt=0;
			for(int g=len-1;g>fd_eq;g--){			//z
				if(ch[g]!=' '){
					z+=((ch[g]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			printf("%d",y-z);
		}
		
		y=0,z=0,cnt=0;
		
		if(fd_ask>fd_eq){							// y - z = ?
			for(int g=fd_minus-1;g>=0;g--){			//y
				if(ch[g]!=' '){
					y+=((ch[g]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			cnt=0;
			for(int h=fd_eq-1;h>fd_minus;h--){		//z
				if(ch[h]!=' '){
					z+=((ch[h]-'0')*pow(10,cnt));
					cnt++;
				}
			}
			printf("%d",y-z);
		}
	}
	
	return 0;
	
}
2024/12/25 14:37
加载中...