1048 Find Coins (25 point(s))
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 105 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤105, the total number of coins) and M (≤103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1+V2=M and V1≤V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output No Solution
instead.
Sample Input 1:
8 15
1 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 14
1 8 7 2 4 11 15
Sample Output 2:
No Solution
题目大意:给一个数组,找到数组里不同下标的两个数使其等于目标值,并且让第一个数尽量小,然后输出,如果没有输出No Solution。
解题思路:和LeetCode第一题差不多的,暴力肯定超时,思路就是用哈希表了,没什么讲究的,排个序,从索引0开始遍历一波,然后哈希表存下索引和数值,然后从0开始遍历,如果哈希表里找得到目标值-当前数的记录,那么返回出来,如果找不到,就是nosolution,注意要判别一下找到的数不能是他自身,也就是说索引值不能相同。
代码如下:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
map<int, int> sol;
int num[100005];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> num[i];
}
sort(num, num + n);
for (int i = 0; i < n; i++) {
sol[num[i]] = i;
}
bool flag = false;
int start;
int end;
for (int i = 0; i < n; i++) {
if (sol.find(m - num[i]) != sol.end() && sol.find(m - num[i])->second != i) {
start = num[i];
end = sol.find(m - num[i])->first;
flag = true;
break;
}
}
if (!flag) {
cout << "No Solution" << endl;
}
else {
cout << start << " " << end << endl;
}
return 0;
}
1049 Counting Ones (30 point(s))
The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (≤230).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:
12
Sample Output:
5
题目大意:输入一个N,输出从1到N所有数字里面1出现的次数。
解题思路:一开始不信邪的我硬是暴力试了一遍,22分,其实考试的时候来不及的话这么搞一下也差不多了。但是平时练习,还是严肃的去总结一下规律,其实所谓效率都是相对于数据规模而言的,暴力在数据小的时候其实比所谓高效的算法是要来的快的,根据这个特点我们可以知道数据大超时的原因,我们需要让它减少计算,题目问1出现的次数,我后来的想法,把5,10,50,100,500,1000,······1100000000这样2倍5倍都暴力打表当然越细越快,统计出来,然后我们就可以得到每个区间的数目,这样来一个数,我可以直接转string看几位,然后找到那个小的项把前面的数累加,然后再从后面开始遍历。这样也会过的,但是后来去看了别人的解法,我觉得还是别人这样找数学规律好一点,具体就不说了,贴代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
int count = 0;
int b = 1;
int l, r, cur;
while (n / b) {
cur = n / b % 10;
l = n / (b * 10);
r = n%b;
if (cur == 0)count = count + l*b;
else if (cur == 1)count = count + l*b + r + 1;
else count += (l + 1)*b;
b = b * 10;
}
printf("%d\n", count);
return 0;
}