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

2015年7月27日 星期一

[C][瘋狂程設][CPE考題20140325] 14A3.UVA10018:Reverse and Add

Reverse and Add

The "reverse and add" method is simple: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number from left to right and right to left), repeat this procedure. 
For example: 
195 Initial number 
591 
----- 
786 
687 
----- 
1473 
3741 
----- 
5214 
4125 
----- 
9339 Resulting palindrome 

In this particular case the palindrome 9339 appeared after the 4th addition. This method leads to palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a palindrome. 

Task : 
You must write a program that give the resulting palindrome and the number of iterations (additions) to compute the palindrome. 

You might assume that all tests data on this problem: 
- will have an answer , 
- will be computable with less than 1000 iterations (additions), 
- will yield a palindrome that is not greater than 4,294,967,295. 

Input

The first line will have a number N with the number of test cases, the next N lines will have a number P to compute its palindrome.
3
195
265
750

Output

For each of the N tests you will have to write a line with the following data : minimum number of iterations (additions) to get to the palindrome and the resulting palindrome itself separated by one space.
4 9339 
5 45254 
3 6666 

Notes:


1.反轉輸入的數字

void reverse_data(int *num,int size,char rev_data[]){

 int m=*num;
 char data[size];
 
 /*------reverse------*/
 sprintf(data,"%d",m);
 strcpy(rev_data,data);
 strrev(rev_data);
 //printf("[%d]%d,%s,%s\n",__LINE__,size,data,rev_data);
}

Code:


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

void reverse_data(int *num,int size,char rev_data[]){

 int m=*num;
 char data[size];
 
 /*------reverse------*/
 sprintf(data,"%d",m);
 strcpy(rev_data,data);
 strrev(rev_data);
 //printf("[%d]%d,%s,%s\n",__LINE__,size,data,rev_data);
}

int rev_and_add(int *num){

 int m=*num;
 int palindrome;
 int length=sizeof(m);
 char rev_num[length];
 
 /*------reverse------*/
 reverse_data(&m,length,rev_num);
 
 /*------add------*/
 m+=atoi(rev_num);
 length=sizeof(m);
 reverse_data(&m,length,rev_num);
 
 /*-----test palindrome, 0 means yes, 1 means no.-----*/
 char sum[length];
 sprintf(sum,"%d",m);
 //printf("[%d]%s,%s\n",__LINE__,sum,rev_num);
 
 (strcmp(sum,rev_num)==0)?palindrome=0:palindrome=1;
 //printf("[%d],p=%d\n",__LINE__,palindrome);
 *num=m;
 return palindrome;
 
 //return 0;
}

int main(void){

 int total,count,i=0,input,act;
 
 scanf("%d",&total);
 
 while(i<total){
  count=0;
  i++;
  scanf("%d",&input);
  
  do{
   //printf("[%d]count=%d\n",__LINE__,count);
   act=rev_and_add(&input);
   count++;
  }while(act==1);
   
  printf("%d %d\n",count,input);
 }

 return 0;
}

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));