google在线測试练习题1
Problem
You receive a credit C
at a local store and would like to buy two items. You first walk through the store and create a list L
of
all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).
Input
The first line of input gives the number of cases, N. N test cases follow. For each test case there will be:
- One line containing the value C, the amount of credit you have at the store.
- One line containing the value I, the number of items in the store.
- One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
- Each test case will have exactly one solution.
Output
For each test case, output one line containing "Case #x: " followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.
Limits
5 ≤ C ≤ 1000
1 ≤ P ≤ 1000
Small dataset
N = 10
3 ≤ I ≤ 100
Large dataset
N = 50
3 ≤ I ≤ 2000
从文件输入。输出到文件里。
代码:
#include<iostream> #include<fstream> using namespace std; void quick_sort(int array[],int indexs[], int begin, int end) { if(end > begin) { int pivot = begin; int last_small = begin; int i = end; while(last_small != i) { if(array[i] <= array[pivot]) { int temp = array[i]; int tmp = indexs[i]; array[i] = array[++last_small]; array[last_small] = temp; indexs[i] = indexs[last_small]; indexs[last_small] = tmp; } else i--; } int tmp = array[pivot]; array[pivot] = array[last_small]; array[last_small] = tmp; int temp = indexs[pivot]; indexs[pivot] = indexs[last_small]; indexs[last_small] = temp; quick_sort(array, indexs, begin, last_small - 1); quick_sort(array, indexs, last_small + 1, end); } } int main() { int sum, n; int array[2005]; int n_case; ifstream file2("A-large-practice.in"); ofstream file1("resulta2.txt"); file2 >> n_case; for(int i_case = 1; i_case <= n_case; i_case++) { file2 >> sum; file2 >> n; for(int i = 0; i < n; i++) file2 >> array[i]; int indexs[2005]; for(int i = 0; i < n; i++) indexs[i] = i + 1; quick_sort(array, indexs, 0, n - 1);//sort the list int front = 0; int back = n - 1; while(true) { if(array[front] + array[back] == sum)//found break; else if(array[front] + array[back] > sum) back--; else front++; } if(indexs[front] > indexs[back]) { int tmp = indexs[front]; indexs[front] = indexs[back]; indexs[back] = tmp; } file1 << "Case #" << i_case << ": "; file1 << indexs[front] << ' ' << indexs[back] << endl; } system("pause"); return 0; }
相关文章
- 七年,Google数据中心从保守走向开放
- Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法
- Google大牛分享的面试秘籍
- Google Pagespeed,自动压缩优化JS/CSS/Image
- Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
- android SDK 更新问题完美解决 http://dl-ssl.google.com refused
- Google Chart API
- 图片压缩,用Google的这个工具就够了
- Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2
- ReplaceGoogleCDN:将 Google CDN 更换国家
- google云 重置
- android studio1.0 for Mac环境搭建与demo运行(手动下载gradle,科学上google) 转载
- 最长绝对文件路径——算法面试刷题1(google),字符串处理,使用tree遍历dfs类似思路
- 高效的多维空间点索引算法 — Geohash 和 Google S2