7744 problem (output of all 4-bit full squares of all shapes such as AABB)
Method 1:
#include <stdio.h>#include<math.h>intMain () { for(intA=1; a<=9; a++){ for(intb=0; b<=9; b++){ intn = A *1100+b* One; intm = Floor (sqrt (n) +0.5); if(m*m = = N) printf ("%d\n", N); } } return 0; }
Method 2:
#include <stdio.h>#include<math.h>intMain () { for(intx=1;; X + +){ intn = x*x; if(N < +)Continue; if(N >9999) Break; if(n/ += = n/ -%Ten&& n/Ten% -%Ten= = n%Ten) printf ("%d\n", N); } return 0;}
Turn on the light question:
n Lamp, K personal
#include <stdio.h>#include<string.h>inta[ +];intMain () {intN,k,first =1; Memset (A,0,sizeof(a)); scanf ("%d%d",&n,&k); for(intI=1; i<=k;i++){ for(intj=1; j<=n;j++){ if(j%i==0) a[j]=!A[j]; } } for(intI=1; i<=n;i++){ if(A[i]) printf ("%d", i); } return 0;}
Generate meta
Given n, the minimum generator (if the sum of the numbers of x plus x is y, then x is the generating element of y)
#include <stdio.h>#include<string.h>inta[100005];intMain () {intT,n; Memset (A,0,sizeof(a)); for(intm=1;m< the; m++){ intx = M,y =m; while(x>0) {y+=x%Ten; X/=Ten; } if(A[y] = =0|| A[y] > m) a[y]=m; } scanf ("%d",&T); while(t--) {scanf ("%d",&N); printf ("%d\n", A[n]); } return 0;}
Creating an arrangement of 1~n
#include <stdio.h>#include<string.h>inta[ -];voidFintNintBintcur) { if(n==cur) { for(intI=0; i<n;i++) {printf ("%d", A[i]); } printf ("\ n"); } Else for(intI=1; i<=n;i++){ intOK =1; for(intj=0; j<cur;j++){ if(a[j]==i) OK =0; } if(OK) {A[cur]=i; F (N,a,cur+1); } }}intMain () {f (3A0); return 0;}
Number of solutions to the N Queen problem:
#include <stdio.h>#include<string.h>inta[ -];LongTotal=0;intN;voidSearchintcur) { if(cur==n) total++; Else for(intI=0; i<n;i++){ intok=1; A[cur]=i; for(intj=0; j<cur;j++){ if(A[cur]==a[j] | | cur-a[cur]==j-a[j] | | cur+a[cur]==j+A[j]) {OK=0; Break; } } if(OK) search (cur+1); }}intMain () {scanf ("%d",&N); Search (0); printf ("%ld\n", total); return 0;}
Tip
printf ("%5d", X); %5d means printing in 5 digits, less than 5 bits in front of the space
scanf ("%s", s); Encounter blank word inode stop.
printf ("%.1f", X); Keep one after the decimal point
C Language Foundation (i)