(算法练习)——201903-2二十四点(CCF模拟)

2020.2.15更新~~
这次用了栈,真滴灰常方便~果然人还是要多动脑筋呐
思路大致是:
1、用两个栈,一个记录数字,一个记录字符
2、遇到+,+号入字符栈,遇到-,将(-1)*后面的数字,入数字栈,+号入字符栈,因为提前用了下一个字符,要j++跳过下一个字符
3、x、/、直接处理计算的结果入数字栈,j++
4、根据字符栈中+号的个数循环,增加,最后的数字就是计算的结果

AC代码如下:

#include <stdio.h>
#include <stack>
#include <string>
#include <iostream>
using namespace std;

int main(){
	int n;
	int record[100];
	stack<int>num;
	stack<char>zifu;
	string str;
	scanf("%d",&n);
	for(int i = 0;i <n;i++){
		cin>>str;
		while( !num.empty()){
			num.pop();
		}
		while(!zifu.empty()){
			zifu.pop();
		}
		for(int j = 0;j <str.size();j++){
			if(str[j] - '0' >= 0 && str[j] - '0' <= 9){
				num.push(str[j] - '0');
			}
			else{
				if(str[j] == '+'){
					zifu.push('+');
				}
				else if(str[j] == '-'){
					num.push((str[j+1]-'0') * (-1));
					zifu.push('+');
					j++;    //因为后一个数字已经被用掉了,防止重复,j++,下同 
				}
				else if(str[j] == 'x'){
					int t = num.top();
					num.pop();
					num.push(t*(str[j+1] - '0'));
					j++;
				}
				else if(str[j] == '/'){
					int s = num.top();
					num.pop();
					num.push(s/(str[j+1]- '0'));
					j++;
				}
			}	
		}
		while(!zifu.empty()){
			int a = num.top();
			num.pop();
			int b = num.top();
			num.pop();
			num.push(a+b);
			zifu.pop();
		}
		int ans = num.top();
		num.pop();
		record[i] = ans;
	}
	for(int i = 0;i <n;i++){
		if(record[i] == 24){
			printf("Yes\n");
		}
		else{
			printf("No\n");
		}
	}
}

——————————————分割线——————————————
人生第一次C的代码写了200+行快300行。。。无他,if else穷举大法,居然一次AC了= =
事实上这一题用栈,构建后缀表达式是最好的算法,但我翻了书上那一块的代码也很长= =
没办法,太懒了,,改天再写栈的算法吧。。
好在这一题444=64,穷举了64个还可以忍受(其实复制粘贴到吐血= =),换成256那这题只有构建栈了
(这方法真是懒人方法,再说一遍并不推荐= =当然考试的时候能到分不就可以嘛)
AC代码:

#include <stdio.h>
#include <stack>
#include <vector>
#include <stdlib.h>
using namespace std;

int main(){
	int a,b,c,d;
	stack<int>st;
	char f1,f2,f3;  //三个符号
	int n;
	int linshi = 0;   //临时存储结果
	bool hhash[100] = {false};
	scanf("%d",&n);
	for(int i = 0;i <n;i++){
		scanf("%d%c%d%c%d%c%d",&a,&f1,&b,&f2,&c,&f3,&d);
		if(f1 == '+' ){
			if(f2 == '+'){
				if(f3 == '+'){
					linshi = a+b+c+d; 
				}
				else if(f3 == '-'){
					linshi = a+b+c-d;
				}
				else if(f3 == 'x'){
					linshi = a+b+c*d;
				}
				else{
					linshi = a+b+c/d;
				}
			}
			else if(f2 == '-'){
				if(f3 == '+'){
					linshi = a+b-c+d;
				}
				else if(f3 == '-'){
					linshi = a+b-c-d;
				}
				else if(f3 == 'x'){
					linshi = a+b-c*d;
				}
				else{
					linshi = a+b-c/d;
				}
			}
			else if(f2 == 'x'){
				if(f3 == '+'){
					linshi = a+b*c+d;
				}
				else if(f3 == '-'){
					linshi = a+b*c-d;
				}
				else if(f3 == 'x'){
					linshi = a+b*c*d;
				}
				else{
					linshi = a+b*c/d;
				}
			}
			else if(f2 == '/'){
				if(f3 == '+'){
					linshi = a+b/c+d;
				}
				else if(f3 == '-'){
					linshi = a+b/c-d;
				}
				else if(f3 == 'x'){
					linshi = a+b/c*d;
				}
				else{
					linshi = a+b/c/d;
				}
			}
		}
		else if(f1 == '-' ){
			if(f2 == '+'){
				if(f3 == '+'){
					linshi = a-b+c+d;	
				}
				else if(f3 == '-'){
					linshi = a-b+c-d;	
				}
				else if(f3 == 'x'){
					linshi = a-b+c*d;
				}
				else{
					linshi = a-b+c/d;
				}
			}
			else if(f2 == '-'){
				if(f3 == '+'){
					linshi = a-b-c+d;
				}
				else if(f3 == '-'){
					linshi = a-b-c-d;
				}
				else if(f3 == 'x'){
					linshi = a-b-c*d;
				}
				else{
					linshi = a-b-c/d;
				}
			}
			else if(f2 == 'x'){
				if(f3 == '+'){
					linshi = a-b*c+d;
				}
				else if(f3 == '-'){
					linshi = a-b*c-d;
				}
				else if(f3 == 'x'){
					linshi = a-b*c*d;
				}
				else{
					linshi = a-b*c/d;
				}
			}
			else if(f2 == '/'){
				if(f3 == '+'){
					linshi = a-b/c+d;
				}
				else if(f3 == '-'){
					linshi = a-b/c-d;
				}
				else if(f3 == 'x'){
					linshi = a-b/c*d;
				}
				else{
					linshi = a-b/c/d;
				}
			}
		}
		else if(f1 == 'x' ){
			if(f2 == '+'){
				if(f3 == '+'){
					linshi = a*b+c+d;
				}
				else if(f3 == '-'){
					linshi = a*b+c-d;
				}
				else if(f3 == 'x'){
					linshi = a*b+c*d;
				}
				else{
					linshi = a*b+c/d;
				}
			}
			else if(f2 == '-'){
				if(f3 == '+'){
					linshi = a*b-c+d;
				}
				else if(f3 == '-'){
					linshi = a*b-c-d;
				}
				else if(f3 == 'x'){
					linshi = a*b-c*d;
				}
				else{
					linshi = a*b-c/d;
				}
			}
			else if(f2 == 'x'){
				if(f3 == '+'){
					linshi = a*b*c+d;
				}
				else if(f3 == '-'){
					linshi = a*b*c-d;
				}
				else if(f3 == 'x'){
					linshi = a*b*c*d;
				}
				else{
					linshi = a*b*c/d;
				}
			}
			else if(f2 == '/'){
				if(f3 == '+'){
					linshi = a*b/c+d;
				}
				else if(f3 == '-'){
					linshi = a*b/c-d;
				}
				else if(f3 == 'x'){
					linshi = a*b/c*d;
				}
				else{
					linshi = a*b/c/d;
				}
			}
		}
		else if(f1 == '/' ){
			if(f2 == '+'){
				if(f3 == '+'){
					linshi = a/b+c+d;
				}
				else if(f3 == '-'){
					linshi = a/b+c-d;
				}
				else if(f3 == 'x'){
					linshi = a/b+c*d;
				}
				else{
					linshi = a/b+c/d;
				}
			}
			else if(f2 == '-'){
				if(f3 == '+'){
					linshi = a/b-c+d;
				}
				else if(f3 == '-'){
					linshi = a/b-c-d;
				}
				else if(f3 == 'x'){
					linshi = a/b-c*d;
				}
				else{
					linshi = a/b-c/d;
				}
			}
			else if(f2 == 'x'){
				if(f3 == '+'){
					linshi = a/b*c+d;
				}
				else if(f3 == '-'){
					linshi = a/b*c-d;
				}
				else if(f3 == 'x'){
					linshi = a/b*c*d;
				}
				else{
					linshi = a/b*c/d;
				}
			}
			else if(f2 == '/'){
				if(f3 == '+'){
					linshi = a/b/c+d;
				}
				else if(f3 == '-'){
					linshi = a/b/c-d;
				}
				else if(f3 == 'x'){
					linshi = a/b/c*d;
				}
				else{
					linshi = a/b/c/d;
				}
			}
		}
		if(linshi == 24){
			hhash[i] = true;
		}
		else{
			hhash[i] = false;
		}
		linshi = 0;
		
	}
	for(int i = 0 ;i <n;i++){
		if(hhash[i] == false){
			printf("No\n");
		}
		else{
			printf("Yes\n");
		}
	} 
	 
}

/*
10
9+3+4x3
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4+4/5
1x8x3x1

*/
发布了212 篇原创文章 · 获赞 6 · 访问量 6406

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/104269171