Old_Bill(上海交大复试上机题)

编程废物在此,总共用了快一个小时才做出来,也太废了!!!

这道题大概题意就是:

【题目大意】N只火鸡的价格为XYZ,火鸡的总数N在1到99之间。价格由五位数组成,两边的数字由于褪色而看不清,所以只能看到中间的三位数。假设第一个数字非零,每只火鸡的价格是整数,并且所有火鸡的价格相同。给定N,X,Y和z,编写一个程序来猜测两边褪色的数字和火鸡的原始价格。如果有多个价格符合题意,那么输出最昂贵的那个。

原题目描述

Among grandfather’s papers a bill was found. 72 turkeys $.The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted_), for they are faded and are illegible. What are the two faded digits and what was the price of one turkey? We want to write a program that solves a general version of the above problem. N turkeys $ The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the turkeys cost the same price. Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. In case that there is more than one candidate for the original price, the output should be the most expensive one. That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.

输入描述:

The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys. In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $XYZ.

输出描述:

For each case, output the two faded digits and the maximum price per turkey for the turkeys.

代码(纯暴力枚举)

#include<iostream>
using namespace std;
void isPrice(int n, int num){
    int max = 0;
    int A, B;//分别代表第一位数和最后一位数  __XYZ__   a X Y Z b 
    for(int a = 1; a <= 9; a++){
        for(int b = 0; b <= 9; b++){
            if((a * 10000 + num + b) % n == 0){
                int price = a * 10000 + num + b;
                if(price >= max){ //判断是否为最大值
                    max = price;
                    A = a;
                    B = b;
                }
            }
        }
    }
    if(max == 0){
        cout << "0" << endl;
    }else{
        cout << A << " " << B << " " << (max / n) << endl;
    }
}
int main(){
    int n, x, y, z;
    while(cin >> n){
        cin >> x >> y >> z;
        int num = x * 1000 + y * 100 + z * 10;
        isPrice(n, num);
    }
    return 0;
}

看到别人的题解思路中:

循环从9递减可以避免去判断是否为最昂贵的价格,但凡出现的第一个则为价格最贵的,跳出循环即可。

尝试一下

代码(循环优化)

void isPrice(int n, int num){
    int max = 0, flag = 0; ////0代表未找到最佳答案
    int a, b;//分别代表第一位数和最后一位数  __XYZ__   a X Y Z b 
    for(a = 9; a >= 1; a--){
        for(b = 9; b >= 0; b--){
            if((a * 10000 + num + b) % n == 0){
                max = a * 10000 + num + b;
                cout << a << " " << b << " " << (max / n) << endl;
                flag = 1; //1代表已找到最佳答案,可以跳出循环
                break;
            }
        }
        if(flag){
            break;
    }
    }
    if(max == 0){
        cout << "0" << endl;
    }
}
全部评论
第二个题昨天我笔试的时候遇到了
点赞 回复 分享
发布于 2022-08-31 20:24 陕西

相关推荐

自从我室友在计算机导论课上听说了“刷&nbsp;LeetCode&nbsp;是进入大厂的敲门砖”,整个人就跟走火入魔了一样。他在宿舍门口贴了一张A4纸,上面写着:“正在&nbsp;DP,请勿打扰,否则&nbsp;Time&nbsp;Limit&nbsp;Exceeded。”日记本的扉页被他用黑色水笔加粗描了三遍:“Talk&nbsp;is&nbsp;cheap.&nbsp;Show&nbsp;me&nbsp;the&nbsp;code。”连宿舍聚餐,他都要给我们讲解:“今天的座位安排可以用回溯算法解决,但为了避免栈溢出,我建议用动态规划。来,这是状态转移方程:dp[i][j]&nbsp;代表第&nbsp;i&nbsp;个人坐在第&nbsp;j&nbsp;个位置的最优解。”我让他去楼下取个快递,他不直接去,非要在门口踱步,嘴里念念有词:“这是一个图的遍历问题。从宿舍楼(root)到驿站(target&nbsp;node),我应该用&nbsp;BFS&nbsp;还是&nbsp;DFS?嗯,求最短路径,还是广度优先好。”和同学约好出去开黑,他会提前发消息:“集合点&nbsp;(x,&nbsp;y),我们俩的路径有&nbsp;k&nbsp;个交点,为了最小化时间复杂度,应该在&nbsp;(x/2,&nbsp;y/2)&nbsp;处汇合。”有一次另一个室友低血糖犯了,让他帮忙找颗糖,他居然冷静地分析道:“别急,这是一个查找问题。零食箱是无序数组,暴力查找是&nbsp;O(n)。如果按甜度排序,我就可以用二分查找,时间复杂度降到&nbsp;O(log&nbsp;n)。”他做卫生也要讲究算法效率:“拖地是典型的岛屿问题,要先把连通的污渍区块都清理掉。倒垃圾可以用双指针法,一个指针从左往右,一个从右往左,能最快匹配垃圾分类。”现在我们宿舍的画风已经完全变了,大家不聊游戏和妹子,对话都是这样的:“你&nbsp;Two&nbsp;Sum&nbsp;刷了几遍了?”“别提了,昨天遇到一道&nbsp;Hard&nbsp;题,我连暴力解都想不出来,最后只能看题解。你呢?”“我动态规划还不行,总是找不到最优子结构。今天那道接雨水给我整麻了。”……LeetCode&nbsp;真的害了我室友!!!
老六f:编程嘉豪来了
AI时代还有必要刷lee...
点赞 评论 收藏
分享
04-15 13:42
四川大学 Java
蹲蹲offerrr:快投吧,有点晚现在
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务