顯示具有 ACM 標籤的文章。 顯示所有文章
顯示具有 ACM 標籤的文章。 顯示所有文章

2010年7月18日 星期日

[Timus Online Judge] 1068. Sum

This question is too simple.








Time Limit: 2.0 second
Memory Limit: 16 MB
Your task is to find the sum of all integer numbers lying between 1 and N inclusive.
Input
The input consists of a single integer N that is not greater than 10000 by it's absolute value.
Output
Write a single integer number that is the sum of all integer numbers lying between 1 and N inclusive.
Sample
input

-3

output

-5





#include
#include

int main(){
int i,num,sum;
scanf("%d",&num);

sum = 0;
if ( num > 0)
for(i = 1 ; i <= num ; i ++)
sum += i;
else
for(i = 1 ; i >= num ; i --)
sum+=i;

printf("%d",sum);

return 0;
}

1078. Segments

Oh ~ my god!!
Why alway got the WA #2.
Let me think about this question later.

This question describe :
One line have several segment.
Each segment have two point.

Find which segment is big than other.

ie.

------+-----+--------+------+---------------
-3 -2 2 3

So input N = 2 segment and a = -3 3 && b = -2 2.
a include b.
So a this segment is big than b.
so the answer is
2 segment include to each other.
b a ( The order is small segment to big segment.)







Time Limit: 1.0 second
Memory Limit: 16 MB
A number of segments are lying on a line. Every segment is given with the coordinates of its endpoints. Segments are numbered from 1 to N (0 < N < 500). We assume, that one segment is inside another, if the two segments are different, the first one is fully contained in the second one, and their endpoints do not coincide. Write a program, which finds the numbers of the segments in the longest sequence of segments which are contained in. In the sequence, every segment except the last is inside the next segment in the sequence.
Input
The first line contains one integer N. Next, there are N lines, with two integers on every line, which are the coordinates of the left and the right endpoints of the corresponding segment. These coordinates are integers in the interval [–10000, 10000]. We assume that, the given segments are numbered according to their place in the input.
Output
The first line must contain one integer, equal to the number of segments in the found sequence. The following line must contain the numbers of the segments in this sequence. These numbers must be outputted, in the order in which the segments' lengths increase, starting from the smallest. If there are more than one output sequences, write any of them.
Sample
input

4
-2 2
-1 1
-3 3
4 5

output

3
2 1 3






#include
#include
#include

typedef struct{
int right;
int left;
int index;
int win;
int lose;
}segment;


int contain(segment a,segment b){
if (a.left > b.left && a.right < b.right ) return 1;
else return 0;
}


int main(){

int i,j,m,num;
segment point[500],tmp;
memset(point , '\0' , sizeof(point) );

scanf("%d",&num);
if(num <= 0 || num >= 500) exit(0);

for(i = 0 ; i < num ; i++){
scanf("%d %d",&point[i].left , &point[i].right );
if(point[i].left > 10000 || point[i].left < -10000) exit(0);
else if(point[i].right > 10000 || point[i].right < -10000) exit(0);
point[i].index = i;
}

for(i = 0 ; i < num ; i++){
if(point[i].left > point[i].right){
m = point[i].left;
point[i].left = point[i].right;
point[i].right = m ;
}
}
/*
for(i = 0 ; i < num ; i ++){
printf("%2d %2d\n",point[i].left , point[i].right);
}
*/
for (i = 0 ; i < num ; i++){
for(j = 0; j < num; j++){
if( ( i != j ) && ( contain( point[i] , point[j] ) ) ){
/* printf("i = %d j = %d \n" , i, j); */
point[i].lose ++;
point[j].win ++;
}
}
}
/*
for(i = 0 ; i < num ; i ++){
printf("%2d %2d\n",point[i].win , point[i].lose);
}
*/
for(i = 0 ; i < num ; i ++){
for(j = i ; j < num ; j++ ){
if ( point[i].lose < point[j].lose ){

tmp = point[i];
point[i] = point[j];
point[j] = tmp ;
}
}
}

for(i = point[0].lose ; i < num ; i ++){
for(j = i ; j < num ; j++ ){
if ( point[i].win < point[j].win ){
tmp = point[i];
point[i] = point[j];
point[j] = tmp ;
}
}
}

printf("%d\n",point[0].lose + 1);

for(i = 0 ; i <= point[0].lose ; i ++){
printf("%d ",point[i].index+1);
}

return 0;
}

2010年7月17日 星期六

[Timus Online Judge] 1294. Mars satellites

Got the AC.
According to Euclidean distance in various coordinate systems.

Keypoint printf("Distance is %.0lf km.",x);
Using .0lf no print number behind the point.
ie. 2.23 printf just 2

lf = double







1294. Mars satellites
Time Limit: 1.0 second
Memory Limit: 16 MB
Four artificial satellites travel in one plane along the areostationary orbit around Mars. They have code names A, B, C and D and travel exactly in this order. Venus’s scouts for military purposes (for what particular purpose they did not say) decided to find a distance between satellites C and D. All Mars satellites could measure distances to the other satellites, that is why all what is needed to do is to penetrate in the computer system of satellite C and measure the distance to satellite D (or vice versa). Nevertheless, Martians are not so stupid and have not very bad defense. That is why all what could Venus’s scouts do is to break the defense of satellites A and B (that were older models). They measured distances from satellites A and B to satellites C and D, but now they do not know how to find the distance from C to D using these measurements. You can help them.
Input
There are 4 numbers: distances from A to D, from A to C, from B to D and from B to C in thousands kilometers (integers from 1 to 10000). Satellites can measure distance even through the planet and you may assume that orbit is a circle. Do not assume the radius of the orbit equal to 20392 km as it should be for the real areostationary orbit.
Output
If it is impossible to find out the distance from C to D with these data, you should print "Impossible.", otherwise you are to print "Distance is X km.", where X is the required distance in kilometers (rounded to the integer number).

Sample
input

4 7 5 7

output

Distance is 5385 km.



#include
#include
#include

int main()
{
int AD,AC,BD,BC;
double x, _x;
double cos_ ;
scanf("%d %d %d %d",&AD,&AC,&BD,&BC);

if (fabs(AD*AC - BD*BC) < 0.001){
printf("Impossible.");
}
else{
cos_ = (double)(AD*AD + AC*AC - BD*BD - BC*BC) / (double)(2*AD*AC - 2*BD*BC);
x = sqrt (AD*AD + AC*AC - 2*AD*AC*cos_) * 1000;
_x = floor(x);

if (fabs(x - _x) > 0.5){
x = ceil(x);
}
else{
x = _x;
}
printf("Distance is %.0lf km.",x);
}
return 0;
}

2010年7月16日 星期五

[Timus Online Judge] 1201. Which day is it?

I test sample test that is OK.
But when I upload to Judgement.

I got WA#1.
I don't know where is the problem ?
I must check this program later.


According this program there have some trick point.
int day=4; /* Key point */
The day must be 4 first.








1201. Which day is it?
Time Limit: 1.0 second
Memory Limit: 16 MB
Sometimes it is of great importance to know which day of the week a given date will be. And we start searching for the nearest calendar. Being lucky we may find one. And find out that this one does not contain the date you need. What a pity!
Thus you are asked to create a calendar that will be able to process any given date in the years range from 1600 till 2400. Given a date, your program should print (see the examples below) a correct calendar for the month containing the date. Do not forget about the leap years. A year is considered to be leap if it is multiple of 4 except it is multiple of 100 except it is multiple of 400. For example 1996 is a leap year, 1900 is not a leap year (it is a multiple of 4 and multiple of 100) and 2000 is a leap year (it is a multiple of 4, multiple of 100 and multiple of 400 as well).
Input
The first line of input contains a date, i.e. three integer numbers: day (1–31), month (1–12) and year (1600–2400) separated by spaces.
Output
The output should contain exactly 7 lines with the correct calendar for the month containing the given date. Format of a calendar is given by the examples below (for a reading convenience spaces in output example are replaced with dots, real output should contain spaces instead). And do not forget to highlight the given date by square brackets.
Samples
input output

16 3 2002



mon........4...11...18...25
tue........5...12...19...26
wed........6...13...20...27
thu........7...14...21...28
fri...1....8...15...22...29
sat...2....9..[16]..23...30
sun...3...10...17...24...31


1 3 2002

mon........4...11...18...25
tue........5...12...19...26
wed........6...13...20...27
thu........7...14...21...28
fri.[.1]...8...15...22...29
sat...2....9...16...23...30
sun...3...10...17...24...31





#include<stdio.h>
#include<stdlib.h>

int is_leap_year(int y){
return ( ( y%4==0 ) && (y%100!=0||y%400==0) );
}


int days_per_year(int y){

if(is_leap_year(y))
return 366;
else
return 365;
}

int days_per_month(int m,int y){

const int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(m!=2)
return days[m-1];
else if(is_leap_year(y))
return 29;
else
return 28;
}

int get_day_of_week(int m,int d,int y){
<font color=red> int day=4; /* Key point */ </font>
int i;

for(i=1600;i<y ; i++) day+=days_per_year(i);
for(i=1;i<m;i++) day+=days_per_month(i,y);
day+=d;


return day%7;
}

void print_calendar(int m,int d,int y)
{
const char* day_name[7]={"mon","tue","wed","thu","fri","sat","sun"};
int days=days_per_month(m,y);
int day_begin=1-get_day_of_week(m,1,y);

int more_print = (day_begin + days)%7;

int i,j,flag;


flag = 0;
for(i=0;i<7;i++){
printf("%s",day_name[i]);

for(j=day_begin+i ; j <= days + more_print ; j+=7 )

if( j<1 || j>days )
printf("....");

else if(j==d){
flag = 1;
if(j<10)
printf("..[%d]",j);
else
printf("..[%2d]",j);
}
else{
if(j<day_begin+7){
if(flag){
printf("..%d",j);
flag = 0 ;
}
else{
printf("...%d",j);
}
}
else if(j >= day_begin+7 && j<10){
if(flag){
printf("...%d",j);
flag = 0 ;
}
else{
printf("....%d",j);
}
}
else if(j>=10 && j<= days){
if(flag){
printf("..%2d",j);
flag = 0 ;
}
else{
printf("...%2d",j);
}
}
else{
printf("....");

}
}/* if( j<1 || j>days ) */

printf("\n");
}/* for(i=0;i<7;i++) */

}


int main()
{
int d,m,y;
scanf("%d %d %d",&d,&m,&y);

if( y < 1600 || y > 2400) exit(0);
if( m < 1 || m > 12 ) exit(0);
if( d < 1 || d > 31 ) exit(0);

print_calendar(m,d,y);


exit (0);
}


2010年7月14日 星期三

[Timus Online Judge] 1083. Factorials!!!

Try first time and got AC.
The point is how to get string.
Count how many '!' do user input.







Time Limit: 1.0 second
Memory Limit: 16 MB
Definition 1. n!!…! = n(n−k)(n−2k)…(n mod k), if k doesn’t divide n; n!!…! = n(n−k)(n−2k)…k, if k divides n (There are k marks ! in the both cases).
Definition 2. X mod Y — a remainder after division of X by Y.
For example, 10 mod 3 = 1; 3! = 3·2·1; 10!!! = 10·7·4·1.
Given numbers n and k we have calculated a value of the expression in the first definition. Can you do it as well?
Input
contains the only line: one integer n, 1 ≤ n ≤ 10, then exactly one space, then k exclamation marks, 1 ≤ k ≤ 20.
Output
contains one number — n!!…! (there are k marks ! here).



#include
#include


int Factorials(int n,int k){

if (n<=k){
if(n%k == 0)
return k;
else
return n%k;

}
else
return n*Factorials(n-k,k);

}

int main(void){

size_t nbytes = 2;
int n_num,k_num;
char *line;
line = (char *) malloc (nbytes+1);

scanf("%d ",&n_num);

k_num = 0;
while ( fgets(line, nbytes , stdin) != NULL ){
k_num++;
}
k_num--;

printf("%d",Factorials(n_num,k_num));

return 0;
}

[Timus Online Judge] 1079. Maximum

Finally I got the AC.
I am so happy about this.
Step by Step to success.

According to this program.
It must consider about the time complex.








Time Limit: 2.0 second
Memory Limit: 16 MB
Consider the sequence of numbers ai, i = 0, 1, 2, …, which satisfies the following requirements:

* a0 = 0
* a1 = 1
* a2i = ai
* a2i+1 = ai + ai+1

for every i = 1, 2, 3, … .
Write a program which for a given value of N (0 < N < 100000) finds the largest number among the numbers a0, a1, …, aN.
Input
Input contains not more than 10 lines containing one number N. The last line contains 0.
Output
For every N in the input write the corresponding maximum value found.



#include

int Maximum(int N){

if (N == 0)
return 0;
else if (N == 1)
return 1;
else if( (N % 2) == 0 )
return Maximum(N/2) ;
else
return ( Maximum(N/2) + Maximum(N/2 + 1) );

}


int main(void){

long i,num,max;
long number[100001];

for(i = 0 ; i <= 100000 ; i++){
number[i]=Maximum(i);
}

while(scanf("%ld",&num)!=EOF){
if (num==0) break;
for(i=0,max=0;i<=num;i++)
if(number[i]>max) max=number[i];
printf("%ld\n",max);
}


return 0;
}

[GDB] Using gdb debug

compile the code

gcc -Wall -g xxx.c -o xxx

-g mean open debug function.

When compile is done.


Execute the code
gdb ./a.out or
gdb --args executablename arg1 arg2 arg3 [2]

Type the command like below.

(gdb) b main

Breakpoint 1 at 0x804857e: file 1038.c, line 33.

(gdb) run < 2  ( feed some sample file from outside  )

(gdb) s  (  execute the next code  )

(gdb) p  (  print the variable  )


Repeat to print one variable

$ break myfile.cpp:180
Breakpoint 1 at 0x46ba0e: file myfile.cpp, line 180.

$ commands 1
> print decoder.m_msg
> end
$

Reference :

  1. How do I use gcc, g++, and gdb?
  2. How do I run a program with commandline args using gdb within a bash script?
  3. Can gdb print a specific variable every time it breaks? [duplicate]
  4. GDB實用教學:自動化你的debug
Youtube
  1. 'Become a GDB Power User' - Greg Law [ ACCU 2016 ]

[Timus Online Judge] 1038. Spell checker

I got the WA.
So think this subject later.

But the sample can pass, I got the same answer.







1038. Spell checker
Time Limit: 0.5 second
Memory Limit: 16 MB
The boss of a firm that you are employed with is dissatisfied with the text processor Word. He wants you to write a better text processor by tomorrow. The interface of the new processor should be clearer, there should be more options, and the resulting text should be more beautiful. You told the boss that this work would take not less than four days. Then your boss asked you to begin with a spell checking program. This program should check capital and small letters. It should detect a mistake in each of the following cases.

1. The first letter in a sentence is small.
2. A capital letter is not the first letter in a word.

A word is a sequence of letters not containing any other symbols or ends of line.
The end of a sentence is defined a full stop, a question-mark or an exclamation mark.
Input
Input contains a text that consists of capital and small letters of the Latin alphabet (A–Z, a–z), digits (0–9), punctuation marks (.,;:-!?) and space characters. The text length is not more than 10000.
Output
Output should contain a number of mistakes in the input text.





#include
#include
#include

char *mygetline(char *line, int size){

int len;
char *com_line;
com_line = (char *) malloc (size);
while ( fgets(line, size, stdin) != NULL ){
len = strlen(line);

strcat(com_line,line);

}
len = strlen(com_line);
com_line[len-1] = '\0' ;
/*
printf("%s",com_line);
printf("%d",strlen(com_line));
*/
return com_line;
}





int main(void)
{
size_t nbytes = 2;
char *c_string;
char text[2] = "";
int i,error;
int flag_first_letter,flag_punctuation;

/* These 2 lines are the heart of the program. */
c_string = (char *) malloc (nbytes + 1);
c_string = mygetline(text, sizeof text);

/* printf("%s",c_string); */
flag_first_letter = 0;
flag_punctuation = 1;
error = 0;



for ( i=0; i< strlen(c_string);i++ ){

if( c_string[i]=='.' || c_string[i]==',' || c_string[i]==';' || c_string[i]==':' || c_string[i]=='-' || c_string[i]=='!' || c_string[i]=='?' || c_string[i]=='\n'){
flag_punctuation = 1;
flag_first_letter = 0 ;
continue ;
}

if( c_string[i]==' ' ){
flag_first_letter = 0 ;
continue ;
}



if ( flag_punctuation == 1 ){
if( c_string[i] >= 'a' || c_string[i] >= 'z' ){
error++;
flag_punctuation = 0 ;
flag_first_letter = 1 ;

}
else{
flag_punctuation = 0 ;
flag_first_letter = 1 ;
}
}
else{
if( flag_first_letter == 1 && ( c_string[i] >= 'A' && c_string[i] <= 'Z' ) ){

error++;
continue;
}
else if( (c_string[i] >= 'A' && c_string[i] <= 'Z' ) || (c_string[i] >= 'a' && c_string[i] <= 'z' ) ){
flag_first_letter = 1 ;
continue;
}
else
continue;
}



}

/* printf("Error : %d",error); */
printf("%d\n",error);


return 0;
}

/*

http://acm.timus.ru/forum/thread.aspx?space=1&num=1038&id=5701&upd=633298366317090000

http://acm.timus.ru/forum/thread.aspx?space=1&num=1038&id=15439&upd=633298373142715000

http://www.seas.upenn.edu/cets/answers/gcc.html gdb



(gdb) b main
Breakpoint 1 at 0x804857e: file 1038.c, line 33.
(gdb) run < 2



*/

2010年7月11日 星期日

[Timus Online Judge] 1098. Questions - read multi line without newline


#include
#include
#include

char *mygetline(char *line, int size)
{
char *com_line;
int len;
com_line = (char *)malloc (size);
while ( fgets(line, size, stdin) != NULL ){

len = strlen(line);
if( line[len-1] == '\n' )
line[len-1] = 0;

strcat(com_line,line);
}


return com_line;
}

int main(void)
{
char text[101] = "";
fputs("prompt: ", stdout);
fflush(stdout);

printf("text = \"%s\"\n", mygetline(text, sizeof text) );
return 0;

}

/*
http://cboard.cprogramming.com/c-programming/70320-how-remove-newline-string.html
*/

2010年7月9日 星期五

[Timus Online Judge] 1098. Questions

Explain :
Input one string.
Print this string until to 1999 character.

For example :
Taking a string "Is it a good question?". <-- This string include 22 character.
Print this string to 1999th character.
And delete.So the 1999th character is "i".

And print the whole sting again, but exclude "i".








If the last remaining character is a question-mark ("?") then the answer to the question is "Yes". If it is a space then the answer is "No". Any other character will lead to "No comments" answer.



Holding a collegiate programming contest is a very exhausting work. There is a well-known proverb that one fool can ask so many questions that a hundred clever men will not answer. And during a collegiate programming contest questions are asked by one hundred clever people.
The jury of the Third Urals Collegiate Programming Contest being clever enough has found a simple way to make its work easier. We have invented a simple algorithm that will help us answer ALL your numerous questions! Moreover, this algorithm guarantees that the same questions will have the same answers (this would be hardly possible, if we would undertook such a task ourselves). According to this algorithm a member of the jury starts to delete characters of the question in the following order:

1. Starting from the first character he or she counts out N−1 characters (spaces, punctuation marks etc. are considered to be characters too) and deletes the Nth character.
2. If a string ends the count continues from the beginning of the string.
3. After deleting a character the count restarts from the character that would be the (N+1)-st in the previous count.
4. If the last remaining character is a question-mark ("?") then the answer to the question is "Yes". If it is a space then the answer is "No". Any other character will lead to "No comments" answer.

You should help the jury and write a program that will do a hard work of answering your questions tomorrow. The number N is secret and will not be announced even after the end of the contest. Your program should use N = 1999.

For example, taking a string "Is it a good question?" (its length is 22) the characters will be counted in the following way: "Is it a good question?Is it … quest" and "i" will be deleted. Then the count restarts from "on?Is it…" etc., until "s" will be left (thus the answer is "No comments", as usual).
Input
The input is a question, that is any text containing at least one character (end of line is not a character). Each character of the input (excepting the ends of lines) is a part of the question. You should read question from input.
The size of the input is not more than 30000.
Output
The answer.

2010年7月8日 星期四

[Timus Online Judge] 1025. Democracy in danger






1025. Democracy in danger
Time Limit: 1.0 second
Memory Limit: 16 MB
Background
In one of the countries of Caribbean basin all decisions were accepted by the simple majority of votes at the general meeting of citizens (fortunately, there were no lots of them). One of the local parties, aspiring to come to power as lawfully as possible, got its way in putting into effect some reform of the election system. The main argument was that the population of the island recently had increased and it was to longer easy to hold general meetings.
The essence of the reform is as follows. From the moment of its coming into effect all the citizens were divided into K (may be not equal) groups. Votes on every question were to be held then in each group, moreover, the group was said to vote “for” if more than half of the group had voted “for”, otherwise it was said to vote “against”. After the voting in each group a number of group that had voted “for” and “against” was calculated. The answer to the question was positive if the number of groups that had voted “for” was greater than the half of the general number of groups.
At first the inhabitants of the island accepted this system with pleasure. But when the first delights dispersed, some negative properties became obvious. It appeared that supporters of the party, that had introduced this system, could influence upon formation of groups of voters. Due to this they had an opportunity to put into effect some decisions without a majority of voters “for” it.
Let’s consider three groups of voters, containing 5, 5 and 7 persons, respectively. Then it is enough for the party to have only three supporters in each of the first two groups. So it would be able to put into effect a decision with the help of only six votes “for” instead of nine, that would be necessary in the case of general votes.
Problem
You are to write a program, which would determine according to the given partition of the electors the minimal number of supporters of the party, sufficient for putting into effect of any decision, with some distribution of those supporters among the groups.
Input
In the first line an only odd integer K — a quantity of groups — is written (1 ≤ K ≤ 101). In the second line there are written K odd integers, separated with a space. Those numbers define a number of voters in each group. The population of the island does not exceeds 9999 persons.
Output
You should write an only natural number — a minimal quantity of supporters of the party, that can put into effect any decision.







#include "stdio.h"

int main ()
{
long n,total;
long i,j;
long tmp;
long a[1000];

scanf ("%ld",&n);

for (i=1;i<=n;)
{
scanf ("%ld",&a[i]);
if (a[i]%2!=0) i++;
}


for (i=1;i<=n;i++)
for (j=i+1;j<=n;j++)
if (a[i]>a[j]) {
tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
total=0;
for (i=1;i<=n/2+1;i++)
total=total+a[i]/2+1;

printf ("%ld",total);
return (0);
}




Reference :
http://acm.timus.ru/problem.aspx?space=1&num=1025

2010年7月3日 星期六

2010年7月1日 星期四

[ACM] 2007 tag checker - how to take a letter out


#include
#include

int main()
{
int bytes_read;
//int nbytes = 100;
size_t nbytes = 100;
char *my_string;

puts ("Please enter a line of text.");

/* These 2 lines are the heart of the program. */
my_string = (char *) malloc (nbytes + 1);
bytes_read = getline (&my_string, &nbytes, stdin);

//Take particular letter out
printf("%c",my_string[2]);

if (bytes_read == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");
//puts (my_string);
int i=1;
while((i < bytes_read)&&(*my_string != '#')){
printf("%c\n",*my_string++);
i++;
}
}

return 0;
}

[ACM] 2007 tag checker - how to read a line and print every letter


#include
#include

int main()
{
int bytes_read;
//int nbytes = 100;
size_t nbytes = 100;
char *my_string;

puts ("Please enter a line of text.");

/* These 2 lines are the heart of the program. */
my_string = (char *) malloc (nbytes + 1);
bytes_read = getline (&my_string, &nbytes, stdin);

if (bytes_read == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");

// This is how to read a line and print every letter.
int i=1;
while(i < bytes_read){
printf("%c\n",*my_string++);
i++;

}
}

return 0;
}

2010年6月30日 星期三

strcmp


#include
#include

int main(void){

char name[10];

printf( "Enter your name: " );
scanf( "%s", name );


if( strcmp( name, "mary" ) == 0 )
printf( "Hello, Dr. Mary!\n" );

return 0;
}