2015年7月20日 星期一

[C][瘋狂程設][CPE考題20141223] 14G2.UVA10235:Simply Emirp


Problem G: Simply Emirp


An integer greater than 1 is called a prime number if its only positive divisors (factors) are 1 and itself. Prime numbers have been studied over the years by a lot of mathematicians. Applications of prime numbers arise in Cryptography and Coding Theory among others.
Have you tried reversing a prime ? For most primes, you get a composite (43 becomes 34). An Emirp (Prime spelt backwards) is a Prime that gives you a different Prime when its digits are reversed. For example, 17 is Emirp because 17 as well as 71 are Prime. In this problem, you have to decide whether a number N is Non-prime or Prime or Emirp. Assume that 1< N< 1000000.
Interestingly, Emirps are not new to NTU students. We have been boarding 199 and 179 buses for quite a long time!

Input


Input consists of several lines specifying values for N.

Output


For each N given in the input, output should contain one of the following:
    1. "N is not prime.", if N is not a Prime number.
    2. "N is prime.", if N is Prime and N is not Emirp.
    3. "N is emirp.", if N is Emirp.

Sample Input

17
18
19
179
199

Sample Output

17 is emirp.
18 is not prime.
19 is prime.
179 is emirp.
199 is emirp.

Notes:


1.判斷是否為質數,若為質數則回傳1,不是質數則回傳0

/*return value: 1=prime, 0=not prime*/
int is_prime(int *n){

 int num,i,result=0;
 num=*n; 
  
 // num == 2: 2 is prime.
 // num >  2: num is even, num is not prime.
 if(num%2==0){
  //printf("**[%d]\n",__LINE__);
  (num==2)?(result=1):(result=0);
  return result;
 }
 
 for(i=3;i*i<=num;i+=2){
  //printf("**[%d]\n",__LINE__);
  if(num%i==0){
   //printf("**[%d]\n",__LINE__);
   result=0;
   return result;
  }
  //printf("**[%d]\n",__LINE__);
 }
 result=1;
 return result;
}


2.整數轉字串、字串反轉、字串轉整數

#include <string.h>
int input,rev_input;
int value;
char temp[7];
char *rev_temp;

value=is_prime(&input);
sprintf(temp,"%d",input);
rev_temp=strrev(temp);
rev_input=atoi(rev_temp);

3.debug專用,顯示行號的標籤"__LINE__"

printf("**[%d]\n",__LINE__);

4.利用兩個三元運算子(條件運算子 conditional operator),化簡三選一的if else

三選一:
  if(value==1)
   printf("%d %s\n",input,PRIME);
  else if(value==2)
   printf("%d %s\n",input,EMIRP);
  else
   printf("%d %s\n",input,NOT_PRIME);
利用兩個條件運算子化簡:
  printf("%d %s\n",input,(value>1)?(EMIRP):((value<1)?NOT_PRIME:PRIME));

Code:


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

#define PRIME "is prime."
#define NOT_PRIME "is not prime."
#define EMIRP "is emirp."

/*return value: 1=prime, 0=not prime*/
int is_prime(int *n){

 int num,i,result=0;
 num=*n; 
  
 // num == 2: 2 is prime.
 // num >  2: num is even, num is not prime.
 if(num%2==0){
  (num==2)?(result=1):(result=0);
  return result;
 }
 
 for(i=3;i*i<=num;i+=2){
  if(num%i==0){
   result=0;
   return result;
  }
 }
 result=1;
 return result;
}

int main(void){

 int input,rev_input;
 int value;
 char temp[7];
 char *rev_temp;
 
 while(scanf("%d",&input)!=EOF){

  value=0;  
  value=is_prime(&input);
  sprintf(temp,"%d",input);
 
  if(value>0){
   rev_temp=strrev(temp);
   rev_input=atoi(rev_temp);
   value+=is_prime(&rev_input);
  }
 
  if(value!=0 && input==rev_input)
   value--;
  
  printf("%d %s\n",input,(value>1)?(EMIRP):((value<1)?NOT_PRIME:PRIME));



[C][瘋狂程設][CPE考題20141223] 14G3.UVA264:Count on Cantor

Count on Cantor

One of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational numbers is enumerable. The proof works by using an explicit enumeration of rational numbers as shown in the diagram below.
displaymath27
In the above diagram, the first term is 1/1, the second term is 1/2, the third term is 2/1, the fourth term is 3/1, the fifth term is 2/2, and so on.

Input and Output

You are to write a program that will read a list of numbers in the range from 1 to tex2html_wrap_inline29 and will print for each number the corresponding term in Cantor's enumeration as given below. No blank line should appear after the last number.
The input list contains a single number per line and will be terminated by end-of-file.

Sample input

3
14
7

Sample output

TERM 3 IS 2/1
TERM 14 IS 2/4
TERM 7 IS 1/4




Code:


#include <stdio.h>

int main(void){
 int input;
 int num,i,level,sum,value,x,y;
 
 while(scanf("%d",&input)!=EOF){
  num=input;
  i=sum=level=value=0;
  
  while(sum<num){
   i++;
   sum+=i;
   level++;
  }
  value=level+1;
  
  //printf("i=%d,level=%d,sum=%d\n",i,level,sum);
  
  if(level%2==0){
   y=1+(sum-num);
   x=value-y;
  }else{
   x=1+(sum-num);
   y=value-x;
  }
  
  printf("TERM %d IS %d/%d\n",input,x,y);
 }

 return 0;
}

[C][瘋狂程設][CPE考題20150324] 15A3.UVA579:Clock Hands

ClockHand

The medieval interest in mechanical contrivances is well illustrated by the development of the mechanical clock, the oldest of which is driven by weights and controlled by a verge, an oscillating arm engaging with a gear wheel. It dates back to 1386.
Clocks driven by springs had appeared by the mid-15th century, making it possible to con- struct more compact mechanisms and preparing the way for the portable clock.
English spring-driven pendulum clocks were first commonly kept on a small wall bracket and later on a shelf. Many bracket clocks contained a drawer to hold the winding key. The earliest bracket clocks, made for a period after 1660, were of architectural design, with pillars at the sides and a pediment on top.
In 17th- and 18th-century France, the table clock became an object of monumental design, the best examples of which are minor works of sculpture.
The longcase clocks (also called grandfather clocks) are tall pendulum clock enclosed in a wooden case that stands upon the floor and is typically from 6 to 7.5 feet (1.8 to 2.3 m) in height. Later, the name ``grandfather clock'' became popular after the popular song "My Grandfather's Clock," written in 1876 by Henry Clay Work.
One of the first atomic clocks was an ammonia-controlled clock. It was built in 1949 at the National Bureau of Standards, Washington, D.C.; in this clock the frequency did not vary by more than one part in 108
Nuclear clocks are built using two clocks. The aggregate of atoms that emit the gamma radiation of precise frequency may be called the emitter clock; the group of atoms that absorb this radiation is the absorber clock. One pair of these nuclear clocks can detect energy changes of one part in 1014 , being about 1,000 times more sensitive than the best atomic clock.
The cesium clock is the most accurate type of clock yet developed. This device makes use of transitions between the spin states of the cesium nucleus and produces a frequency which is so regular that it has been adopted for establishing the time standard.
The history of clocks is fascinating, but unrelated to this problem. In this problem, you are asked to find the angle between the minute hand and the hour hand on a regular analog clock. Assume that the second hand, if there were one, would be pointing straight up at the 12. Give all angles as the smallest positive angles. For example 9:00 is 90 degrees; not -90 or 270 degrees.

Input

The input is a list of times in the form H:M, each on their own line, with $1 \le H \le 12$ and $00 \le M \le 59$. The input is terminated with the time 0:00. Note that H may be represented with 1 or 2 digits (for 1-9 or 10-12, respectively); M is always represented with 2 digits (The input times are what you typically see on a digital clock).

Output

The output displays the smallest positive angle in degrees between the hands for each time. The answer should between 0 degrees and 180 degrees for all input times. Display each angle on a line by itself in the same order as the input. The output should be rounded to the nearest 1/1000, i.e., three places after the decimal point should be printed.

Sample Input

12:00
9:00
8:10
0:00

Sample Output

0.000
90.000
175.000

Notes:


1.說明常數定義

#define DEGREE_HOUR 30 //時針每個小時轉30度(30=360/12)
#define DEGREE_MINS 6 //分針每個小時轉6度(6=360/60)
#define DEGREE_MAX 180.000 //題目規定答案要取銳角
#define PRECISION 3 //題目規定角度要取到小數點後面3位數
#define MINUTE 60 //一個小時有60分鐘

2.輸出的數字,準確度到小數點後面三位

#define PRECISION 3
printf("%.*f\n",PRECISION,result);

3. mod 的限制

% : mod運算只能用在整數,所以,若要得到銳角,不能將直接將角度除以180取餘數。

4.scanf 的用法,抓取格式化的數字輸入:

 float hr,min,result;
 scanf("%f:%f",&hr,&min);

5.輸入的中止"0:00":


        scanf("%f:%f",&hr,&min);
 while( hr!=0 || min!=0 ){
             ....
        }


Code:

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

#define DEGREE_HOUR 30
#define DEGREE_MINS 6
#define DEGREE_MAX 180.000
#define PRECISION 3
#define MINUTE 60

int main(void){
 float hr,min,result;
 
 scanf("%f:%f",&hr,&min);
 
 while( hr!=0 || min!=0 ){
  
  hr=(hr+min/MINUTE)*DEGREE_HOUR;
  min*=DEGREE_MINS;
  (hr>min)?(result=hr-min):(result=min-hr);
  if(result>DEGREE_MAX)
   result=2*DEGREE_MAX-result;
  printf("%.*f\n",PRECISION,result);
  scanf("%f:%f",&hr,&min);
 }

 return 0;
} 

[C][瘋狂程設][CPE考題20150324] 15A2.UVA10783:Odd Sum

Odd Sum

Given a range [a,b], you are to find the summation of all the odd integers in this range. For example, the summation of all the odd integers in the range [3,9] is 3 + 5 + 7 + 9 = 24.

Input

There can be at multiple test cases. The first line of input gives you the number of test cases,T (1≤ T≤ 100) Then T test cases follow. Each test case consists of 2 integers a and b (0≤ a≤ b≤ 100 )in two separate lines.

Output

For each test case you are to print one line of output - the serial number of the test case followed by the summation of the odd integers in the range [a, b].

Sample Input

2
1
5
3
5

Sample Output

Case 1: 9
Case 2: 8

Notes:


1.儲存輸入的資料

total:表示資料有幾筆,也就是有多少個test case pairs陣列:存放每一筆輸入的起點a與終點b

 int total;
 scanf("%d", &total);

 int input=2*total;
 int pairs[input];
 int i=1,start,end,k,sum;
 
 while(i<=input){
  scanf("%d",&pairs[i]);
  i++;
 }


Code:

#include <stdio.h>

int main(void){

 int total;
 scanf("%d", &total);

 int input=2*total;
 int pairs[input];
 int i=1,start,end,k,sum;
 
 while(i<=input){
  scanf("%d",&pairs[i]);
  i++;
 }
 
 for(i=1;i<=input;i+=2){
  sum=0;
  (pairs[i]%2)?start=pairs[i]:start=pairs[i]+1;
  (pairs[i+1]%2)?end=pairs[i+1]:end=pairs[i+1]-1;
  for(k=start;k<=end;k+=2)
   sum+=k;
  printf("Case %d: %d\n",(i/2+1),sum);
 }
 
 return 0;
}