G-Ternary Calculation(字符串模拟题)

Complete the ternary calculation.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is a string in the form of "number1 operatora number2 operatorb number3". Each operator will be one of {'+', '-' , '*', '/', '%'}, and each number will be an integer in [1, 1000].

Output

For each test case, output the answer.

Sample Input
5
1 + 2 * 3
1 - 8 / 3
1 + 2 - 3
7 * 8 / 5
5 - 8 % 3
Sample Output
7
-1
0
11
3
Note

The calculation "A % B" means taking the remainder of A divided by B, and "A / B" means taking the quotient.

 

题目的大致意思就是:

给你3个实型整数,然后两个运算符,要你模拟它的过程,然后得出最终的答案。

一开始我想的是分别把这几个运算符存到数组中去,然后判断第几个再进行相应的过程,但是后来发现这样会有错误而且写的很复杂,因为你并没有判断过符号的优先顺序,所以最后算出来的结果可能是错的。

后来我瞄了一下题解,发现可以用暴力枚举法,因为数据量不是很大,所以直接分类就好。

1.oper1是低级的运算符(即为+或是-),oper2是高级的运算符(即为*、/、%),那么就先算第二个然后再算第一个;

2.oper1是低级的,oper2也是低级的,那么直接顺序相加就好。

3.oper1是高级的,oper2是高级的或是低级的,那么也是直接顺序的算过来就好。

 

#include<stdio.h>
#include<string.h>
int main(){
	int a,b,c,T,i,j,k,sum;
	char ss1[10],ss2[10];
	scanf("%d",&T);
	while(T--){
		scanf("%d%s%d%s%d",&a,ss1,&b,ss2,&c);
		sum=0;
		//注意这里不能直接把ss2[0]=='+'或'-'给写进去,因为这样的话还是从第二个开始计算,然后才算第一个的,
		//比如样例1-3+2这样的就错了; 
		if((ss1[0]=='+'||ss1[0]=='-')&&(ss2[0]=='*'||ss2[0]=='/'||ss2[0]=='%')){
			if(ss2[0]=='*') sum=b*c;
			else if(ss2[0]=='/')  sum=b/c;
			else if(ss2[0]=='%') sum=b%c;
			else if(ss2[0]=='+') sum=b+c;
			else if(ss2[0]=='-')  sum=b-c;
			if(ss1[0]=='+')  sum+=a;
			else if(ss1[0]=='-')  sum=a-sum;
		}
		if((ss1[0]=='+'||ss1[0]=='-')&&(ss2[0]=='+'||ss2[0]=='-')){
			if(ss1[0]=='+') sum=a+b;
			else if(ss1[0]=='-') sum=a-b;
			if(ss2[0]=='+') sum+=c;
			else sum-=c;
		}
		if((ss1[0]=='*'||ss1[0]=='/'||ss1[0]=='%')&&(ss2[0]=='*'||ss2[0]=='/'||ss2[0]=='%'||ss2[0]=='+'||ss2[0]=='-')){
			if(ss1[0]=='*') sum=a*b;
			else if(ss1[0]=='/') sum=a/b;
			else if(ss1[0]=='%') sum=a%b;
			if(ss2[0]=='*') sum=sum*c;
			else if(ss2[0]=='/')  sum=sum/c;
			else if(ss2[0]=='%') sum=sum%c;
			else if(ss2[0]=='+')  sum+=c;
			else if(ss2[0]=='-')  sum-=c;
		}
		printf("%d\n",sum);
	}
}


 

这种题目也许第一次遇见自己可能会想的稍微久一点,但是积累了思路之后就能慢慢的变得更加强大!加油~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个警告是由ESLint的规则`no-unneeded-ternary`引起的,它表示在默认赋值时使用条件表达式是不必要的。 在上面的代码示例中,可以看到在`filterItems`方法中使用了条件表达式来处理空字符串的情况。然而,根据该警告,使用条件表达式是不必要的,因为我们可以直接使用简单的赋值操作来实现相同的效果。 要解决这个警告,你可以将条件表达式改为简单的赋值操作。 下面是修改后的代码示例: ```vue <template> <div> <q-select v-model="selectedItem" :options="filteredItems" option-label="label" option-value="value" :filter="filterItems" input-debounce="300" placeholder="Search" /> </div> </template> <script> export default { data() { return { selectedItem: '', items: [ { label: 'Option 1', value: 'option1' }, { label: 'Option 2', value: 'option2' }, { label: 'Option 3', value: 'option3' }, // 其他选项... ], filteredItems: [] }; }, methods: { filterItems(val, update) { if (val === '') { update(() => { this.filteredItems = []; }); } else { const lowercaseVal = val.toLowerCase(); this.filteredItems = this.items.filter(item => item.label.toLowerCase().includes(lowercaseVal) ); } } } }; </script> ``` 在修改后的代码中,我们移除了条件表达式,并直接在`if`语句的分支中进行赋值操作。这样可以避免不必要的条件表达式。 通过这种方式,我们可以消除ESLint警告,并且代码逻辑仍然保持一致。 希望这个解决方案对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值