现在的位置: 首页 > 综合 > 正文

微软编程一小时 题目1 : Arithmetic Expression

2018年01月14日 ⁄ 综合 ⁄ 共 685字 ⁄ 字号 评论关闭

好久没写code了,bug好多%>_<%。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<string>
#include<cstring>
#include <cmath>
#include<algorithm>
using namespace std;

int N;
double a,b;
char c;
double mx=0x3f3f3f3f3f;
int ans=0;
double  res=0;
int idx=0;
void cal()
{
    switch(c)
    {
    case '+':
        res=a+b;
        break;
    case '-':
        res=a-b;
        break;
    case '*':
        res=a*b;
        break;
    case '/':
        res=a/b;
        break;

    }
}
int main()
{
//    double s=3,d=3;
//    double f=0;
//    f=s*d;
//    cout<<f;
   //freopen("input.txt","r",stdin);
    scanf("%d",&N);
   // cout<<N<<endl;
    for(int i=1;i<=N;i++)
    {
        //scanf("%d %c %d",&a,&c,&b);
        cin>>a>>c>>b;
       // cout<<a<<" "<<c<<" "<<b<<endl;
        cal();
        double tmp=abs(9-res);
      // cout<<res<<" "<<mx<<" "<<tmp<<endl;
        if(tmp<mx)
        {
            idx=i;
            mx=tmp;
        }
    }
    printf("%d\n",idx);
    return 0;
}

抱歉!评论已关闭.