-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
147 changed files
with
451 additions
and
5,053 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,27 @@ | ||
1002. A+B for Polynomials (25) | ||
This time, you are supposed to find A+B where A and B are two polynomials. | ||
|
||
Input | ||
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000. | ||
|
||
Output | ||
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place. | ||
|
||
Sample Input | ||
2 1 2.4 0 3.2 | ||
2 2 1.5 1 0.5 | ||
Sample Output | ||
3 2 1.5 1 2.9 0 3.2 | ||
|
||
#include <iostream> | ||
using namespace std; | ||
int main() { | ||
float a[1001], b[1001], c[1001]; | ||
for (int i = 0; i < 1001; i++) | ||
c[i] = 0.0; | ||
int m, n; | ||
cin >> m; | ||
float c[1001] = {0}; | ||
int m, n, t; | ||
float num; | ||
scanf("%d", &m); | ||
for (int i = 0; i < m; i++) { | ||
int t; | ||
cin >> t; | ||
cin >> a[t]; | ||
c[t] += a[t]; | ||
scanf("%d%f", &t, &num); | ||
c[t] += num; | ||
} | ||
cin >> n; | ||
scanf("%d", &n); | ||
for (int i = 0; i < n; i++) { | ||
int t; | ||
cin >> t; | ||
cin >> b[t]; | ||
c[t] += b[t]; | ||
scanf("%d%f", &t, &num); | ||
c[t] += num; | ||
} | ||
int count = 0; | ||
int cnt = 0; | ||
for (int i = 0; i < 1001; i++) { | ||
if (c[i] != 0) | ||
count++; | ||
if (c[i] != 0) cnt++; | ||
} | ||
cout << count; | ||
if (count != 0) | ||
cout << " "; | ||
int temp = 1001; | ||
printf("%d", cnt); | ||
for (int i = 1000; i >= 0; i--) { | ||
if (c[i] != 0.0 && count > 1) { | ||
cout << i << " "; | ||
printf("%.1f ", c[i]); | ||
count--; | ||
temp = i; | ||
} | ||
} | ||
for (int i = temp - 1; i >= 0; i--) { | ||
if (c[i] != 0.0) { | ||
cout << i << " "; | ||
printf("%.1f", c[i]); | ||
} | ||
if (c[i] != 0.0) | ||
printf(" %d %.1f", i, c[i]); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,15 @@ | ||
1005. Spell It Right (20) | ||
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. | ||
|
||
Input Specification: | ||
|
||
Each input file contains one test case. Each case occupies one line which contains an N (<= 10100). | ||
|
||
Output Specification: | ||
|
||
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line. | ||
|
||
Sample Input: | ||
12345 | ||
Sample Output: | ||
one five | ||
|
||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
int main() { | ||
string s[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; | ||
string a; | ||
cin >> a; | ||
int sum = 0; | ||
for(int i = 0; i < a.length(); i++) { | ||
for (int i = 0; i < a.length(); i++) | ||
sum += (a[i] - '0'); | ||
} | ||
vector<int> v; | ||
while(sum != 0) { | ||
v.push_back(sum % 10); | ||
sum = sum / 10; | ||
} | ||
int len = v.size(); | ||
if(len == 0) { // if sum == 0 print zero else print s[v[len - 1]] 否则会段错误 | ||
cout << s[0]; | ||
} else { | ||
cout << s[v[len - 1]]; | ||
} | ||
for(int i = len - 2; i >= 0; i--) { | ||
cout << " " << s[v[i]]; | ||
} | ||
string s = to_string(sum); | ||
string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; | ||
cout << arr[s[0] - '0']; | ||
for (int i = 1; i < s.length(); i++) | ||
cout << " " << arr[s[1] - '0']; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,24 @@ | ||
1007. Maximum Subsequence Sum (25) | ||
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20. | ||
|
||
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence. | ||
|
||
Input Specification: | ||
|
||
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space. | ||
|
||
Output Specification: | ||
|
||
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence. | ||
|
||
Sample Input: | ||
10 | ||
-10 1 2 3 4 -5 -23 3 7 -21 | ||
Sample Output: | ||
10 1 4 | ||
题目大意:求最大连续子序列和,输出最大的和以及这个子序列的开始值和结束值。如果所有数都小于0,那么认为最大的和为0,并且输出首尾元素。 | ||
分析:sum为要求的最大和,temp为临时最大和,left和right为所求的子序列的下标,tempindex标记left的临时下标。 | ||
temp = temp + v[i],当temp比sum大,就更新sum的值、left和right的值;当temp < 0,那么后面不管来什么值,都应该舍弃temp < 0前面的内容,因为负数对于总和只可能拉低总和,不可能增加总和,还不如舍弃; | ||
舍弃后,直接令temp = 0,并且同时更新left的临时值tempindex。因为对于所有的值都为负数的情况要输出0,第一个值,最后一个值,所以在输入的时候用flag判断是不是所有的数字都是小于0的,如果是,要在输入的时候特殊处理。~~~~ | ||
|
||
|
||
#include <cstdio> | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
int main() { | ||
int n, flag = 0, sum = -1, temp = 0, left = 0, right = 0, tempindex = 0; | ||
int n; | ||
scanf("%d", &n); | ||
vector<int> v(n); | ||
for(int i = 0; i < n; i++) { | ||
int leftindex = 0, rightindex = n - 1, sum = -1, temp = 0, tempindex = 0; | ||
for (int i = 0; i < n; i++) { | ||
scanf("%d", &v[i]); | ||
if(v[i] >= 0) | ||
flag = 1; | ||
temp = temp + v[i]; | ||
if(temp > sum) { | ||
sum = temp; | ||
left = tempindex; | ||
right = i; | ||
} else if(temp < 0) { | ||
if (temp < 0) { | ||
temp = 0; | ||
tempindex = i + 1; | ||
} else if (temp > sum) { | ||
sum = temp; | ||
leftindex = tempindex; | ||
rightindex = i; | ||
} | ||
} | ||
if(flag == 0) | ||
printf("0 %d %d", v[0], v[n - 1]); | ||
else | ||
printf("%d %d %d", sum, v[left], v[right]); | ||
if (sum < 0) sum = 0; | ||
printf("%d %d %d", sum, v[leftindex], v[rightindex]); | ||
return 0; | ||
} |
Oops, something went wrong.