SuzumiyaSAMA 2017-05-16 10:18 采纳率: 92.3%
浏览 1202
已采纳

一道算法水题,但是不知道问题在哪,求解

原题:

   JH苦练10年,终于成为了一个神箭手,在下山之前,大师兄YZ不放心,想考验他,只给他一定时间t,同时给他n支箭,最终根据他的表现,考虑他是否能下山。
   对于每发一次箭,YZ给他4种成绩(优、良、中、差),JH有三种拉弓以及瞄准时间a ,b,c(a>=b>=c)分别能拿优,良,中等级,如果不拉弓不瞄(直接射),只能拿差(不能中靶)了。
   现在JH想知道,在保证自己弹无虚发(不获得差)的情况下,最多能拿多少个优。
   如果JH不能做到弹无虚发,输出Oh,my god! 

Input

   输入数据包含T组:
   对于每组数据,第一行为一个整数n,表示总共有n支箭。

(0<n<=1000)
之后n行,每行包含三个数字a,b,c,分别表示拿对应等级所需要花的时间。
(0<c<=b<=a<=1000)
之后一个数字t,表示JH有考核总时间为t
(0<=t<=1e6)

Output

对于每组输入,如果JH能箭无虚发,则输出一个数字x,表示最多能拿到的优的数量。如果不能,则输出Oh,my god!

Sample Input

3
1
3 2 1
1
2
3 2 1
3 2 1
4
2
3 2 1
3 2 1
1

Sample Output

0
1
Oh,my god!

代码1:
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
//DEBUG MODE
#define debug 0
//循环
#define REP(n) for(int o=0;o<n;o++)
const int maxn = 1005;
struct Time {
    int PTime,MTime;
    int delta;
    Time(int x=0,int y=0) {
        PTime = x;
        MTime = y;
        delta = x - y;
    }
    bool operator < (const Time &rhs)const {
        return delta > rhs.delta;
    }
};
Time UseTime[maxn];
void Do() {
    int t,n,UT=0;
    scanf("%d",&n);
    int sum = 0;
    REP(n) {
        int a,c;
        scanf("%d%*d%d",&a,&c);
        sum += c;
        UT += a;
        UseTime[o] = Time(a,c);
    }
    scanf("%d",&t);
    if(sum > t) {
        printf("Oh,my god!\n");
        return;
    }
    sort(UseTime,UseTime + n);
    int ans = n;
    for(int i = 0;UT > t;i++) {
        ans--;
        UT -= UseTime[i].delta;
    }
    printf("%d\n",ans);
}
int main() {
    int T;
    scanf("%d",&T);
    while(T--)
        Do();
    return 0;
}

代码2:

#include <algorithm>
#include <iostream>
using namespace std;

struct TimeNode {
    int a, c, delta;
    TimeNode(int A = 0, int C = 0){
        a = A;
        c = C;
        delta = a-c;
    }
    bool operator < (const TimeNode &rhs) const {
        return delta > rhs.delta;
    }
};
int main(void){
    TimeNode times[1005];
    int T = 0;
    cin >> T;
    while (T--){
        int n = 0, t = 0, a = 0, b = 0, c = 0, a_plus = 0, c_plus = 0;
        cin >> n;
        for (int count = 0; count < n; count++){
            cin >> a >> b >> c;
            times[count] = TimeNode(a,c);
            a_plus += a;
            c_plus += c;
        }
        cin >> t;
        if (c_plus > t){
            cout << "Oh,my god!" << endl;
            continue;
        }
        sort(times, times+n);
        for (int count = 0; a_plus > t; count++){
            n--;
            a_plus -= times[count].delta;
        }
        cout << n << endl;
    }
    return 0;
}

程序1正确但是程序2无法通过,但是我觉得两个程序的语义是一样的,求解。

  • 写回答

1条回答 默认 最新

  • shen_wei 2017-05-17 07:37
    关注
      if (c_plus > t){
                cout << "Oh,my god!" << endl;
                continue;
            }
    
      if(sum > t) {
            printf("Oh,my god!\n");
            return;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题