|
| 1 | +/**************************************** |
| 2 | +技巧01:求100-200之间的素数 |
| 3 | +****************************************/ |
| 4 | +/* |
| 5 | +#include <stdio.h> |
| 6 | +#include <math.h> |
| 7 | +main() |
| 8 | +{ |
| 9 | + int i,j,n=0; |
| 10 | + for(i=100;i<=200;i++) |
| 11 | + for(j=2;j<=sqrt(i);j++) |
| 12 | + if(i%j==0) |
| 13 | + break; |
| 14 | + else if(j>sqrt(i)-1) |
| 15 | + { |
| 16 | + printf ("%d ",i); |
| 17 | + n++; |
| 18 | + if(n%5==0) |
| 19 | + printf ("\n"); |
| 20 | + } |
| 21 | + return 0; |
| 22 | +} |
| 23 | +*/ |
| 24 | +/**************************************** |
| 25 | +技巧02:可逆素数 |
| 26 | +****************************************/ |
| 27 | + /* |
| 28 | +#include <stdio.h> |
| 29 | +#include <math.h> |
| 30 | + //自定义ss函数,作用是判断一个数是否为素数 |
| 31 | +int ss(int i) |
| 32 | +{ |
| 33 | + int j; |
| 34 | + if(i<=1) //小于1的数不是素数 |
| 35 | + return 0; |
| 36 | + if(i==2) //2是素数 |
| 37 | + return 1; |
| 38 | + for (j=2; j<i; j++) //对大于2的数进行判断 |
| 39 | + { |
| 40 | + if(i%j==0) |
| 41 | + return 0; |
| 42 | + else if(i!=j+1) |
| 43 | + continue; |
| 44 | + else |
| 45 | + return 1; |
| 46 | + } |
| 47 | +} |
| 48 | +int main(int argc, char *argv[]) |
| 49 | +{ |
| 50 | + int i,n=0,n1,n2,n3,n4; |
| 51 | + for(i=1000;i<10000;i++) |
| 52 | + if (ss(i)==1) |
| 53 | + { |
| 54 | + n4=i%10; |
| 55 | + n3=(i%100)/10; |
| 56 | + n2=(i/100)%10; |
| 57 | + n1=i/1000; |
| 58 | + if(ss(1000*n4+100*n3+10*n2+n1)==1 |
| 59 | + &&1000*n4+100*n3+10*n2+n1>i) |
| 60 | + { |
| 61 | + printf ("%d ",i); |
| 62 | + n++; |
| 63 | + if(n%10==0) |
| 64 | + printf ("\n"); |
| 65 | + } |
| 66 | + } |
| 67 | + return 0; |
| 68 | +} |
| 69 | +*/ |
| 70 | +/**************************************** |
| 71 | +技巧03:回文素数 |
| 72 | +****************************************/ |
| 73 | +/* |
| 74 | +#include <stdio.h> |
| 75 | +int ss(int i) |
| 76 | +{ |
| 77 | + int j; |
| 78 | + if(i<=1) |
| 79 | + return 0; |
| 80 | + if(i==2) |
| 81 | + return 1; |
| 82 | + for (j=2; j<i; j++) |
| 83 | + { |
| 84 | + if(i%j==0) |
| 85 | + return 0; |
| 86 | + else if(i!=j+1) |
| 87 | + continue; |
| 88 | + else |
| 89 | + return 1; |
| 90 | + } |
| 91 | +} |
| 92 | +int main(int argc, char *argv[]) |
| 93 | +{ |
| 94 | + int i; |
| 95 | + for (i=10; i<1000; i++) |
| 96 | + if(ss(i)==1) //判断是否是素数 |
| 97 | + if (i/100==0) //判断是否是两位数 |
| 98 | + { |
| 99 | + if (i/10==i%10) //判断十位和各位是否相同 |
| 100 | + printf ("%5d ",i); |
| 101 | + } |
| 102 | + else |
| 103 | + if(i/100==i%10) //判断百位和各位是否相同 |
| 104 | + printf ("%5d ",i); |
| 105 | + return 0; |
| 106 | +} |
| 107 | +*/ |
| 108 | +/**************************************** |
| 109 | +技巧04:特殊的完全平方数 |
| 110 | +****************************************/ |
| 111 | +/* |
| 112 | +#include <stdio.h> |
| 113 | +int main(int argc, char *argv[]) |
| 114 | +{ |
| 115 | + int i,j; |
| 116 | + int hun,ten,data; |
| 117 | + printf ("the result is:\n"); |
| 118 | + for (i=100; i<=999; i++) |
| 119 | + { |
| 120 | + j=10; |
| 121 | + while(j*j<=i) |
| 122 | + { |
| 123 | + if (i==j*j) |
| 124 | + { |
| 125 | + hun=i/100; |
| 126 | + ten=i%100/10; |
| 127 | + data=i%10; |
| 128 | + if(hun==ten||hun==data||ten==data) |
| 129 | + printf ("%5d ",i); |
| 130 | + } |
| 131 | + j++; |
| 132 | + } |
| 133 | + } |
| 134 | + return 0; |
| 135 | +} |
| 136 | +*/ |
| 137 | +/**************************************** |
| 138 | +技巧05:求1000以内的玩数 |
| 139 | +****************************************/ |
| 140 | + /* |
| 141 | +#include <stdio.h> |
| 142 | +main() |
| 143 | +{ |
| 144 | + int i,j,sum=0; |
| 145 | + for (i=1; i<1000; i++) |
| 146 | + { |
| 147 | + sum=0; |
| 148 | + for(j=1;j<i;j++) |
| 149 | + if(i%j==0) |
| 150 | + sum+=j; |
| 151 | + if(sum==i) |
| 152 | + printf ("%4d ",i); |
| 153 | + } |
| 154 | + return 0; |
| 155 | +} |
| 156 | + */ |
| 157 | +/**************************************** |
| 158 | +技巧06:三重回文数(未成功) |
| 159 | +****************************************/ |
| 160 | + /* |
| 161 | +#include <string.h> |
| 162 | +#include <stdio.h> |
| 163 | +#include <stdlib.h> |
| 164 | + //自定义judge函数,用来判断一个数是否是回文数 |
| 165 | + int judge(long n) |
| 166 | +{ |
| 167 | + int i,len,half; |
| 168 | + char a[20]; |
| 169 | + ltoa(n,a,10); //把常整形n转换成字符串存到数组a中 |
| 170 | + len=strlen(a); |
| 171 | + half=len/2; |
| 172 | + for(i=0;i<half;i++) |
| 173 | + if(a[i]!=a[--len])//判断相对应位置的字符串是否相同 |
| 174 | + break; |
| 175 | + if (i>=half) |
| 176 | + return 1; |
| 177 | + else |
| 178 | + return 0; |
| 179 | +} |
| 180 | +int main(int argc, char *argv[]) |
| 181 | +{ |
| 182 | + long n; |
| 183 | + printf ("the result is:\n"); |
| 184 | + for(n=11;n<1000;n++) |
| 185 | + if(judge(n)&&judge(n*n)&&judge(n*n*n))//判断三重回文数 |
| 186 | + printf ("n=%ld\tn*n=%ld\tn*n*n=%ld\n",n,n*n,n*n*n); |
| 187 | + return 0; |
| 188 | +} |
| 189 | + */ |
| 190 | +/**************************************** |
| 191 | +技巧07:亲密数 |
| 192 | +****************************************/ |
| 193 | +/* |
| 194 | +#include <stdio.h> |
| 195 | +int main(int argc, char *argv[]) |
| 196 | +{ |
| 197 | + int i,j,k,sum1,sum2; |
| 198 | + for (i=1; i<=10000; i++) |
| 199 | + { |
| 200 | + sum1=0; |
| 201 | + sum2=0; |
| 202 | + for(j=1;j<i;j++) |
| 203 | + if(i%j==0) |
| 204 | + sum1+=j; |
| 205 | + for(k=1;k<sum1;k++) |
| 206 | + if(sum1%k==0) |
| 207 | + sum2+=k; |
| 208 | + if(sum2==i&&i!=sum1&&i<sum1)//判断亲密数 |
| 209 | + printf ("%5d=>%5d\n",i,sum1); |
| 210 | + } |
| 211 | + return 0; |
| 212 | +} |
| 213 | +*/ |
| 214 | +/**************************************** |
| 215 | +技巧08:自守数(没写完) |
| 216 | +****************************************/ |
| 217 | + /* |
| 218 | +#include <stdio.h> |
| 219 | +int main(int argc, char *argv[]) |
| 220 | +{ |
| 221 | + long i,j,k1,k2,k3,a[10]={0},num,m,n,sum; |
| 222 | + printf ("please input a number:\n"); |
| 223 | + scanf("%ld",&num); |
| 224 | + printf ("the result is:\n"); |
| 225 | + for (j=0; j<num; j++) |
| 226 | + { |
| 227 | + m=j; |
| 228 | + n=1; |
| 229 | + sum=0; |
| 230 | + k1=10; |
| 231 | + k2=1; |
| 232 | + while(m!=0) //判断该数的位置 |
| 233 | + { |
| 234 | + a[n]=j%k1; |
| 235 | + |
| 236 | + } |
| 237 | + } |
| 238 | + return 0; |
| 239 | +} |
| 240 | + */ |
| 241 | +/**************************************** |
| 242 | +技巧09:满足abcd=(ab+cd)^2的数 |
| 243 | +****************************************/ |
| 244 | + /* |
| 245 | +#include <stdio.h> |
| 246 | +int main(int argc, char *argv[]) |
| 247 | +{ |
| 248 | + int i,a,b; |
| 249 | + for (i=1000; i<10000; i++) |
| 250 | + { |
| 251 | + a=i/100; //求出该数的前两位数; |
| 252 | + b=i%100; //求出该数的后两位数; |
| 253 | + if((a+b)*(a+b)==i) |
| 254 | + printf ("%5d\n",i); |
| 255 | + } |
| 256 | + return 0; |
| 257 | +} |
| 258 | + */ |
| 259 | +/**************************************** |
| 260 | +技巧10:神奇的数字6174 |
| 261 | +****************************************/ |
| 262 | + /* |
| 263 | +#include <stdio.h> |
| 264 | + //自定义函数,实现求最大4位数与最小数的差 |
| 265 | + int difference(int a[]) |
| 266 | +{ |
| 267 | + int t,i,j,sum,sum1,sum2; |
| 268 | + for(i=0;i<3;i++) |
| 269 | + for(j=i+1;j<4;j++) |
| 270 | + if (a[i]<a[j]) |
| 271 | + { |
| 272 | + t=a[i]; |
| 273 | + a[i]=a[j]; |
| 274 | + a[j]=t; |
| 275 | + } |
| 276 | + sum1=a[0]*1000+a[1]*100+a[2]*10+a[3]; |
| 277 | + sum2=a[3]*1000+a[2]*100+a[1]*10+a[0]; |
| 278 | + sum=sum1-sum2; |
| 279 | + printf ("%5d=%5d-%5d\n",sum,sum1,sum2); |
| 280 | + return sum; |
| 281 | +} |
| 282 | +int main(int argc, char *argv[]) |
| 283 | +{ |
| 284 | + int n,a[4]; |
| 285 | + printf ("please input a number:\n"); |
| 286 | + scanf("%d",&n); |
| 287 | + while(n!=6174) |
| 288 | + { |
| 289 | + a[0]=n/1000; |
| 290 | + a[1]=n/100%10; |
| 291 | + a[2]=n/10%10; |
| 292 | + a[3]=n%10; |
| 293 | + n=difference(a); |
| 294 | + } |
| 295 | + return 0; |
| 296 | +} |
| 297 | + */ |
| 298 | + |
0 commit comments