OJ题 坑爹的黑店

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43690204/article/details/88967337

题目描述

今天小明去了一个风景如画的地方散心,但是自己带的饮料喝完了,小明口渴难耐,见不远处有家小商店,于是跑去买饮料。

小明:“我要买饮料!”

店主:“我们这里有三种饮料,矿泉水1.5元一瓶,可乐2元一瓶,橙汁3.5元一瓶。”

小明:“好的,给我一瓶矿泉水。”

说完他掏出一张N元的大钞递给店主。

店主:“我忘了提醒你了,我们这里没有找客人钱的习惯的,多的钱我们都当小费收了的,嘿嘿。”

小明:“......”

小明环顾四周,就这一家商店,况且实在太渴了,就决定在这买了。不过小明想,与其把钱当小费送给他还不如自己多买一点饮料,反正早晚都要喝,但是要尽量少让他赚小费。

现在小明希望你能帮他计算一下,最少他要给店主多少小费。

 

输入

输入数据的第一行是一个整数T(1<=T<=100),代表测试数据的数量。然后是T行测试数据,每个测试数据只包含一个正整数N(1<=N<=10000),N代表小明手中钞票的面值,以分为单位。
注意:商店里只有题中描述的三种饮料。

 

输出

对于每组测试数据,请你输出小明最少要浪费多少钱给店主作为小费,以分为单位。

 

样例输入

2
900
250

 

样例输出

0
50

代码块:

import java.util.Scanner;
 
public class Main{
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner=new Scanner(System.in);
        int T=scanner.nextInt();
        int num=0;
        for(int i=0;i<T;i++){
            int N=scanner.nextInt();
            if(N<150)
                System.out.println(N);
            else if(N>=300)
                System.out.println(N%50);
            else if(N==150||N==200)
                System.out.println(0);
            else if(150<N&&N<200)
                System.out.println(N%150);
            else
                System.out.println(N-200);
        }
    }
 
}

解题思路:首先在0-150分时,直接是小费,直接输出N。

                  N在150-200阶段时,当N=150或者是N=200时可以直接输出0。当N大于150小于200的时候,给的小费直接是N-150.

                  N大于300的时候,当N=300和350的时候,给的小费是0,在300和350 之间的时候,给的小费就是N-300,N在350到400的时候,给的小费是N-350。以此类推,大于30 的时候给的小费都不会超过50分。

                综合上边的情况,其实代码还可以简化,把150到200 和大于等于300的情况写在一个条件里面。

               最后剩下 的就是200到300的情况,可以直接用N-200,这个条件比较简单。

简化之后的代码:

import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner=new Scanner(System.in);
        int T=scanner.nextInt();
        int num=0;
        for(int i=0;i<T;i++){
            int N=scanner.nextInt();
            if(N<150)
                System.out.println(N);
            else if(N<=200 || N>=300)
                System.out.println(N%50);
            else
                System.out.println(N-200);
        }
    }
 
}

代码简化之后,代码更加简洁了。

感想:做很多题之后会发现,我们写一个程序可能有很多种办法,这道题我的第一种想法可能会是,谢一个三重循环,第一个先写条件150,这样子下去,因为大一的时候做过类似的题,用的就是那种方法。课时做这个题之后,我去博客里面找了一下这道题,当时就看见了用着个算法做的。当时还和室友争论了很久都没有明白,第二天找了老师给我们讲。说实话之前没有想过算法的奥妙之处,这次这道题之后,我对算法产生了极大的兴趣。

展开阅读全文

求助一道OJ

01-13

DescriptionrnAn adjacent difference of a sequence is a new sequence formed by replacing every element with the difference between the element and rnthe immediately preceding element. The first value in the new sequence remains unchanged. For example, a sequence such as (1, 3, 2, 4, 5) rnis transformed into (1, 3-1,2-3, 4-2, 5-4), and in this manner becomes the sequence (1, 2, -1, 2, 1). Then, we want to sort the adjacent rndifference of the sequence in non-decreasing order. It?s an easy job for you, isn?t it? So, please solve it quickly.rnInputrn Standard input will contain multiple test cases. The first line of the input is a single integer T(1 <= T <= 50) which is the number of rntest cases.rn For each test case, the first line contains an integer N(1 <= N <= 1000), representing the size of the sequence. The second line contains rnN integers (you are ensured that the absolute value of each integer is less than ), representing the elements of this sequence.rnOutputrn Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from rn1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.rn For each test case, output one line containing n elements representing the sorted adjacent difference of the sequence. Elements are rnseparated by one blank space.rnSample Inputrn1rn5rn1 3 2 4 5rnSample OutputrnCase 1:rn-1 1 1 2 2rnrn为啥我的代码总是Wrong answerrn[code=c]#include rn#include rnint main()rnrn int n;rn int sum;rn int tmp;rn long a[1001] = 0 ;rn int i, j, k;rnrn scanf("%d", &n);rnrn for (k = 1; k <= n; k++)rn rn scanf("%d", &sum);rn for (i = 0; i < sum; i++)rn scanf("%d", &a[i]);rn for (i = sum - 1; i > 0; i--)rn a[i] -= a[i - 1];rnrn for (i = 1; i < sum; i++)rn rn for (j = i; j > 0 && a[j] < a[j - 1]; j--)rn rn tmp = a[j];rn a[j] = a[j - 1];rn a[j - 1] = tmp;rn rn rn printf("Case %d:\n", k);rn for (i = 0; i < sum; i++)rn rn printf("%d", a[i]);rn if (i != sum - 1)rn printf(" ");rn rn if (k != n)rn printf("\n\n");rn rnrn return 0;rn[/code] 论坛

求教一道OJ

05-02

图片我不会贴,网址是http://acm.uestc.edu.cn/ShowProblem.aspx?ProblemID=1185rn帐号是jimoqianchenrn密码是19890929rn1185 Easy Math rn题目的大概意思是说给出 a, b, c, drn第一次是由X1 + X2= a , X1^2 + X2^2 = b , X1^3 + X2^3 = c , X1^4 + X2^4 = d求X1 * X2得出第一个数rn第二次是由X1 + X2 + X3 = a,X1^2 + X2^2 + X3^2 = b,X1^3 + X2^3 + X3^3 = c, X1^4 + X2^4 + X3^4 = drn求X1 * X2 * X3得出第二个数 rn第三次是由X1 + X2 + X3 + X4 = a,X1^2 + X2^2 + X3^2 + X4^2 = b,X1^3 + X2^3 + X3^3 + X4^3 = c,X1^4 + X2^4 + X3^4 + X4^4 = d,求X1 * X2 * X3 * X4 得出第三个数rnThe following equation will be constantly valid no matter what values the variable n and xi have.rn有一张图表示 2 * x1 * x2 = (x1 + x2)^2 - x1^2 - x2^2rnrnwhich means, we could yield the sum of cross items product from sum and sum of square. Now the challenge is to calculate the sum cross items product with given sum, sum of squares, sum of cubes and sum of fourth powers.rnrnHere we have to define of sum of m-th power items as: x1到xn的m次方和rnrnrnIn addition, sum of cross items product is defined as:x1连乘到xn rnrnrn rnInput rnThe first line of the input contains one integer T, which indicate the number of test cases. Each test case contains four integers indicating sum, sum of squares, sum of cubes and sum of fourth power in order, and each is in the range of [-50000,50000]. rn rnOutput rnOne line for each test case contains three integers indicating the answer, which are sum of the cross items with k in the range (2 ≤ k ≤ 4) separated with a blank. rn rnSample Input rn1rn10 30 100 354 rn rnSample Output rn35 50 24rn rn 论坛

一道oj的题

12-03

从键盘输入10个整数(10个整数均不相同), 然后再从键盘中输入一个整数a,如果该整数a为奇数, 且与这10个整数中的任一个数相同,则删掉与a相同的 这个数,并将剩下的9个数按升序排序输出;若a为偶数, 且与这10个数都不同,则加入a,并将这11个数降序排 序输出;否则,则将这10个数奇数在前,偶数在后, 降序排列输出。例如,10个数分别为6,89,34,12, 0,-8,78,15,9,3.若a为9,则输出为:-8,0,3, 6,12,15,34,78,89.若a为2,则输出为:89,78, 34,15,12,9,6,3,2,0,-8.若a为7或者12,则 输出为:89,15,9,3,78,34,12,6,0,-8.rnrn要求:使用函数解决本题,至少编写以下几个函数,1、升序排序函数void sort(int a[], int n);2、查找函数int find(int a[],int n,int value)----在大小为n的a数组中查找值为value的元素,找到就返回元素的下标,找不到,就返回-1;3、输出函数display(int a[], int n)---逐个输出数组a的元素;4、反转数组函数void reverse(int a[], int n)----将数组逆序。rn我的代码:rn#includern#includernvoid sort(int a[],int n);rnint find(int a[],int n,int value);rndisplay(int a[],int n);rnvoid reverse(int a[],int n);rnint main()rnint a,b,c,d,e,i,j,h;rnint arr[11];rnint arr1[11];rnint arr2[11];rnscanf("%d",&a);rnfor(b=0;b=0)rn for(e=0;e<10;e++)rn if(arr[e]%2==1)rn arr1[i]=arr[e];rn i=i+1;rn elsern arr2[j]=arr[e];rn j=j+1;rn rn rnrn reverse(arr1,i);rn display(arr1,i);rn printf(" ");rn reverse(arr2,j);rn display(arr2,j);rn printf("\n");rn elsern arr[10]=d;rn reverse(arr,11);rn display(arr,11);rn printf("\n");rn rn elsern if(find(arr,10,d)>=0)rn i=find(arr,10,d);rn for(e=i;e<9;e++)rn arr[e]=arr[e+1];rn rn sort(arr,9);rn display(arr,9);rn printf("\n");rn elsern i=0;rn j=0;rn for(e=0;e<10;e++)rn if(arr[e]%2==1)rn arr1[i]=arr[e];rn i=i+1;rn elsern arr2[j]=arr[e];rn j=j+1;rn rn rn reverse(arr1,i);rn display(arr1,i);rn printf(" ");rn reverse(arr2,j);rn display(arr2,j);rn printf("\n");rn rnrnrnrnreturn 0;rnrnvoid sort(int a[],int n)rn int i,j,h;rn for(i=0;ia[j+1])rn h=a[j];rn a[j]=a[j+1];rn a[j+1]=h;rn rn rn rnrnint find(int a[],int n,int value)rn int i;rn for(i=0;i 论坛

pku oj的1002题

09-29

在codeblocks和vs2010上编译运行结果都正确 为什么 上传就说Compile ErrorrnrnMain.crnF:\temp\10866305.72650\Main.c(16) : error C2143: syntax error : missing ';' before 'type'rnF:\temp\10866305.72650\Main.c(20) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(20) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(21) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(21) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(22) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(22) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(22) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(22) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(22) : error C2168: 'strlen' : too few actual parameters for intrinsic functionrnF:\temp\10866305.72650\Main.c(22) : error C2198: 'change' : too few arguments for callrnF:\temp\10866305.72650\Main.c(30) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(30) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(30) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(30) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(30) : error C2168: 'strcmp' : too few actual parameters for intrinsic functionrnF:\temp\10866305.72650\Main.c(33) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(33) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(33) : error C2168: 'strcpy' : too few actual parameters for intrinsic functionrnF:\temp\10866305.72650\Main.c(34) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(34) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(34) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(34) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(34) : error C2168: 'strcpy' : too few actual parameters for intrinsic functionrnF:\temp\10866305.72650\Main.c(35) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(35) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(35) : error C2168: 'strcpy' : too few actual parameters for intrinsic functionrnF:\temp\10866305.72650\Main.c(39) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(39) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(39) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(39) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(39) : error C2168: 'strcmp' : too few actual parameters for intrinsic functionrnF:\temp\10866305.72650\Main.c(41) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(41) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(46) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(46) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(46) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(46) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(46) : error C2168: 'strcpy' : too few actual parameters for intrinsic functionrnF:\temp\10866305.72650\Main.c(61) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(61) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(66) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(66) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(68) : error C2065: 'p_tel' : undeclared identifierrnF:\temp\10866305.72650\Main.c(68) : error C2109: subscript requires array or pointer typernF:\temp\10866305.72650\Main.c(74) : error C2065: 'p_tel' : undeclared identifierrnrnrn代码贴上[code=C/C++]#include rn#includernrnrnstruct telrnrn //char *num;//wrongrn char num[15];rn int n;rn;rnvoid change(char *,int);rnint main()rnrn int tel_num=0;rn int i=0,j=0;rn scanf("%d",&tel_num);rn struct tel *p_tel=(struct tel*)malloc(sizeof(struct tel)*tel_num);rnrn for(i=0;i0)rn rn char m[8];rn strcpy(m,p_tel[j].num);rn strcpy(p_tel[j].num,p_tel[j+1].num);rn strcpy(p_tel[j+1].num,m);rnrnrn rn else if(strcmp((p_tel[j].num),(p_tel[j+1].num))==0)rn rn p_tel[j].n++;rn tel_num--;rnrn for(q=j;q 论坛

没有更多推荐了,返回首页