Today is my beginning, formally joined the C language, C + + programming This long-distance team. The reason to join this is also very simple, every time through a short program to solve a problem, that feeling is still proud of it! Small code, sometimes very short, contains the logical thinking is very rich, very ingenious!
Share some of the code and experience summarized yesterday:
Title 1: See picture
My program (positive solution):
/*Cao Tan The opposite number of the Little Prince by It_greenhand 2014/11/30*/#include<stdio.h>#include<string.h>#include<stdbool.h>#defineN 1000000000Long intN,no,i,len;BOOLA[n];intMain () {intm; I=0; Memset (A,false,sizeof(a)); while(SCANF ("%ld", &n)! =EOF) {No=0; i=0;//Note 0 while(n!=0) {m= n%2; N= n/2; if(m) a[i]=true; Elsea[i]=false; I++; } Len=i; I=0; while(!a[i]) i++; for(; i<len;i++) { //if (A[i]) printf ("1"); //Else printf ("0"); if(A[i]) No = no*2+1; ElseNo = no*2; } printf ("%d\n", no); } return 0;}
This question involves the son binary system thought, the feeling is still relatively unfamiliar, is a long insight!
The idea is: first convert the input decimal number into binary (True/false) inverted order in the array, then the positive order into decimal, and finally get the opposite number to meet the requirements.
Original question 2 (see picture):
My Code:
#include<stdio.h>#include<math.h>intMain () {unsignedLong intA; intcnt,i,k; while(SCANF ("%ld", &a)! =EOF) { if(a!=1) {CNT=0; K=sqrt (a); for(i=1; I<sqrt (a); i++) { if(a%i==0) CNT+=2; } if(K*k = = a) cnt++; printf ("%d\n", CNT); } Elseprintf ("1\n"); } return 0;}
Summarize:
1, for the data 2^31 test does not pass, long int data range ( -2^31,2^31-1), in this paste the data range summary
unsigned int 0~4294967295
int-2147483648~2147483647
unsigned long 0~4294967295
Long 2147483648~2147483647
The maximum value of long long: 9223372036854775807
The minimum value of long long:-9223372036854775808
Unsigned the maximum value of long long: 18446744073709551615
Maximum value of __int64:9223372036854775807
Minimum value of __int64:9223372036854775808
Maximum value of unsigned __int64:18446744073709551615
Positive number is unsigned __int64, take to 2^64-1, exceed to use high precision algorithm.
2. Find the number of conventions only sqrt (a) can reduce the complexity of time, is an optimization, and here can not be equated.
The beginning!