Java - 一道关于整型和字符类型相加的题目

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

题目

public class Test {

    public static void main(final String[] args) {
        final int a = 10;
        final int b = 20;
        System.out.println(a + '+' + b + '=' + (a + b));
    }
}

乍一看,可能有的同学会觉得很简单,直接给出如下答案:

10+20=30

其实这是道陷进题,很容易就看混了,正确的答案是:

164

分析

为什么答案会是164

这是因为这里并不是字符串拼接,而是整型与字符类型的加法运算。

用一对单引号'包括起来的是字符,用一对双引号"包括起来的是字符串。

对于字符串来说,用+拼接起来的结果还是字符串。但这道题中,用+拼接起来的是整型和字符类型,就变成了单纯的加法运算。在加法运算中,数据类型会从低位自动转换成高位,因为高位转低位会有数据溢出导致丢失精度的风险。

当整型与字符类型相加时,字符类型会转换成整型,也就是转换成对应的ASCII码值。于是乎,a + '+' + b + '=' + (a + b)就变成了10 + 43 + 20 + 61 + (10 + 20),其运算结果就是164

题目变形

其一

public class Test {

    public static void main(final String[] args) {
        final int a = 10;
        final int b = 20;
        System.out.println(a + "+" + b + "=" + (a + b));
    }
}

这道题的答案就是10+20=30了。

其二

public class Test {

    public static void main(final String[] args) {
        System.out.println('A');
        System.out.println((int)'A');
    }
}

这道题的答案是:

A
65

如果我们想知道某个字符对应的ASCII码是多少,可以将其转换成整型,这个值就是其ASCII码值。

展开阅读全文

一道关于动态规划的题目

12-03

Description rnrnBefore ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid. rnrnBut there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs! rnrnInput rnrnThe input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams. rnrnOutput rnrnPrint exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.". rnrnSample Input rnrnrn3rn10 110rn2rn1 1rn30 50rn10 110rn2rn1 1rn50 30rn1 6rn2rn10 3rn20 4rnrnrnSample Output rnrnrnThe minimum amount of money in the piggy-bank is 60.rnThe minimum amount of money in the piggy-bank is 100.rnThis is impossible.rn————————————————————————————————rn[code=C/C++][/code]rn#includernusing namespace std;rnint main()rnrn int test;rn cin>>test;rn while(test!=0)rn rn int weight,weight_box;rn cin>>weight_box>>weight;rn int n;rn cin>>n;rn int *p=new int[n+1];rn int *w=new int[n+1];rn for(int i=1;i!=n+1;i++)rn rn cin>>p[i]>>w[i];rn rn int coin_weight=weight-weight_box;rn int **value=new int*[n+1];rn for(int i=0;i!=n+1;i++)rn value[i]=new int[coin_weight+1];rn for (int i=0;i!=n+1;i++)rn for(int j=0;j!=coin_weight+1;j++)rn rn if(j==0)rn value[i][j]=0;rn elsern value[i][j]=20002000;rn rn //value[0][0]=0;rn for(int i=1;i!=n+1;i++)rn for(int j=1;j!=coin_weight+1;j++)rn for(int k=0; k!=j/w[i]+1;k++)rn rn rn value[i][j]=min(value[i][j],value[i-1][j-k*w[i]]+k*p[i]);rn rn rn if(value[n][coin_weight]!=20002000)rn cout<<"The minimum amount of money in the piggy-bank is "< 论坛

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