remove problem text and trim tailing spaces.

This commit is contained in:
Oliver Lew
2019-02-04 00:51:23 +08:00
parent 4ad08ab1e4
commit d71f4ddec7
109 changed files with 427 additions and 3940 deletions

View File

@@ -1,27 +1,3 @@
/**
* 1001. A+B Format (20)
*
* Calculate a + b and output the sum in standard format -- that is, the digits
* must be separated into groups of three by commas (unless there are less than
* four digits).
*
* Input
*
* Each input file contains one test case. Each case contains a pair of integers
* a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a
* space.
*
* Output
*
* For each test case, you should output the sum of a and b in one line. The sum
* must be written in the standard format.
*
* Sample Input
* -1000000 9
* Sample Output
* -999,991
*/
#include <stdio.h>
#include <string.h>
@@ -29,16 +5,16 @@ int main()
{
int a, b, pos;
char num[11];
scanf("%d%d", &a, &b);
sprintf(num, "%d", a + b);
for(pos = strlen(num) - 3; pos > 0 && num[pos - 1] != '-'; pos -= 3)
{
memmove(num + pos + 1, num + pos, strlen(num) - pos + 1);
num[pos] = ',';
}
puts(num);
return 0;
}

View File

@@ -1,29 +1,3 @@
/**
* 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 a_N1 N2 a_N2 ... NK a_NK,
* where K is the number of nonzero terms in the polynomial, Ni and a_Ni (i=1,
* 2, ..., K) are the exponents and coefficients, respectively. It is given that
* 1 <= K <= 100 <= 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 <stdio.h>
#include <math.h>
@@ -36,15 +10,15 @@ int main()
{
int KA, KB, Ksum = 0;
Poly A, B, sum;
scanf("%d", &KA);
for(int i = 0; i < KA; i++)
for(int i = 0; i < KA; i++)
scanf("%d %lf", &A[i].exp, &A[i].coef);
scanf("%d", &KB);
for(int i = 0; i < KB; i++)
for(int i = 0; i < KB; i++)
scanf("%d %lf", &B[i].exp, &B[i].coef);
int i = 0, j = 0;
while(i < KA || j < KB)
{
@@ -65,11 +39,11 @@ int main()
}
if(fabs(sum[Ksum].coef) >= 0.05) Ksum++;
}
printf("%d", Ksum);
for(int i = 0; i < Ksum; i++)
for(int i = 0; i < Ksum; i++)
printf(" %d %.1lf", sum[i].exp, sum[i].coef);
return 0;
}

View File

@@ -1,46 +1,3 @@
/**
* 1003. Emergency (25)
*
* As an emergency rescue team leader of a city, you are given a special map of
* your country. The map shows several scattered cities connected by some roads.
* Amount of rescue teams in each city and the length of each road between any
* pair of cities are marked on the map. When there is an emergency call to you
* from some other city, your job is to lead your men to the place as quickly as
* possible, and at the mean time, call up as many hands on the way as possible.
*
* Input
*
* Each input file contains one test case. For each test case, the first line
* contains 4 positive integers: N (<= 500) - the number of cities (and the
* cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the
* cities that you are currently in and that you must save, respectively. The
* next line contains N integers, where the i-th integer is the number of rescue
* teams in the i-th city. Then M lines follow, each describes a road with three
* integers c1, c2 and L, which are the pair of cities connected by a road and
* the length of that road, respectively. It is guaranteed that there exists at
* least one path from C1 to C2.
*
* Output
*
* For each test case, print in one line two numbers: the number of different
* shortest paths between C1 and C2, and the maximum amount of rescue teams you
* can possibly gather.
* All the numbers in a line must be separated by exactly one space, and there
* is no extra space allowed at the end of a line.
*
* Sample Input
* 5 6 0 2
* 1 2 1 5 3
* 0 1 1
* 0 2 2
* 0 3 1
* 1 2 1
* 2 4 1
* 3 4 1
* Sample Output
* 2 4
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
@@ -90,7 +47,7 @@ void Read(Graph G)
v->npath = 0;
v->adj = NULL;
}
int id1, id2, length;
for(int i = 0; i < G->nadj; i++)
{
@@ -110,7 +67,7 @@ void Read(Graph G)
}
}
/* Find the shortest path length using Dijkstra alg,
/* Find the shortest path length using Dijkstra alg,
* in the same time record the number of shortest paths and max rescue teams */
void ModifiedDijkstra(Graph G)
{
@@ -128,7 +85,7 @@ void ModifiedDijkstra(Graph G)
v = w;
}
if(v == NULL) break;
v->known = 1;
for(Adj e = v->adj; e; e = e->iter) if(!G->vs[e->id].known)
{
@@ -155,22 +112,22 @@ int main()
{
int N, M, C1, C2;
scanf("%d %d %d %d", &N, &M, &C1, &C2);
/* Create graph */
Vertexes vs = (Vertexes)malloc(N * sizeof(struct Vertex));
Adjs es = (Adjs)malloc(M * 2 * sizeof(struct Adj));
struct Graph sG = {.vs = vs, .es = es, .nvertex = N, .nadj = M * 2};
Graph G = &sG;
/* Read all the data and build the graph */
Read(G);
G->vs[C1].dist = 0;
G->vs[C1].npath = 1;
/* Find the shortest path and maximum rescue teams */
ModifiedDijkstra(G);
printf("%d %d", G->vs[C2].npath, G->vs[C2].totrescue);
return 0;
}

View File

@@ -1,38 +1,3 @@
/**
* 1004. Counting Leaves (30)
*
* A family hierarchy is usually presented by a pedigree tree. Your job is to
* count those family members who have no child.
*
* Input
*
* Each input file contains one test case. Each case starts with a line
* containing 0 < N < 100, the number of nodes in a tree, and M (< N), the
* number of non-leaf nodes. Then M lines follow, each in the format:
*
* ID K ID[1] ID[2] ... ID[K]
* where ID is a two-digit number representing a given non-leaf node, K is the
* number of its children, followed by a sequence of two-digit ID's of its
* children. For the sake of simplicity, let us fix the root ID to be 01.
*
* Output
*
* For each test case, you are supposed to count those family members who have
* no child for every seniority level starting from the root. The numbers must
* be printed in a line, separated by a space, and there must be no extra space
* at the end of each line.
*
* The sample case represents a tree with only 2 nodes, where 01 is the root and
* 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and
* on the next level, there is 1 leaf node. Then we should output "0 1" in a
* line.
*
* Sample Input
* 2 1
* 01 1 02
* Sample Output
* 0 1
*/
#include <stdio.h>
#define MAX 999
@@ -55,7 +20,7 @@ int main()
int N, M, ID, cID, K;
struct Member nodes[100];
struct Child children[100];
/* Read data and initiate a adjacent list */
scanf("%d %d", &N, &M);
for(int i = 1; i <= N; i++)
@@ -75,7 +40,7 @@ int main()
nodes[ID].child = &children[k];
}
}
/* For every level, find leaf nodes */
int n = N, count;
for(int level = 0; n; level++)
@@ -90,9 +55,9 @@ int main()
for(Child c = nodes[i].child; c; c = c->iter)
nodes[c->ID].level = level + 1;
}
printf("%d%c", count, n ? ' ' : '\0');
}
return 0;
}

View File

@@ -1,26 +1,3 @@
/**
* 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 <stdio.h>
int main()
@@ -30,15 +7,15 @@ int main()
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"
};
while((c = getchar()) != '\n')
sum += c - '0';
if(sum >= 100)
if(sum >= 100)
printf("%s ", digits[sum / 100]);
if(sum >= 10)
if(sum >= 10)
printf("%s ", digits[sum % 100 / 10]);
printf("%s", digits[sum % 10]);
return 0;
}

View File

@@ -1,40 +1,3 @@
/**
* 1006. Sign In and Sign Out (25)
*
* At the beginning of every day, the first person who signs in the computer
* room will unlock the door, and the last one who signs out will lock the door.
* Given the records of signing in's and out's, you are supposed to find the
* ones who have unlocked and locked the door on that day.
*
* Input Specification:
*
* Each input file contains one test case. Each case contains the records for
* one day. The case starts with a positive integer M, which is the total number
* of records, followed by M lines, each in the format:
*
* ID_number Sign_in_time Sign_out_time
* where times are given in the format HH:MM:SS, and ID number is a string with
* no more than 15 characters.
*
* Output Specification:
*
* For each test case, output in one line the ID numbers of the persons who have
* unlocked and locked the door on that day. The two ID numbers must be
* separated by one space.
*
* Note: It is guaranteed that the records are consistent. That is, the sign in
* time must be earlier than the sign out time for each person, and there are no
* two persons sign in or out at the same moment.
*
* Sample Input:
* 3
* CS301111 15:30:28 17:00:10
* SC3021234 08:00:00 11:25:25
* CS301133 21:45:00 21:58:40
* Sample Output:
* SC3021234 CS301133
*/
#include <stdio.h>
#include <string.h>
@@ -43,7 +6,7 @@ int main()
int N, HH, MM, SS;
int firsttime = 86400, lasttime = -1, time;
char firstname[16], lastname[16], name[16];
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
@@ -54,17 +17,17 @@ int main()
firsttime = time;
strcpy(firstname, name);
}
scanf("%d:%d:%d", &HH, &MM, &SS);
time = (HH * 60 + MM) * 60 + SS;
if(time > lasttime)
{
{
lasttime = time;
strcpy(lastname, name);
}
}
}
printf("%s %s", firstname, lastname);
return 0;
}

View File

@@ -1,38 +1,3 @@
/**
* 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
*/
#include <stdio.h>
int main()
@@ -40,25 +5,25 @@ int main()
int K, n, first;
int start, end, sum = -1; /* the current non-negative subsequence */
int a = -1, b, s = 0; /* the maximum-sum subsequence */
scanf("%d", &K);
for(int i = 0; i < K; i++)
{
scanf("%d", &n);
if(i == 0) first = n; /* record the first number */
/* update start, end and sum for the current non-negative subsequence */
if(sum < 0) start = n, sum = 0; /* reset if sum < 0 */
sum += n; /* update sum */
if(sum >= 0) end = n; /* update end if sum >= 0 */
/* update a, b and s for the so-far maximum-sum subsequence */
if(sum > s || (!sum && !s)) /* special case is max-sum is 0 */
a = start, b = end, s = sum;
}
/* a won't be updated if all the numbers are negative */
if(a == -1) printf("0 %d %d", first, n);
if(a == -1) printf("0 %d %d", first, n);
else printf("%d %d %d", s, a, b);
return 0;
}

View File

@@ -1,37 +1,8 @@
/**
* 1008. Elevator (20)
*
* The highest building in our city has only one elevator. A request list is
* made up with N positive numbers. The numbers denote at which floors the
* elevator will stop, in specified order. It costs 6 seconds to move the
* elevator up one floor, and 4 seconds to move down one floor. The elevator
* will stay for 5 seconds at each stop.
*
* For a given request list, you are to compute the total time spent to fulfill
* the requests on the list. The elevator is on the 0th floor at the beginning
* and does not have to return to the ground floor when the requests are
* fulfilled.
*
* Input Specification:
*
* Each input file contains one test case. Each case contains a positive integer
* N, followed by N positive numbers. All the numbers in the input are less than
* 100.
*
* Output Specification:
*
* For each test case, print the total time on a single line.
*
* Sample Input:
* 3 2 3 1
* Sample Output:
* 41
*/
#include <stdio.h>
int main()
{
int N, time, cur, pre;
scanf("%d", &N);
for(pre = 0, time = 0; N--; pre = cur)
{
@@ -39,8 +10,8 @@ int main()
if(cur > pre) time += (cur - pre) * 6 + 5;
else time += (pre - cur) * 4 + 5;
}
printf("%d", time);
return 0;
}

View File

@@ -1,29 +1,3 @@
/**
* 1009. Product of Polynomials (25)
*
* This time, you are supposed to find A*B where A and B are two polynomials.
*
* Input Specification:
*
* 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 Specification:
*
* For each test case you should output the product 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 up to 1 decimal place.
*
* Sample Input
* 2 1 2.4 0 3.2
* 2 2 1.5 1 0.5
* Sample Output
* 3 3 3.6 2 6.0 1 1.6
*/
#include <stdio.h>
int main()
{
@@ -38,13 +12,13 @@ int main()
for(int i = 0; i < 1001; i++)
for(int j = 0; j < 1001; j++)
MUL[i + j] += A[i] * B[j];
for(int i = 0; i < 2001; i++)
if(MUL[i]) count++;
printf("%d", count);
for(int i = 2000; i >= 0; i--) if(MUL[i])
for(int i = 2000; i >= 0; i--) if(MUL[i])
printf(" %d %.1f", i, MUL[i]);
return 0;
}

View File

@@ -1,39 +1,3 @@
/**
* 1010. Radix (25)
*
* 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
*/
#include <ctype.h>
#include <stdio.h>
#include <limits.h>
@@ -61,7 +25,7 @@ long long base10(char *s, long long radix)
int minradix(char *s)
{ /* Simply the largest digit in the number plus 1 */
char r;
for(r = '0'; *s; s++)
for(r = '0'; *s; s++)
if(*s > r)
r = *s;
return CBASE10(r) + 1;
@@ -74,7 +38,7 @@ long long binsearch(char *s, long long n, long long rmin, long long rmax)
while(rmax >= rmin)
{
r = rmin + (rmax - rmin) / 2; /* avoid (rmin + rmax) overflow */
if((m = base10(s, r)) > n || m == OVERFLOW)
if((m = base10(s, r)) > n || m == OVERFLOW)
rmax = r - 1;
else if(m < n)
rmin = r + 1;
@@ -89,12 +53,12 @@ int main()
int tag, radix;
long long N1, rmin, rmax, r;
char buf1[11], buf2[11], *S1, *S2;
/* Make S1 point to the number with known radix, S2 to the other */
scanf("%s %s %d %d", buf1, buf2, &tag, &radix);
if(tag == 1) S1 = buf1, S2 = buf2;
if(tag == 2) S1 = buf2, S2 = buf1;
N1 = base10(S1, radix); /* Corresponding decimal of S1 */
rmin = minradix(S2); /* Smallest possible radix of S2 */
rmax = LLONG_MAX; /* Largest possible radix of S2 */
@@ -109,6 +73,6 @@ int main()
if(r != NOTFOUNT) printf("%lld", r);
else printf("Impossible");
}
return 0;
}

View File

@@ -1,56 +1,10 @@
/**
* 1011. World Cup Betting (20)
*
* With the 2010 FIFA World Cup running, football fans the world over were
* becoming increasingly excited as the best players from the best teams doing
* battles for the World Cup trophy in South Africa. Similarly, football betting
* fans were putting their money where their mouths were, by laying all manner
* of World Cup bets.
*
* Chinese Football Lottery provided a "Triple Winning" game. The rule of
* winning was simple: first select any three of the games. Then for each
* selected game, bet on one of the three possible results -- namely W for win,
* T for tie, and L for lose. There was an odd assigned to each result. The
* winner's odd would be the product of the three odds times 65%.
*
* For example, 3 games' odds are given as the following:
*
* W T L
* 1.1 2.5 1.7
* 1.2 3.0 1.6
* 4.1 1.2 1.1
* To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd
* game, and T for the 1st game. If each bet takes 2 yuans, then the maximum
* profit would be (4.1*3.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal
* places).
*
* Input
*
* Each input file contains one test case. Each case contains the betting
* information of 3 games. Each game occupies a line with three distinct odds
* corresponding to W, T and L.
*
* Output
*
* For each test case, print in one line the best bet of each game, and the
* maximum profit accurate up to 2 decimal places. The characters and the number
* must be separated by one space.
*
* Sample Input
* 1.1 2.5 1.7
* 1.2 3.0 1.6
* 4.1 1.2 1.1
* Sample Output
* T T W 37.98
*/
#include <stdio.h>
int main()
{
char c;
float odd, maxodd, maxprofit = 1;
for(int i = 0; i < 3; i++)
{
maxodd = 0;
@@ -67,6 +21,6 @@ int main()
maxprofit *= maxodd;
}
printf("%.2f", (maxprofit * 0.65 - 1) * 2);
return 0;
}

View File

@@ -1,68 +1,3 @@
/**
* 1012. The Best Rank (25)
*
* To evaluate the performance of our first year CS majored students, we
* consider their grades of three courses only: C - C Programming Language, M -
* Mathematics (Calculus or Linear Algebra), and E - English. At the mean time,
* we encourage students by emphasizing on their best ranks -- that is, among
* the four ranks with respect to the three courses and the average grade, we
* print the best rank for each student.
*
* For example, The grades of C, M, E and A - Average of 4 students are given as
* the following:
*
* StudentID C M E A
* 310101 98 85 88 90
* 310102 70 95 88 84
* 310103 82 87 94 88
* 310104 91 91 91 91
* Then the best ranks for all the students are No.1 since the 1st one has done
* the best in C Programming Language, while the 2nd one in Mathematics, the 3rd
* one in English, and the last one in average.
*
* Input
*
* Each input file contains one test case. Each case starts with a line
* containing 2 numbers N and M (<=2000), which are the total number of
* students, and the number of students who would check their ranks,
* respectively. Then N lines follow, each contains a student ID which is a
* string of 6 digits, followed by the three integer grades (in the range of
* [0, 100]) of that student in the order of C, M and E. Then there are M lines,
* each containing a student ID.
*
* Output
*
* For each of the M students, print in one line the best rank for him/her, and
* the symbol of the corresponding rank, separated by a space.
*
* The priorities of the ranking methods are ordered as A > C > M > E. Hence if
* there are two or more ways for a student to obtain the same best rank, output
* the one with the highest priority.
*
* If a student is not on the grading list, simply output "N/A".
*
* Sample Input
* 5 6
* 310101 98 85 88
* 310102 70 95 88
* 310103 82 87 94
* 310104 91 91 91
* 310105 85 90 90
* 310101
* 310102
* 310103
* 310104
* 310105
* 999999
* Sample Output
* 1 C
* 1 M
* 1 E
* 1 A
* 3 A
* N/A
*/
#include <stdio.h>
typedef struct Student{ int ID, score[4]; } Student;
@@ -71,29 +6,29 @@ int main()
{
/* scores array stores the number of students of any score */
int N, M, ID, scores[4][102] = {{0}};
/* Setting a score of 101 is to make it easy to calculate rank */
scores[0][101] = scores[1][101] = scores[2][101] = scores[3][101] = 1;
Student students[2000];
scanf("%d %d", &N, &M);
for(int i = 0; i < N; i++)
{ /* record all the scores for every student: A, C, M, E */
Student *s = students + i;
scanf("%d %d %d %d", &s->ID, &s->score[1], &s->score[2], &s->score[3]);
s->score[0] = (s->score[1] + s->score[2] + s->score[3] + 1) / 3;
s->score[0] = (s->score[1] + s->score[2] + s->score[3] + 1) / 3;
for(int j = 0; j < 4; j++) /* +1 for rounding */
scores[j][s->score[j]]++; /* record how many got this score */
}
for(int i = 0; i < M; i++)
{
int max = 3, ranks[4] = {0}, stu;
scanf("%d", &ID);
for(stu = 0; stu < N && students[stu].ID != ID; stu++) ; /* find */
if(stu == N) { puts("N/A"); continue;}
for(int j = 3; j >= 0; j--)
{ /* calculate the rank: sum the counts from score + 1 to 101 */
/* e.g. score 100 will have rank 1, since scores[101] is 1 */

View File

@@ -1,43 +1,3 @@
/**
* 1013. Battle Over Cities (25)
*
* It is vitally important to have all the cities connected by highways in a
* war. If a city is occupied by the enemy, all the highways from/toward that
* city are closed. We must know immediately if we need to repair any other
* highways to keep the rest of the cities connected. Given the map of cities
* which have all the remaining highways marked, you are supposed to tell the
* number of highways need to be repaired, quickly.
*
* For example, if we have 3 cities and 2 highways connecting city1-city2 and
* city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway
* repaired, that is the highway city2-city3.
*
* Input
*
* Each input file contains one test case. Each case starts with a line
* containing 3 numbers N (<1000), M and K, which are the total number of
* cities, the number of remaining highways, and the number of cities to be
* checked, respectively. Then M lines follow, each describes a highway by 2
* integers, which are the numbers of the cities the highway connects. The
* cities are numbered from 1 to N. Finally there is a line containing K
* numbers, which represent the cities we concern.
*
* Output
*
* For each of the K cities, output in a line the number of highways need to be
* repaired if that city is lost.
*
* Sample Input
* 3 2 3
* 1 2
* 1 3
* 1 2 3
* Sample Output
* 1
* 0
* 0
*/
#include <stdio.h>
#define MAX 1000

View File

@@ -1,70 +1,3 @@
/**
* 1014. Waiting in Line (30)
*
* Suppose a bank has N windows open for service. There is a yellow line in
* front of the windows which devides the waiting area into two parts. The rules
* for the customers to wait in line are:
*
* The space inside the yellow line in front of each window is enough to
* contain a line with M customers. Hence when all the N lines are full, all the
* customers after (and including) the (NM+1)st one will have to wait in a line
* behind the yellow line.
* Each customer will choose the shortest line to wait in when crossing the
* yellow line. If there are two or more lines with the same length, the
* customer will always choose the window with the smallest number.
* Customer[i] will take T[i] minutes to have his/her transaction processed.
* The first N customers are assumed to be served at 8:00am.
*
* Now given the processing time of each customer, you are supposed to tell the
* exact time at which a customer has his/her business done.
*
* For example, suppose that a bank has 2 windows and each window may have 2
* custmers waiting inside the yellow line. There are 5 customers waiting with
* transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the
* morning, customer1 is served at window1 while customer2 is served at window2.
* Customer3 will wait in front of window1 and customer4 will wait in front of
* window2. Customer5 will wait behind the yellow line.
*
* At 08:01, customer1 is done and customer5 enters the line in front of window1
* since that line seems shorter now. Customer2 will leave at 08:02, customer4
* at 08:06, customer3 at 08:07, and finally customer5 at 08:10.
*
* Input
*
* Each input file contains one test case. Each case starts with a line
* containing 4 positive integers: N (<=20, number of windows), M (<=10, the
* maximum capacity of each line inside the yellow line), K (<=1000, number of
* customers), and Q (<=1000, number of customer queries).
*
* The next line contains K positive integers, which are the processing time of
* the K customers.
*
* The last line contains Q positive integers, which represent the customers who
* are asking about the time they can have their transactions done. The
* customers are numbered from 1 to K.
*
* Output
*
* For each of the Q customers, print in one line the time at which his/her
* transaction is finished, in the format HH:MM where HH is in [08, 17] and MM
* is in [00, 59]. Note that since the bank is closed everyday after 17:00, for
* those customers who cannot be served before 17:00, you must output "Sorry"
* instead.
* Sample Input
*
* 2 2 7 5
* 1 2 6 4 3 534 2
* 3 4 5 6 7
*
* Sample Output
*
* 08:07
* 08:06
* 08:10
* 17:00
* Sorry
**/
#include <stdio.h>
#define LATE_FLAG -1
@@ -77,11 +10,11 @@ int main()
int N, M, K, Q, query;
int time[1000], queue[20][11] = {{0}};
int front[20] = {0}, rear[20] = {0}, length[20] = {0};
scanf("%d %d %d %d", &N, &M, &K, &Q);
for(int i = 1; i <= K; i++)
scanf("%d", time + i);
/* Total number of operations */
int count = (K < M * N) ? (2 * K) : (K + M * N);
/* Doing dequeues and enqueues for every customer */
@@ -121,7 +54,7 @@ int main()
length[shortest]++;
}
}
/* Read queries and print answers */
for(int i = 0; i < Q; i++)
{
@@ -131,6 +64,6 @@ int main()
else
printf("Sorry\n");
}
return 0;
}

View File

@@ -1,35 +1,3 @@
/**
* 1015. Reversible Primes (20)
*
* A reversible prime in any number system is a prime whose "reverse" in
* that number system is also a prime. For example in the decimal system
* 73 is a reversible prime because its reverse 37 is also a prime.
*
* Now given any two positive integers N (< 105) and D (1 < D <= 10),
* you are supposed to tell if N is a reversible prime with radix D.
*
* Input Specification:
*
* The input file consists of several test cases. Each case occupies a
* line which contains two integers N and D. The input is finished by a
* negative N.
*
* Output Specification:
*
* For each test case, print in one line "Yes" if N is a reversible
* prime with radix D, or "No" if not.
*
* Sample Input:
* 73 10
* 23 2
* 23 10
* -2
* Sample Output:
* Yes
* Yes
* No
*/
#include <stdio.h>
int iPrime(int N)
@@ -46,8 +14,8 @@ int Rev(int N, int D)
{
int Nrev;
for(Nrev = 0; N; N /= D)
{
Nrev *= D;
{
Nrev *= D;
Nrev += N % D;
}
return Nrev;

View File

@@ -1,80 +1,3 @@
/**
* 1016. Phone Bills (25)
*
* A long-distance telephone company charges its customers by the following
* rules:
*
* Making a long-distance call costs a certain amount per minute, depending on
* the time of day when the call is made. When a customer starts connecting a
* long-distance call, the time will be recorded, and so will be the time when
* the customer hangs up the phone. Every calendar month, a bill is sent to the
* customer for each minute called (at a rate determined by the time of day).
* Your job is to prepare the bills for each month, given a set of phone call
* records.
*
* Input Specification:
*
* Each input file contains one test case. Each case has two parts: the rate
* structure, and the phone call records.
*
* The rate structure consists of a line with 24 non-negative integers denoting
* the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and
* so on for each hour in the day.
*
* The next line contains a positive number N (<= 1000), followed by N lines of
* records. Each phone call record consists of the name of the customer (string
* of up to 20 characters without space), the time and date (mm:dd:hh:mm), and
* the word "on-line" or "off-line".
*
* For each test case, all dates will be within a single month. Each "on-line"
* record is paired with the chronologically next record for the same customer
* provided it is an "off-line" record. Any "on-line" records that are not
* paired with an "off-line" record are ignored, as are "off-line" records not
* paired with an "on-line" record. It is guaranteed that at least one call is
* well paired in the input. You may assume that no two records for the same
* customer have the same time. Times are recorded using a 24-hour clock.
*
* Output Specification:
*
* For each test case, you must print a phone bill for each customer.
*
* Bills must be printed in alphabetical order of customers' names. For each
* customer, first print in a line the name of the customer and the month of
* the bill in the format shown by the sample. Then for each time period of a
* call, print in one line the beginning and ending time and date (dd:hh:mm),
* the lasting time (in minute) and the charge of the call. The calls must be
* listed in chronological order. Finally, print the total charge for the month
* in the format shown by the sample.
*
* Sample Input:
*
* 10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
* 10
* CYLL 01:01:06:01 on-line
* CYLL 01:28:16:05 off-line
* CYJJ 01:01:07:00 off-line
* CYLL 01:01:08:03 off-line
* CYJJ 01:01:05:59 on-line
* aaa 01:01:01:03 on-line
* aaa 01:02:00:01 on-line
* CYLL 01:28:15:41 on-line
* aaa 01:05:02:24 on-line
* aaa 01:04:23:59 off-line
*
* Sample Output:
*
* CYJJ 01
* 01:05:59 01:07:00 61 $12.10
* Total amount: $12.10
* CYLL 01
* 01:06:01 01:08:03 122 $24.40
* 28:15:41 28:16:05 24 $3.85
* Total amount: $28.25
* aaa 01
* 02:00:01 04:23:59 4318 $638.80
* Total amount: $638.80
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -89,7 +12,7 @@ typedef struct {
int cmp(const void *record1, const void *record2)
{
pRecord r1 = *(pRecord*)record1, r2 = *(pRecord*)record2;
return strcmp(r1->name, r2->name) ?
return strcmp(r1->name, r2->name) ?
strcmp(r1->name, r2->name) : r1->time - r2->time;
}
@@ -97,14 +20,14 @@ int cmp(const void *record1, const void *record2)
int calccharge(pRecord p1, pRecord p2, int toll[])
{
int charge = 0, start = p1->time, end = p2->time, h, time1, time2;
for(time1 = start; time1 < end; time1 = time2)
{ /* Add the charge hour by hour */
time2 = (time1 / 60 + 1) * 60; /* time2 will be the time of next hour */
h = time1 / 60 % 24; /* h will be the index of the hour */
charge += ((time2 > end ? end : time2) - time1) * toll[h];
}
return charge;
}
@@ -114,7 +37,7 @@ int main()
int N, toll[24], charge, charge_total = 0;
Record records[1001] = {0};
pRecord precords[1001] = {0}, *p = precords;
/* Read data */
for(int i = 0; i < 24; i++)
scanf("%d", toll + i);
@@ -122,18 +45,18 @@ int main()
for(int i = 0; i < N; i++, p++)
{
*p = records + i;
scanf("%s %d:%d:%d:%d %s", (*p)->name,
scanf("%s %d:%d:%d:%d %s", (*p)->name,
&(*p)->month, &(*p)->day, &(*p)->hour, &(*p)->min, state);
(*p)->time = ((*p)->day * 24 + (*p)->hour) * 60 + (*p)->min;
(*p)->state = strcmp(state, "on-line") ? 0 : 1;
}
/* Sort first by name, then by date and time */
qsort(precords, N, sizeof(pRecord), cmp);
/* Print phone bill one by one */
for(p = precords + 1; *p; p++)
{
{
if(strcmp((*p)->name, (*(p - 1))->name))
{ /* A new customer, print last total amount if any */
if(charge_total)
@@ -147,14 +70,14 @@ int main()
charge = calccharge(*(p - 1), *p, toll);
charge_total += charge;
/* Print info of this call */
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
(*(p - 1))->day, (*(p - 1))->hour, (*(p - 1))->min,
(*p)->day, (*p)->hour, (*p)->min,
(*p)->time - (*(p - 1))->time, charge * 1e-2);
(*p)->time - (*(p - 1))->time, charge * 1e-2);
}
}
if(charge_total)
printf("Total amount: $%.2f\n", charge_total * 1e-2);
return 0;
}

View File

@@ -1,48 +1,3 @@
/**
* 1017. Queueing at Bank (25)
*
* Suppose a bank has K windows open for service. There is a yellow line in
* front of the windows which devides the waiting area into two parts. All the
* customers have to wait in line behind the yellow line, until it is his/her
* turn to be served and there is a window available. It is assumed that no
* window can be occupied by a single customer for more than 1 hour.
*
* Now given the arriving time T and the processing time P of each customer, you
* are supposed to tell the average waiting time of all the customers.
*
* Input Specification:
*
* Each input file contains one test case. For each case, the first line
* contains 2 numbers: N (<=10000) - the total number of customers, and K
* (<=100) - the number of windows. Then N lines follow, each contains 2 times:
* HH:MM:SS - the arriving time, and P - the processing time in minutes of a
* customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59].
* It is assumed that no two customers arrives at the same time.
*
* Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will
* have to wait in line till 08:00, and anyone comes too late (at or after
* 17:00:01) will not be served nor counted into the average.
*
* Output Specification:
*
* For each test case, print in one line the average waiting time of all the
* customers, in minutes and accurate up to 1 decimal place.
* Sample Input:
*
* 7 3
* 07:55:00 16
* 17:00:01 2
* 07:59:59 15
* 08:01:00 60
* 08:00:00 30
* 08:00:02 2
* 08:03:00 10
*
* Sample Output:
*
* 8.2
**/
#include <stdio.h>
#include <stdlib.h>
@@ -64,7 +19,7 @@ int main()
int HH, MM, SS;
int wait_time = 0, queue_time[100] = {0};
Customer customers[10000], *p;
scanf("%d %d", &N, &K);
for(int i = 0; i < N; i++)
{
@@ -73,26 +28,26 @@ int main()
customers[i].start = SS + 60 * (MM + 60 * (HH - 8));
customers[i].len *= 60;
}
qsort(customers, N, sizeof(Customer), cmp);
for(i = 0; i < N; i++)
{
p = customers + i;
/* Find the queue number which will finish next */
earliest = 0;
for(int i = 0; i < K; i++)
if(queue_time[i] < queue_time[earliest])
earliest = i;
/* later than 17:00:00 */
if(p->start > (17 - 8) * 3600)
break;
/* processing time longer than one hour */
if(p->len > 3600)
p->len = 3600;
/* increase total waiting time and modify the time of each queue */
if(p->start < queue_time[earliest])
{
@@ -104,11 +59,11 @@ int main()
queue_time[earliest] = p->start + p->len;
}
}
if(i)
printf("%.1f", wait_time / 60.0 / i);
else
printf("0.0");
return 0;
}

View File

@@ -1,52 +1,3 @@
/**
* 1019. General Palindromic Number (20)
*
* A number that will be the same when it is written forwards or backwards is
* known as a Palindromic Number. For example, 1234321 is a palindromic number.
* All single digit numbers are palindromic numbers.
*
* Although palindromic numbers are most often considered in the decimal system,
* the concept of palindromicity can be applied to the natural numbers in any
* numeral system. Consider a number N > 0 in base b >= 2, where it is written
* in standard notation with k+1 digits ai as the sum of (aibi) for i from 0 to
* k. Here, as usual, 0 <= ai < b for all i and ak is non-zero. Then N is
* palindromic if and only if ai = ak-i for all i. Zero is written 0 in any base
* and is also palindromic by definition.
*
* Given any non-negative decimal integer N and a base b, you are supposed to
* tell if N is a palindromic number in base b.
*
* Input Specification:
*
* Each input file contains one test case. Each case consists of two non-
* negative numbers N and b, where 0 <= N <= 109 is the decimal number and
* 2 <= b <= 109 is the base. The numbers are separated by a space.
*
* Output Specification:
*
* For each test case, first print in one line "Yes" if N is a palindromic
* number in base b, or "No" if not. Then in the next line, print N as the
* number in base b in the form "ak ak-1 ... a0". Notice that there must be no
* extra space at the end of output.
* Sample Input 1:
*
* 27 2
*
* Sample Output 1:
*
* Yes
* 1 1 0 1 1
*
* Sample Input 2:
*
* 121 5
*
* Sample Output 2:
*
* No
* 4 4 1
**/
#include <stdio.h>
int main()

View File

@@ -1,33 +1,3 @@
/**
* 1020. Tree Traversals (25)
*
* Suppose that all the keys in a binary tree are distinct positive integers.
* Given the postorder and inorder traversal sequences, you are supposed to
* output the level order traversal sequence of the corresponding binary tree.
*
* Input Specification:
*
* Each input file contains one test case. For each case, the first line gives a
* positive integer N (<=30), the total number of nodes in the binary tree. The
* second line gives the postorder sequence and the third line gives the inorder
* sequence. All the numbers in a line are separated by a space.
*
* Output Specification:
*
* For each test case, print in one line the level order traversal sequence of
* the corresponding binary tree. All the numbers in a line must be separated by
* exactly one space, and there must be no extra space at the end of the line.
* Sample Input:
*
* 7
* 2 3 1 5 7 6 4
* 1 2 3 4 5 6 7
*
* Sample Output:
*
* 4 1 6 3 5 7 2
**/
#include <stdio.h>
#define QLEN 15
@@ -62,13 +32,13 @@ int main()
int post[CNODE] = {0}, in[CNODE] = {0}, N;
queue q = {0}; /* Initialize to zeros or NULL array */
int root, index, count;
scanf("%d", &N);
for(int i = 0; i < N; i++)
scanf("%d", post + i);
for(int i = 0; i < N; i++)
scanf("%d", in + i);
node nodes[CNODE] = {{post, in, N}}, *p = nodes, *n;
enqueue(&q, p++);
for(count = 0; q.count; count++)
@@ -95,6 +65,6 @@ int main()
/* print */
printf("%d%c", root, (count == N - 1) ? '\0' : ' ');
}
return 0;
}

View File

@@ -1,41 +1,17 @@
/**
* 1001. 害死人不偿命的(3n+1)猜想
* 卡拉兹(Callatz)猜想:
*
* 对任何一个自然数n如果它是偶数那么把它砍掉一半如果它是奇数那么把(3n+1)砍
* 掉一半。这样一直反复砍下去最后一定在某一步得到n=1。卡拉兹在1950年的世界数学家
* 大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的
* 命题,结果闹得学生们无心学业,一心只证(3n+1),以至于有人说这是一个阴谋,卡拉兹是
* 在蓄意延缓美国数学界教学与科研的进展……
*
* 我们今天的题目不是证明卡拉兹猜想而是对给定的任一不超过1000的正整数n简单地数
* 一下需要多少步砍几下才能得到n=1
*
* 输入格式每个测试输入包含1个测试用例即给出自然数n的值。
*
* 输出格式输出从n计算到1需要的步数。
*
* 输入样例:
* 3
*
* 输出样例:
* 5
**/
#include <stdio.h>
int main()
{
int number, steps;
scanf("%d", &number);
for(steps = 0; number != 1; steps++)
if(number % 2 == 0)
for(steps = 0; number != 1; steps++)
if(number % 2 == 0)
number /= 2;
else
number = (3 * number + 1) / 2;
printf("%d", steps);
return 0;
}

View File

@@ -1,35 +1,19 @@
/**
* 1002. 写出这个数
* 读入一个自然数n计算其各位数字之和用汉语拼音写出和的每一位数字。
*
* 输入格式每个测试输入包含1个测试用例即给出自然数n的值。这里保证n小于10^100。
*
* 输出格式在一行内输出n的各位数字之和的每一位拼音数字间有1 空格,但一行中最后
* 一个拼音数字后没有空格。
*
* 输入样例:
* 1234567890987654321123456789
*
* 输出样例:
* yi san wu
**/
#include <stdio.h>
int main()
{
int sum = 0;
char c, *pinyins[] = {"ling", "yi", "er", "san", "si",
char c, *pinyins[] = {"ling", "yi", "er", "san", "si",
"wu", "liu", "qi", "ba", "jiu"};
while((c = getchar()) != '\n')
while((c = getchar()) != '\n')
sum += c - '0';
if(sum / 100) /* hundreds */
printf("%s ", pinyins[sum / 100]);
if(sum / 10) /* tens */
printf("%s ", pinyins[sum / 10 % 10]);
printf("%s", pinyins[sum % 10]); /* units */
return 0;
}

View File

@@ -1,45 +1,3 @@
/**
* 1003. 我要通过!
* “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送
* —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
*
* 得到“答案正确”的条件是:
*
* 1. 字符串中必须仅有P, A, T这三种字符不可以包含其它字符
* 2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者
* 是仅由字母 A 组成的字符串;
* 3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符
* 串,或者是仅由字母 A 组成的字符串。
*
* 现在就请你为PAT写一个自动裁判程序判定哪些字符串是可以获得“答案正确”的。
* 输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测
* 的字符串个数。接下来每个字符串占一行字符串长度不超过100且不包含空格。
*
* 输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出
* YES否则输出NO。
*
* 输入样例:
* 8
* PAT
* PAAT
* AAPATAA
* AAPAATAAAA
* xPATx
* PT
* Whatever
* APAAATAA
*
* 输出样例:
* YES
* YES
* YES
* YES
* NO
* NO
* NO
* NO
**/
#include <stdio.h>
int main()
@@ -47,7 +5,7 @@ int main()
char c;
int num;
scanf("%d", &num);
while(getchar() != '\n'); /* read the rest of the line,
while(getchar() != '\n'); /* read the rest of the line,
make sure to start from a new line later */
for(int i = 0; i < num; i++)
{
@@ -61,19 +19,19 @@ int main()
else if(c == 'T' && pos == 1) pos = 2; /* one T after P */
else break; /* 'wrong' string */
}
if(c == '\n' /* 1. no other characters at end */
&& pos == 2 /* 2. appearance of 'P' and 'T' */
&& count[1] /* 3. existance of 'A' */
&& count[2] == count[1] * count[0]) /* 4. relation between numbers */
&& count[2] == count[1] * count[0]) /* 4. relation between numbers */
puts("YES");
else
puts("NO");
/* read the rest of the line */
if(c != '\n')
while(getchar() != '\n');
if(c != '\n')
while(getchar() != '\n');
}
return 0;
}

View File

@@ -1,33 +1,3 @@
/**
* 1004. 成绩排名
*
* 读入n名学生的姓名、学号、成绩分别输出成绩最高和成绩最低学生的姓名和学号。
*
* 输入格式每个测试输入包含1个测试用例格式为
*
* 第1行正整数n
* 第2行第1个学生的姓名 学号 成绩
* 第3行第2个学生的姓名 学号 成绩
* ... ... ...
* 第n+1行第n个学生的姓名 学号 成绩
*
* 其中姓名和学号均为不超过10个字符的字符串成绩为0到100之间的一个整数这里保证在
* 一组测试用例中没有两个学生的成绩是相同的。
*
* 输出格式对每个测试用例输出2行第1行是成绩最高学生的姓名和学号第2行是成绩最
* 低学生的姓名和学号字符串间有1空格。
*
* 输入样例:
* 3
* Joe Math990112 89
* Mike CS991301 100
* Mary EE990830 95
*
* 输出样例:
* Mike CS991301
* Joe Math990112
**/
#include <stdio.h>
#include <string.h>
@@ -35,15 +5,15 @@ int main()
{
int N;
scanf("%d", &N);
char maxname[11], minname[11], curname[11],
char maxname[11], minname[11], curname[11],
maxid[11], minid[11], curid[11];
int maxgrade = -1, mingrade = 101, curgrade;
for(int i = 0; i < N; i++)
{
scanf("%s %s %d", curname, curid, &curgrade);
if(curgrade > maxgrade)
{
strcpy(maxname, curname);
@@ -57,8 +27,8 @@ int main()
mingrade = curgrade;
}
}
printf("%s %s\n%s %s", maxname, maxid, minname, minid);
return 0;
}

View File

@@ -1,53 +1,25 @@
/**
* 1005. 继续(3n+1)猜想
*
* 卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里情况稍微有些复杂。
*
* 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。
* 例如对n=3进行验证的时候我们需要计算3、5、8、4、2、1则当我们对n=5、8、4、2
* 进行验证的时候就可以直接判定卡拉兹猜想的真伪而不需要重复计算因为这4个数已经
* 在验证3的时候遇到过了我们称5、8、4、2是被3“覆盖”的数。我们称一个数列中的某个
* 数n为“关键数”如果n不能被数列中的其他数字所覆盖。
*
* 现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证
* 余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。
*
* 输入格式每个测试输入包含1个测试用例第1行给出一个正整数K(<100)第2行给出K个
* 互不相同的待验证的正整数n(1<n<=100)的值,数字间用空格隔开。
*
* 输出格式每个测试用例的输出占一行按从大到小的顺序输出关键数字。数字间用1个
* 空格隔开,但一行中最后一个数字后没有空格。
*
* 输入样例:
* 6
* 3 5 6 7 8 11
*
* 输出样例:
* 7 6
**/
#include <stdio.h>
int main()
{
int K, n, tabel[101] = {0};
scanf("%d", &K);
for(int i = 0; i < K; i++)
{
scanf("%d", &n);
tabel[n] = 1;
}
/* find numbers needed to test */
for(int i = 1; i <= 100; i++)
for(int i = 1; i <= 100; i++)
if(tabel[i])
for(int j = i; j > 1; )
{
/* calculate for one step */
if(j % 2) j = (3 * j + 1) / 2;
else j /= 2;
/* see if the new number is in given numbers */
if(j <= 100 && tabel[j])
{
@@ -56,10 +28,10 @@ int main()
if(j < i) break; /* did this before, no need going on */
}
}
for(int i = 100; i >= 1; i--)
for(int i = 100; i >= 1; i--)
if(tabel[i] == 1)
printf("%d%c", i, --K ? ' ' : '\0');
return 0;
}

View File

@@ -1,26 +1,3 @@
/**
* 1006. 换个格式输出整数
* 让我们用字母B来表示“百”、字母S表示“十”用“12...n”来表示个位数字n<10换个
* 格式来输出任一个不超过3位的正整数。例如234应该被输出为BBSSS1234因为它有2个
* “百”、3个“十”、以及个位的4。
*
* 输入格式每个测试输入包含1个测试用例给出正整数n<1000
*
* 输出格式每个测试用例的输出占一行用规定的格式输出n。
*
* 输入样例1
* 234
*
* 输出样例1
* BBSSS1234
*
* 输入样例2
* 23
*
* 输出样例2
* SS123
**/
#include <stdio.h>
int main()

View File

@@ -1,22 +1,3 @@
/**
* 1007. 素数对猜想
*
* 让我们定义 dn 为dn = pn+1 - pn其中 pi 是第i个素数。显然有 d1=1 且对于n>1有
* dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
*
* 现给定任意正整数N (< 10^5)请计算不超过N的满足猜想的素数对的个数。
*
* 输入格式每个测试输入包含1个测试用例给出正整数N。
*
* 输出格式每个测试用例的输出占一行不超过N的满足猜想的素数对的个数。
*
* 输入样例:
* 20
*
* 输出样例:
* 4
**/
#include <stdio.h>
int main()
@@ -29,28 +10,28 @@ int main()
int primes[100] = {2, 3}; /* Record the prime numbers before sqrt(10^5) */
int twincount = 0; /* Count of twin primes */
int primecount = 2; /* Count of prime numbers */
/* Start from 4 */
for(int i = 4; i <= N; i++)
{
/* Test if i is a prime number */
iPrime = 1;
for(int j = 0; iPrime && primes[j] * primes[j] <= i; j++)
for(int j = 0; iPrime && primes[j] * primes[j] <= i; j++)
if(i % primes[j] == 0)
iPrime = 0;
/* If i is a prime number, record */
if(iPrime)
{
if(primecount < 100) primes[primecount++] = i;
if(iPrimeMinus2 == 1) twincount++; /* a prime pair found */
}
/* Shift the primality flags to next numbers */
iPrimeMinus2 = iPrimeMinus1;
iPrimeMinus1 = iPrime;
}
printf("%d", twincount);
return 0;
}

View File

@@ -1,25 +1,3 @@
/**
* 1008. 数组元素循环右移问题
*
* 一个数组A中存有NN>0个整数在不允许使用另外数组的前提下将每个整数循环向
* 右移MM>=0个位置即将A中的数据由A0 A1……AN-1变换为
* AN-M …… AN-1 A0 A1……AN-M-1最后M个数循环移至最前面的M个位置。如果需要
* 考虑程序移动数据的次数尽量少,要如何设计移动的方法?
*
* 输入格式每个输入包含一个测试用例第1行输入N ( 1<=N<=100)、MM>=0第2行输
* 入N个整数之间用空格分隔。
*
* 输出格式在一行中输出循环右移M位以后的整数序列之间用空格分隔序列结尾不能有
* 多余空格。
*
* 输入样例:
* 6 2
* 1 2 3 4 5 6
*
* 输出样例:
* 5 6 1 2 3 4
**/
#include <stdio.h>
int main()
@@ -32,13 +10,13 @@ int main()
/* Read */
for(int i = 0; i < N; i++)
scanf("%d", &numbers[i]);
/* Print */
for(int i = N - M; i < N; i++) /* Print N - M to N - 1 */
printf("%d ", numbers[i]);
for(int i = 0; i < N - M - 1; i++) /* Print 0 to N - M - 2 */
printf("%d ", numbers[i]);
printf("%d", numbers[N - M - 1]); /* Print N - M - 1, no blankspace */
return 0;
}

View File

@@ -1,21 +1,3 @@
/**
* 1009. 说反话
*
* 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
*
* 输入格式测试输入包含一个测试用例在一行内给出总长度不超过80的字符串。字符串
* 由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,
* 单词之间用1个空格分开输入保证句子末尾没有多余的空格。
*
* 输出格式:每个测试用例的输出占一行,输出倒序后的句子。
*
* 输入样例:
* Hello World Here I Come
*
* 输出样例:
* Come I Here World Hello
**/
#include <stdio.h>
#include <ctype.h>
@@ -23,12 +5,12 @@ int main()
{
char line[81], *p = line, *i;
fgets(line, 81, stdin);
while(*++p); /* Go to the end of the string */
while(p > line) /* Will break at the beginning of the string */
{
while(isspace(*--p)) ; /* Find the end of a word */
while(p > line && !isspace(*(p - 1))) p--; /* Find start of the word */
while(p > line && !isspace(*(p - 1))) p--; /* Find start of the word */
for(i = p; *i && !isspace(*i); putchar(*i++)); /* Print the word */
putchar(p == line ? '\0' : ' '); /* print blankspace if not at end */
}

View File

@@ -1,28 +1,10 @@
/**
* 1010. 一元多项式求导
*
* 设计函数求一元多项式的导数。x^nn为整数的一阶导数为n*x^n-1。
*
* 输入格式以指数递降方式输入多项式非零项系数和指数绝对值均为不超过1000的整数
* 数字间以空格分隔。
*
* 输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但
* 结尾不能有多余空格。注意“零多项式”的指数和系数都是0但是表示为“0 0”。
*
* 输入样例:
* 3 4 -5 2 6 1 -2 0
*
* 输出样例:
* 12 3 -10 1 6 0
**/
#include <stdio.h>
int main()
{
int coef, index, count = 0;
while(scanf("%d %d", &coef, &index) != EOF)
while(scanf("%d %d", &coef, &index) != EOF)
{
if(index) /* Constant terms result in zero */
{
@@ -30,10 +12,10 @@ int main()
printf("%d %d", coef * index, index - 1);
}
}
/* Zero polynomial or constant */
if(count == 0)
if(count == 0)
puts("0 0");
return 0;
}

View File

@@ -1,32 +1,3 @@
/**
* 1011. A+B和C
*
* 给定区间[-2^31, 2^31]内的3个整数A、B和C请判断A+B是否大于C。
*
* 输入格式:
*
* 输入第1行给出正整数T(<=10)是测试用例的个数。随后给出T组测试用例每组占一行
* 顺序给出A、B和C。整数间以空格分隔。
*
* 输出格式:
*
* 对每组测试用例在一行中输出“Case #X: true”如果A+B>C否则输出
* “Case #X: false”其中X是测试用例的编号从1开始
*
* 输入样例:
* 4
* 1 2 3
* 2 3 4
* 2147483647 0 2147483646
* 0 -2147483648 -2147483647
*
* 输出样例:
* Case #1: false
* Case #2: true
* Case #3: true
* Case #4: false
**/
#include <stdio.h>
int main()
@@ -34,12 +5,12 @@ int main()
int T;
long int A, B, C;
scanf("%d", &T);
for(int i = 0; i < T; i++)
{
scanf("%ld %ld %ld", &A, &B, &C);
printf("Case #%d: %s\n", i + 1, A + B > C ? "true" : "false");
}
return 0;
}

View File

@@ -1,39 +1,3 @@
/**
* 1012. 数字分类
*
* 给定一系列正整数请按要求对数字进行分类并输出以下5个数字
*
* A1 = 能被5整除的数字中所有偶数的和
* A2 = 将被5除后余1的数字按给出顺序进行交错求和即计算n1-n2+n3-n4...
* A3 = 被5除后余2的数字的个数
* A4 = 被5除后余3的数字的平均数精确到小数点后1位
* A5 = 被5除后余4的数字中最大数字。
*
* 输入格式:
*
* 每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N随后给出N个
* 不超过1000的待分类的正整数。数字间以空格分隔。
*
* 输出格式:
*
* 对给定的N个正整数按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔
* 行末不得有多余空格。
*
* 若其中某一类数字不存在则在相应位置输出“N”。
*
* 输入样例1
* 13 1 2 3 4 5 6 7 8 9 10 20 16 18
*
* 输出样例1
* 30 11 2 9.7 9
*
* 输入样例2
* 8 1 2 4 5 6 7 9 16
*
* 输出样例2
* N 11 2 N 9
**/
#include <stdio.h>
int main()
@@ -41,7 +5,7 @@ int main()
int count;
int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0;
int A2flag = 0, A4count = 0;
scanf("%d", &count);
for(int i = 0, n; i < count; i++)
{
@@ -61,6 +25,6 @@ int main()
if(A3 == 0) printf("N "); else printf("%d ", A3);
if(A4 == 0) printf("N "); else printf("%.1f ", A4 * 1.0 / A4count);
if(A5 == 0) printf("N"); else printf("%d", A5);
return 0;
}

View File

@@ -1,34 +1,12 @@
/**
* 1013. 数素数 (20)
*
* 令P_i表示第i个素数。现任给两个正整数M <= N <= 10^4请输出P_M到P_N的所有素数。
*
* 输入格式:
*
* 输入在一行中给出M和N其间以空格分隔。
*
* 输出格式:
*
* 输出从P_M到P_N的所有素数每10个数字占1行其间以空格分隔但行末不得有多余空格。
*
* 输入样例:
* 5 27
*
* 输出样例:
* 11 13 17 19 23 29 31 37 41 43
* 47 53 59 61 67 71 73 79 83 89
* 97 101 103
**/
#include <stdio.h>
int main()
{
int M, N;
scanf("%d %d", &M, &N);
int primes[10000];
for(int n = 2, count = 0; count < N; n++)
{
/* Check if n is prime number */
@@ -36,12 +14,12 @@ int main()
for(int j = 0; count > 0 && primes[j] * primes[j] <= n; j++)
if(n % primes[j] == 0)
iprime = 0;
/* Record */
if(iprime)
if(iprime)
primes[count++] = n;
}
/* Print */
for(int i = M; i < N; i++)
{
@@ -49,6 +27,6 @@ int main()
printf((i - M + 1) % 10 ? " " : "\n");
}
printf("%d", primes[N - 1]);
return 0;
}

View File

@@ -1,37 +1,3 @@
/**
* 1014. 福尔摩斯的约会 (20)
*
* 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧!
* 3485djDkxh4hhGE
* 2984akDfkkkkggEdsb
* s&hgsfdk
* d&Hyscvnm”。大侦探很快就明白了字条上奇怪的乱码实际上就是约会的时间
* “星期四 14:04”因为前面两字符串中第1对相同的大写英文字母大小写有区分
* 是第4个字母'D'代表星期四第2对相同的字符是'E'那是第5个英文字母代表
* 一天里的第14个钟头于是一天的0点到23点由数字0到9、以及大写字母A到N表示
* 后面两字符串第1对相同的英文字母's'出现在第4个位置从0开始计数代表
* 第4分钟。现给定两对字符串请帮助福尔摩斯解码得到约会的时间。
*
* 输入格式:
*
* 输入在4行中分别给出4个非空、不包含空格、且长度不超过60的字符串。
*
* 输出格式:
*
* 在一行中输出约会的时间格式为“DAY HH:MM”其中“DAY”是某星期的3字符缩写
* MON表示星期一TUE表示星期二WED表示星期三THU表示星期四FRI表示星期五
* SAT表示星期六SUN表示星期日。题目输入保证每个测试存在唯一解。
*
* 输入样例:
* 3485djDkxh4hhGE
* 2984akDfkkkkggEdsb
* s&hgsfdk
* d&Hyscvnm
*
* 输出样例:
* THU 14:04
**/
#include <stdio.h>
#include <ctype.h>
@@ -39,7 +5,7 @@ int main()
{
char str1[61], str2[61], str3[61], str4[61];
scanf("%s %s %s %s", str1, str2, str3, str4);
/* Find day, same char from [A-G] and same position in frist two lines */
int DAY;
char *weekdays[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
@@ -49,7 +15,7 @@ int main()
printf("%s", weekdays[str1[DAY] - 'A']);
break;
}
/* Find hour, picking up from DAY, same character and position, [A-N|0-9] */
int HH;
for(HH = DAY + 1; str1[HH] && str2[HH]; HH++)
@@ -66,7 +32,7 @@ int main()
break;
}
}
/* Find minute, same alphabet character from last two lines */
int MM;
for(MM = 0; str3[MM] && str4[MM]; MM++)
@@ -75,6 +41,6 @@ int main()
printf(":%02d", MM);
break;
}
return 0;
}

View File

@@ -1,62 +1,3 @@
/**
* 1015. 德才论 (25)
*
* 宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,
* 才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子
* 而与之,与其得小人,不若得愚人。”
*
* 现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
*
* 输入格式:
*
* 输入第1行给出3个正整数分别为N<=10^5即考生总数L>=60为录取最低
* 分数线即德分和才分均不低于L的考生才有资格被考虑录取H<100为优先录取线
* ——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;
* 才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;
* 德才分均低于H但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者按总分
* 排序但排在第二类考生之后其他达到最低线L的考生也按总分排序但排在第三类考生之后。
*
* 随后N行每行给出一位考生的信息包括准考证号、德分、才分其中准考证号为8位
* 整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。
*
* 输出格式:
*
* 输出第1行首先给出达到最低分数线的考生人数M随后M行每行按照输入格式输出一位
* 考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按
* 其德分降序排列;若德分也并列,则按准考证号的升序输出。
*
* 输入样例:
* 14 60 80
* 10000001 64 90
* 10000002 90 60
* 10000011 85 80
* 10000003 85 80
* 10000004 80 85
* 10000005 82 77
* 10000006 83 76
* 10000007 90 78
* 10000008 75 79
* 10000009 59 90
* 10000010 88 45
* 10000012 80 100
* 10000013 90 99
* 10000014 66 60
* 输出样例:
* 12
* 10000013 90 99
* 10000012 80 100
* 10000003 85 80
* 10000011 85 80
* 10000004 80 85
* 10000007 90 78
* 10000006 83 76
* 10000005 82 77
* 10000002 90 60
* 10000014 66 60
* 10000008 75 79
* 10000001 64 90
**/
#include <stdio.h>
#include <stdlib.h>
@@ -82,7 +23,7 @@ int comp(const void *a, const void *b)
{
Student s1 = *(Student*)a;
Student s2 = *(Student*)b;
if(s1->rank != s2->rank) return s1->rank - s2->rank;
else if(s1->sum != s2->sum) return s1->sum - s2->sum;
else if(s1->D != s2->D) return s1->D - s2->D;
@@ -95,22 +36,22 @@ int main()
int N, L, H, M = 0;
Student students[100000] = {0};
sStudent buffer[100000];
scanf("%d %d %d", &N, &L, &H);
for(int i = 0; i < N; i++)
{
Student s = buffer + i;
scanf("%d %d %d", &s->ID, &s->D, &s->C);
scanf("%d %d %d", &s->ID, &s->D, &s->C);
s->sum = s->D + s->C;
if((s->rank = rank(s, H, L)) != 0) /* record if passed */
students[M++] = s;
}
qsort(students, M, sizeof(Student), comp);
printf("%d\n", M);
for(int i = M - 1; i >= 0; i--)
printf("%d %d %d\n", students[i]->ID, students[i]->D, students[i]->C);
return 0;
}

View File

@@ -1,29 +1,3 @@
/**
* 1016. 部分A+B (15)
*
* 正整数A的“D_A为1位整数部分”定义为由A中所有D_A组成的新整数P_A。例如给定
* A = 3862767D_A = 6则A的“6部分”P_A是66因为A中有2个6。
*
* 现给定A、D_A、B、D_B请编写程序计算P_A + P_B。
*
* 输入格式:
*
* 输入在一行中依次给出A、D_A、B、D_B中间以空格分隔其中0 < A, B < 10^10。
*
* 输出格式:
*
* 在一行中输出P_A + P_B的值。
*
* 输入样例1
* 3862767 6 13530293 3
* 输出样例1
* 399
* 输入样例2
* 3862767 1 13530293 8
* 输出样例2
* 0
**/
#include <stdio.h>
long Dpart(long A, int D_A)
@@ -41,6 +15,6 @@ int main()
int D_A, D_B;
scanf("%ld %d %ld %d", &A, &D_A, &B, &D_B);
printf("%ld", Dpart(A, D_A) + Dpart(B, D_B));
return 0;
}

View File

@@ -1,23 +1,3 @@
/**
* 1017. A除以B (20)
*
* 本题要求计算A/B其中A是不超过1000位的正整数B是1位正整数。你需要输出商数Q和
* 余数R使得A = B * Q + R成立。
*
* 输入格式:
*
* 输入在1行中依次给出A和B中间以1空格分隔。
*
* 输出格式:
*
* 在1行中依次输出Q和R中间以1空格分隔。
*
* 输入样例:
* 123456789050987654321 7
* 输出样例:
* 17636684150141093474 3
**/
#include <stdio.h>
/* read 2 digits from highest digit of A, do manual division, get the quotient
@@ -29,7 +9,7 @@ int main()
int B;
char A[1001], *p = A;
scanf("%s %d", A, &B);
/* the results are stored in A and B instead of printed out on-the-fly */
int twodigit, remainder = 0;
for(int i = 0; A[i]; i ++)
@@ -39,10 +19,10 @@ int main()
remainder = twodigit % B;
}
B = remainder;
/* print */
if(A[0] == '0' && A[1] != '\0') p++;
printf("%s %d", p, B);
return 0;
}

View File

@@ -1,44 +1,3 @@
/**
* 1018. 锤子剪刀布 (20)
*
*
* 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
*
* 图略
*
* 现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的
* 胜算最大。
*
* 输入格式:
*
* 输入第1行给出正整数N<=105即双方交锋的次数。随后N行每行给出一次交锋的信息
* 即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”第1个字母代表
* 甲方第2个代表乙方中间有1个空格。
*
* 输出格式:
*
* 输出第1、2行分别给出甲、乙的胜、平、负次数数字间以1个空格分隔。第3行给出两个字
* 母分别代表甲、乙获胜次数最多的手势中间有1个空格。如果解不唯一则输出按字母序
* 最小的解。
*
* 输入样例:
* 10
* C J
* J B
* C B
* B B
* B C
* C C
* C B
* J B
* B C
* J J
* 输出样例:
* 5 3 2
* 2 3 5
* B B
*/
#include <stdio.h>
char max(int B, int C, int J)
@@ -54,7 +13,7 @@ int main()
char a, b;
int AwinB = 0, AwinC = 0, AwinJ = 0;
int BwinB = 0, BwinC = 0, BwinJ = 0;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
@@ -66,7 +25,7 @@ int main()
if(a == 'C' && b == 'B') BwinB++;
if(a == 'J' && b == 'C') BwinC++;
}
int Awin = AwinB + AwinC + AwinJ;
int Bwin = BwinB + BwinC + BwinJ;
int Tie = N - Awin - Bwin;

View File

@@ -1,47 +1,7 @@
/**
* 1019. 数字黑洞 (20)
*
* 给定任一个各位数字不完全相同的4位正整数如果我们先把4个数字按非递增排序再按非
* 递减排序然后用第1个数字减第2个数字将得到一个新的数字。一直重复这样做我们
* 很快会停在有“数字黑洞”之称的6174这个神奇的数字也叫Kaprekar常数。
*
* 例如我们从6767开始将得到
*
* 7766 - 6677 = 1089
* 9810 - 0189 = 9621
* 9621 - 1269 = 8352
* 8532 - 2358 = 6174
* 7641 - 1467 = 6174
* ... ...
*
* 现给定任意4位正整数请编写程序演示到达黑洞的过程。
*
* 输入格式:
*
* 输入给出一个(0, 10000)区间内的正整数N。
*
* 输出格式:
*
* 如果N的4位数字全相等则在一行内输出“N - N = 0000”否则将计算的每一步在一行内
* 输出直到6174作为差出现输出格式见样例。注意每个数字按4位数格式输出。
*
* 输入样例1
* 6767
* 输出样例1
* 7766 - 6677 = 1089
* 9810 - 0189 = 9621
* 9621 - 1269 = 8352
* 8532 - 2358 = 6174
* 输入样例2
* 2222
* 输出样例2
* 2222 - 2222 = 0000
*/
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b)
int cmp(const void *a, const void *b)
{
return *(int*)b - *(int*)a;
}
@@ -61,7 +21,7 @@ int reverse(int n)
int main()
{
int N;
scanf("%d", &N);
do
{
@@ -69,6 +29,6 @@ int main()
printf("%04d - %04d = %04d\n", N, reverse(N), N - reverse(N));
N = N - reverse(N);
}while(N != 0 && N != 6174) ;
return 0;
}

View File

@@ -1,58 +1,28 @@
/**
* 1020. 月饼 (25)
*
* 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有
* 种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
*
* 注意销售时允许取出一部分库存。样例给出的情形是这样的假如我们有3种月饼其库
* 存量分别为18、15、10万吨总售价分别为75、72、45亿元。如果市场的最大需求量只有
* 20万吨那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼
* 获得 72 + 45/2 = 94.5(亿元)。
*
* 输入格式:
*
* 每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类
* 数、以及不超过500以万吨为单位的正整数D表示市场最大需求量。随后一行给出N个正数
* 表示每种月饼的库存量以万吨为单位最后一行给出N个正数表示每种月饼的总售价
* (以亿元为单位)。数字间以空格分隔。
*
* 输出格式:
*
* 对每组测试用例在一行中输出最大收益以亿元为单位并精确到小数点后2位。
*
* 输入样例:
* 3 20
* 18 15 10
* 75 72 45
* 输出样例:
* 94.50
*/
#include <stdio.h>
int main()
{
int N, max;
float D, Storage[1000], Total = 0, Price[1000];
float D, Storage[1000], Total = 0, Price[1000];
scanf("%d %f", &N, &D);
for(int i = 0; i < N; i++) scanf("%f", Storage + i);
for(int i = 0; i < N; i++) scanf("%f", Price + i);
while(D > 0)
{
max = 0;
for(int i = 0; i < N; i++)
if(Price[i] / Storage[i] > Price[max] / Storage[max])
max = i;
if(Storage[max] < D)
if(Storage[max] < D)
{
Total += Price[max];
D -= Storage[max];
Price[max] = 0;
}
else
}
else
{
Total += Price[max] * D / Storage[max];
D = 0;

View File

@@ -1,28 +1,3 @@
/**
* 1021. 个位数统计 (15)
*
* 给定一个k位整数N = d_k-1*10^k-1 + ... + d_1*10^1 + d0 (0<=d_i<=9, i=0,...,k-1,
* d_k-1>0)请编写程序统计每种不同的个位数字出现的次数。例如给定N = 100311
* 则有2个03个1和1个3。
*
* 输入格式:
*
* 每个输入包含1个测试用例即一个不超过1000位的正整数N。
*
* 输出格式:
*
* 对N中每一种不同的个位数字以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。
* 要求按D的升序输出。
*
* 输入样例:
* 100311
* 输出样例:
*
* 0:2
* 1:3
* 3:1
*/
#include <stdio.h>
int main()
@@ -31,9 +6,9 @@ int main()
int count[10] = {0};
while((c = getchar()) != '\n')
count[c - '0']++;
for(int i = 0; i < 10; i++) if(count[i])
printf("%d:%d\n", i, count[i]);
return 0;
}

View File

@@ -1,22 +1,3 @@
/**
* 1022. D进制的A+B (20)
*
* 输入两个非负10进制整数A和B(<=2^30-1)输出A+B的D (1 < D <= 10)进制数。
*
* 输入格式:
*
* 输入在一行中依次给出3个整数A、B和D。
*
* 输出格式:
*
* 输出A+B的D进制数。
*
* 输入样例:
* 123 456 8
* 输出样例:
* 1103
*/
#include <stdio.h>
int main()
@@ -24,15 +5,15 @@ int main()
int A, B, D, Sum;
scanf("%d %d %d", &A, &B, &D);
Sum = A + B;
/* calculate the bits of Sum */
int power = 1;
/* use Sum / D >= power to avoid using long int */
while(Sum / D >= power) power *= D;
/* calculate D-base number. print them on-the-fly */
for(; power > 0; Sum %= power, power /= D)
printf("%d", Sum / power);
return 0;
}

View File

@@ -1,34 +1,9 @@
/**
* 1023. 组个最小数 (20)
*
* 给定数字0-9各若干个。你可以以任意顺序排列这些数字但必须全部使用。目标是使得最后
* 得到的数尽可能小注意0不能做首位。例如给定两个0两个1三个5一个8我们
* 得到的最小的数就是10015558。
*
* 现给定数字,请编写程序输出能够组成的最小的数。
*
* 输入格式:
*
* 每个输入包含1个测试用例。每个测试用例在一行中给出10个非负整数顺序表示我们拥有
* 数字0、数字1、……数字9的个数。整数间用一个空格分隔。10个数字的总个数不超过50
* 至少拥有1个非0的数字。
*
* 输出格式:
*
* 在一行中输出能够组成的最小的数。
*
* 输入样例:
* 2 2 0 0 0 3 0 0 1 0
* 输出样例:
* 10015558
*/
#include <stdio.h>
int main()
{
int zero, nonzero;
scanf("%d", &zero);
for(int i = 1; i < 10; i++)
{
@@ -40,6 +15,6 @@ int main()
putchar('0');
}
}
return 0;
}

View File

@@ -1,33 +1,3 @@
/**
* 1024. 科学计数法 (20)
*
* 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式
* [+-][1-9]"."[0-9]+E[+-][0-9]+即数字的整数部分只有1位小数部分至少有1位
* 该数字及其指数部分的正负号即使对正数也必定明确给出。
*
* 现以科学计数法的格式给出实数A请编写程序按普通数字表示法输出A并保证所有有效位
* 都被保留。
*
* 输入格式:
*
* 每个输入包含1个测试用例即一个以科学计数法表示的实数A。该数字的存储长度不超过
* 9999字节且其指数的绝对值不超过9999。
*
* 输出格式:
*
* 对每个测试用例在一行中按普通数字表示法输出A并保证所有有效位都被保留
* 包括末尾的0。
*
* 输入样例1
* +1.23400E-03
* 输出样例1
* 0.00123400
* 输入样例2
* -1.2E+10
* 输出样例2
* -12000000000
*/
#include <stdio.h>
int main()
@@ -55,6 +25,6 @@ int main()
putchar('0');
for(; *p; p++) if(*p != '.') putchar(*p); /* the rest */
}
return 0;
}

View File

@@ -1,42 +1,3 @@
/**
* 1025. 反转链表 (25)
*
* 给定一个常数K以及一个单链表L请编写程序将L中每K个结点反转。例如给定L为
* 1→2→3→4→5→6K为3则输出应该为3→2→1→6→5→4如果K为4则输出应该为
* 4→3→2→1→5→6即最后不到K个元素不反转。
*
* 输入格式:
*
* 每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数
* 正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是
* 5位非负整数NULL地址用-1表示。
*
* 接下来有N行每行格式为
* Address Data Next
*
* 其中Address是结点地址Data是该结点保存的整数数据Next是下一结点的地址。
*
* 输出格式:
*
* 对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
*
* 输入样例:
* 00100 6 4
* 00000 4 99999
* 00100 1 12309
* 68237 6 -1
* 33218 3 00000
* 99999 5 68237
* 12309 2 33218
* 输出样例:
* 00000 4 33218
* 33218 3 12309
* 12309 2 00100
* 00100 1 99999
* 99999 5 68237
* 68237 6 -1
*/
#include <stdio.h>
#include <stdlib.h>
@@ -53,15 +14,15 @@ int main()
int A, N, K;
node nodes[100000] = {0};
Node np[100000] = {0}, *p;
/* read */
scanf("%d %d %d", &A, &N, &K);
for(int i = 0; i < N; i++)
for(int i = 0; i < N; i++)
{
np[i] = nodes + i;
scanf("%d %d %d", &np[i]->addr, &np[i]->data, &np[i]->next);
}
/* link the list */
for(int i = 0; i < N; i++)
{
@@ -82,7 +43,7 @@ int main()
for(int j = 0; j < K / 2; j++)
SWAPNODE(p[j], p[K - j - 1]);
}
/* print the list */
for(int i = 0; i < N - 1; i++)
printf("%05d %d %05d\n", np[i]->addr, np[i]->data, np[i + 1]->addr);

View File

@@ -1,33 +1,3 @@
/**
* 1026. 程序运行时间(15)
*
* 要获得一个C语言程序的运行时间常用的方法是调用头文件time.h其中提供了clock()
* 函数可以捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单位是
* clock tick即“时钟打点”。同时还有一个常数CLK_TCK给出了机器时钟每秒所走的时钟
* 打点数。于是为了获得一个函数f的运行时间我们只要在调用f之前先调用clock(),获得一
* 个时钟打点数C1在f执行完成后再调用clock()获得另一个时钟打点数C2两次获得的
* 时钟打点数之差(C2-C1)就是f运行所消耗的时钟打点数再除以常数CLK_TCK就得到了以
* 秒为单位的运行时间。
*
* 这里不妨简单假设常数CLK_TCK为100。现给定被测函数前后两次获得的时钟打点数请你给
* 出被测函数运行的时间。
*
* 输入格式:
*
* 输入在一行中顺序给出2个整数C1和C2。注意两次获得的时钟打点数肯定不相同即C1 < C2
* 并且取值在[0, 10^7]。
*
* 输出格式:
*
* 在一行中输出被测函数运行的时间。运行时间必须按照“hh:mm:ss”即2位的“时:分:秒”)
* 格式输出不足1秒的时间四舍五入到秒。
*
* 输入样例:
* 123 4577973
* 输出样例:
* 12:42:59
*/
#include <stdio.h>
int main()

View File

@@ -1,37 +1,3 @@
/**
* 1027. 打印沙漏(20)
*
* 本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
*
* *****
* ***
* *
* ***
* *****
* 所谓“沙漏形状”是指每行输出奇数个符号各行符号中心对齐相邻两行符号数差2符号
* 数先从大到小顺序递减到1再从小到大顺序递增首尾符号数相等。
*
* 给定任意N个符号不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
*
* 输入格式:
*
* 输入在一行给出1个正整数N<=1000和一个符号中间以空格分隔。
*
* 输出格式:
* 首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
*
*
* 输入样例:
* 19 *
* 输出样例:
* *****
* ***
* *
* ***
* *****
* 2
*/
#include <stdio.h>
#define ABS(X) ((X) >= 0 ? (X) : -(X))

View File

@@ -1,33 +1,3 @@
/**
* 1028. 人口普查(20)
*
* 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。
*
* 这里确保每个输入的日期都是合法的但不一定是合理的——假设已知镇上没有超过200岁的
* 老人而今天是2014年9月6日所以超过200岁的生日和未出生的生日都是不合理的应该
* 被过滤掉。
*
* 输入格式:
*
* 输入在第一行给出正整数N取值在(0, 10^5]随后N行每行给出1个人的姓名由不超过
* 5个英文字母组成的字符串、以及按“yyyy/mm/dd”即年/月/日)格式给出的生日。题目
* 保证最年长和最年轻的人没有并列。
*
* 输出格式:
*
* 在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。
*
* 输入样例:
* 5
* John 2001/05/12
* Tom 1814/09/06
* Ann 2121/01/30
* James 1814/09/05
* Steve 1967/11/20
* 输出样例:
* 3 Tom John
*/
#include <stdio.h>
#include <string.h>
@@ -36,25 +6,25 @@ int main()
int N, count = 0;
/* store name and birthday in one string: "YYYY/MM/DD\0NAMES\0" */
char cur[17], eldest[17] = {'9'}, youngest[17] = {'0'};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%s %s", cur + 11, cur);
if(strcmp(cur, "1814/09/06") >= 0 && strcmp(cur, "2014/09/06") <= 0)
if(strcmp(cur, "1814/09/06") >= 0 && strcmp(cur, "2014/09/06") <= 0)
{
if(strcmp(cur, eldest) <= 0)
if(strcmp(cur, eldest) <= 0)
memcpy(eldest, cur, 17);
if(strcmp(cur, youngest) >= 0)
memcpy(youngest, cur, 17);
count++;
}
}
if(count)
printf("%d %s %s", count, eldest + 11, youngest + 11);
else
else
printf("0");
return 0;
}

View File

@@ -1,27 +1,3 @@
/**
* 1029. 旧键盘(20)
*
* 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该
* 输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。
*
* 输入格式:
*
* 输入在2行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过80个字符
* 的串由字母A-Z包括大、小写、数字0-9、以及下划线“_”代表空格组成。题目保证
* 2个字符串均非空。
*
* 输出格式:
*
* 按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。
* 题目保证至少有1个坏键。
*
* 输入样例:
* 7_This_is_a_test
* _hs_s_a_es
* 输出样例:
* 7TI
*/
#include <ctype.h>
#include <stdio.h>
@@ -34,7 +10,7 @@ int main()
while((c = getchar()) != '\n')
printed[toupper(c)]++;
for(char *p = line; *p; p++)
for(char *p = line; *p; p++)
{
c = toupper(*p);
if(printed[(int)c] == 0)

View File

@@ -1,50 +1,27 @@
/**
* 1030. 完美数列(25)
*
* 给定一个正整数数列和正整数p设这个数列中的最大值是M最小值是m如果
* M <= m * p则称这个数列是完美数列。
*
* 现在给定参数p和一些正整数请你从中选择尽可能多的数构成一个完美数列。
*
* 输入格式:
*
* 输入第一行给出两个正整数N和p其中N<= 105是输入的正整数的个数p<= 109
* 是给定的参数。第二行给出N个正整数每个数不超过109。
*
* 输出格式:
*
* 在一行中输出最多可以选择多少个数可以用它们组成一个完美数列。
* 输入样例:
* 10 8
* 2 3 20 4 5 1 6 7 8 9
* 输出样例:
* 8
*/
#include <stdio.h>
#include <stdlib.h>
int comp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
int comp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
}
int main()
{
int N, p, data[100000];
int max = 0, first, last;
scanf("%d %d", &N, &p); /* read */
for(int i = 0; i < N; i++)
for(int i = 0; i < N; i++)
scanf("%d", data + i);
qsort(data, N, sizeof(int), comp); /* sort */
for(first = 0, last = 0; last < N && max < N - first; first++) /* find */
{
while(last < N && data[last] <= 1L * data[first] * p)
last++;
if(max < last - first)
if(max < last - first)
max = last - first;
}
printf("%d", max);

View File

@@ -1,46 +1,3 @@
/**
* 1031. 查验身份证(15)
*
* 一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算
* 规则如下:
*
* 首先对前17位数字加权求和权重分配为
* {7910584216379105842}然后将计算的和对11取模得
* 到值Z最后按照以下关系对应Z值与校验码M的值
*
* Z0 1 2 3 4 5 6 7 8 9 10
* M1 0 X 9 8 7 6 5 4 3 2
*
* 现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。
*
* 输入格式:
*
* 输入第一行给出正整数N<= 100是输入的身份证号码的个数。随后N行每行给出1个18位
* 身份证号码。
*
* 输出格式:
*
* 按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理只检查
* 前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常则输出“All passed”。
*
* 输入样例1
* 4
* 320124198808240056
* 12010X198901011234
* 110108196711301866
* 37070419881216001X
* 输出样例1
* 12010X198901011234
* 110108196711301866
* 37070419881216001X
* 输入样例2
* 2
* 320124198808240056
* 110108196711301862
* 输出样例2
* All passed
*/
#include <stdio.h>
int main()
@@ -49,16 +6,16 @@ int main()
int weight[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char ZtoM[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
char ID[19];
scanf("%d", &N);
int d, sum, count = 0; /* index, weighted sum and count for legal IDs */
for(int i = 0; i < N; i++)
{
scanf("%s", ID);
for(d = 0, sum = 0; d < 17 && ID[d] >= '0' && ID[d] <= '9'; d++)
sum += (ID[d] - '0') * weight[d];
if(d == 17 && ID[17] == ZtoM[sum % 11]) /* legal ID */
count++;
else /* illegal ID */
@@ -66,6 +23,6 @@ int main()
}
if(count == N)
puts("All passed");
return 0;
}

Some files were not shown because too many files have changed in this diff Show More