int输出%f浮点值是0,double/float 浮点数%d输出0的原因

输出为:

  • int输出%f浮点值的时候,比如2,2内部表示如果看作是float,是个很小的数所以输出的是0.000000
  • double/float 浮点数按照%d输出,如果输出为0,则说明a的数据放在地址的高端,而整型比浮点数内存中占的字节数少,整型只会把属于它的字节数读出来,如在Win32,VC6.0下,Int是4位,它就会把从a开始的4位读出来(按整型格式),所以它把浮点数低端地址的0给输出出来。
  • 所以说使用printf的时候数据格式一定要对应,或者使用printf("%d", (int)a);这样强制转换的方法输出不同格式的数据

1033. To Fill or Not to Fill (25)-PAT甲级真题(贪心算法)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,…N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print “The maximum travel distance = X” where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00

题目大意:汽车从杭州出发可以通过高速公路去任何城市,但是油箱的容量是有限的,路上有很多加油站,每个加油站的价格不同,为汽车设计一个从杭州到终点的最便宜的路线,Cmax表示油箱最大容量,D表示杭州到目的地的距离,Davg表示平均每单位的汽油可以让汽车行驶的距离,N表示汽车的站点数量,每个站点都会给出它的单位油价Pi和汽车站点和杭州的距离Di,求汽车从杭州到终点的最小花费,如果不能够到达,就输出汽车能够行驶的最大距离

分析:贪心算法。
0.假设增加一个目的地处的加油站,距离为目的地的距离,价格为0,考虑从0距离开始能否到达最后一个加油站的问题
1.因为先开始没有油,所以如果所有的加油站距离都没有等于0的,那么说明车哪也去不了,直接输出并return
2.将加油站按照距离dis从小到大排序
3.先去第一个加油站,设置变量nowdis表示当前所在的距离,maxdis是能够到达的最大距离,nowprice是当前的站点的价格,totalPrice是总的价格。
贪心思想:
0.寻找比自己距离远的,到能够到达的最大距离之间的加油站,看他们的油价。如果找到了更低价格的油价,就加油到刚好能到达那个加油站的距离的油,然后去那个更低价格的加油站(有更低的我一分都不想多花在别的距离上,只加到刚好满足更低价格的加油站的距离就行,那样以后的路程我就可以以更低的价格行驶啦)
1.如果找不到更低的,就找尽可能低的油价的加油站,在当前加油站加满油之后过去。因为想要让路程上使用的尽可能是低价的油,既然没有比当前更低价格的了,就让油箱加到最大值,这样能保证利益最大化,保证最大的距离使用的是便宜的油。

 

1010. Radix (25)-PAT甲级真题(二分法)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.

Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible

分析:convert函数:给定一个数值和一个进制,将它转化为10进制。转化过程中可能产生溢出
find_radix函数:找到令两个数值相等的进制数。在查找的过程中,需要使用二分查找算法,如果使用当前进制转化得到数值比另一个大或者小于0,说明这个进制太大~

 

【C/C++】全局变量只能初始化不能赋值

  • C/C++中,全局变量只能声明、初始化,而不能赋值
  • 也就是说,下面这样是不被允许的:

  • 错误提示是:
  • 声明、初始化与赋值的区别:
    • 声明:int a;
    • 初始化:int a = 2;(在声明的时候顺带赋值叫做初始化)
    • 赋值:a = 2;
  • 只有定义(int a;)才分配存储空间,初始化必须要有存储空间来初始化
  • 全局变量在声明时候顺带赋值(也就是初始化)是可以的,但是如果先声明,不赋值,之后再赋值的话,程序是执行不到这里的,也无法通过编译。

1080. Graduate Admission (30)-PAT甲级真题

It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.

Each applicant will have to provide two grades: the national entrance exam grade GE, and the interview grade GI. The final grade of an applicant is (GE + GI) / 2. The admission rules are:

The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.
If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE. If still tied, their ranks must be the same.
Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one’s turn to be admitted; and if the quota of one’s most preferred shcool is not exceeded, then one will be admitted to this school, or one’s other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.
If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.
Input Specification:

Each input file contains one test case. Each case starts with a line containing three positive integers: N (<=40,000), the total number of applicants; M (<=100), the total number of graduate schools; and K (<=5), the number of choices an applicant may have.

In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.

Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant’s GE and GI, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M-1, and the applicants are numbered from 0 to N-1.

Output Specification:

For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants’ numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.

Sample Input:
11 6 3
2 1 2 2 2 3
100 100 0 1 2
60 60 2 3 5
100 90 0 3 4
90 100 1 2 0
90 90 5 1 3
80 90 1 0 2
80 80 0 1 2
80 80 0 1 2
80 70 1 3 2
70 80 1 2 3
100 100 0 2 4
Sample Output:
0 10
3
5 6 7
2 8

1 4

题目大意:每个考生有两个成绩:GE和GI,最终成绩为(GE + GI) / 2;按照最终成绩排名,如果最终成绩相同,就按照GE排名,如果仍然相同,他们的排名就是相同的。每个申请者有K个选择院校,每个学校也有招生人数限制。按照排名先后,如果当前考生的第一个志愿学校的名额还没满,就录取进去;如果当前志愿名额满了但是该校最后一个录取的人的排名和当前考生相同,则不管招生人数限制,依旧应该被录取;否则考虑该生的下一个志愿。如果所有志愿都没有能被录取,则该生落榜。

分析:
1.stu容器里放学生{id, ge, gi, fin, choice(容器里放学生报考学校的id)}, quota数组放招生计划的数量,cnt数组存放当前学校已经招收的学生数,sch数组里放的容器,容器里是学校已经招的学生的id~
2.对学生按照分数排序,依次学生遍历,分数最高的学生先挑学校~
3.对于每个学生录取到哪里:依次遍历学生的报考志愿,如果(没招满 || 他与已经招的学生的最后一名成绩并列)就把他招进去,该学生录取结果即可确定,更新该学校已经招生的人数,并把次学生加入该学校录取容器中~
4.输出学校录取情况时学生id顺序是乱的,要先从小到大排序,然后输出。每个学校占一行~
5.排序函数要用 & 引用传参,不然会超时~
6.因为分数 fin = ge + gi 不会超出int, fin / 2 和fin排名效果一样, 不除2不会影响结果,而且还可以巧妙躲避除2后double不能精确表示的问题~ 

1103. Integer Factorization (30)-PAT甲级真题(dfs深度优先)

The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K-P factorization of N for any positive integers N, K and P.

Input Specification:

Each input file contains one test case which gives in a line the three positive integers N (<=400), K (<=N) and P (1<P<=7). The numbers in a line are separated by a space.

Output Specification:

For each case, if the solution exists, output in the format:

N = n1^P + … nK^P

where ni (i=1, … K) is the i-th factor. All the factors must be printed in non-increasing order.

Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122 + 42 + 22 + 22 + 12, or 112 + 62 + 22 + 22 + 22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen — sequence { a1, a2, … aK } is said to be larger than { b1, b2, … bK } if there exists 1<=L<=K such that ai=bi for i<L and aL>bL

If there is no solution, simple output “Impossible”.

Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossible

题目大意:给三个正整数N、K、P,将N表示成K个正整数(可以相同,递减排列)的P次方和,如果有多种方案,选择底数n1+…+nk最大的方案,如果还有多种方案,选择底数序列的字典序最大的方案~

分析:dfs深度优先搜索。先把i从0开始所有的i的p次方的值存储在v[i]中,直到v[i] > n为止。然后深度优先搜索,记录当前正在相加的index(即v[i]的i的值),当前的总和tempSum,当前K的总个数tempK,以及因为题目中要求输出因子的和最大的那个,所以保存一个facSum为当前因子的和,让它和maxFacSum比较,如果比maxFacSum大就更新maxFacSum和要求的ans数组的值。
在ans数组里面存储因子的序列,tempAns为当前深度优先遍历而来的序列,从v[i]的最后一个index开始一直到index == 1,因为这样才能保证ans和tempAns数组里面保存的是从大到小的因子的顺序。一开始maxFacSum == -1,如果dfs后maxFacSum并没有被更新,还是-1,那么就输出Impossible,否则输出答案。

(PS:感谢github用户littlesevenmo提供的更优解)
分析:这道题考的是DFS+剪枝,我认为主要剪枝的地方有三个:
1. tempK==K但是tempSum!=n的时候需要剪枝
2. 在枚举的时候,按顺序枚举,上界或者下界可进行剪枝
3. 当且仅当tempSum + v[index] <= n时,进行下一层的DFS,而不要进入下一层DFS发现不满足条件再返回,这样开销会比较大~