标签:算法
1,有几位数字
#include<stdio.h> int main_2_1_digit(){ int n; while(scanf("%d",&n)){ int count = 0; if(n==0) count = 1; while(n){ count++; n/=10; } printf("%d\n",count); } return 0; }
2,三位数的三个数字
#include<stdio.h> int main_2_2_daffodil(){ int i,j,k; for(int n=100;n<=999;n++){ i = n % 10; j = (n/10) % 10; k = (n/100) % 10; if( n == i*i*i + j*j*j + k*k*k){ printf("%d\n",n); } } return 0; }
3,韩信点兵
#include<stdio.h> int main_2_3_hanxin(){ int a,b,c; int i,sum; while(scanf("%d%d%d",&a,&b,&c)){ sum = -1; for(i=10;i<=100;i++){ if(i%3 == a && i%5 == b && i%7 == c){ sum =i; break; } } if(sum == -1){ printf("No answer\n"); }else{ printf("%d\n",sum); } } return 0; }
4,打印倒三角
#include<stdio.h> int main_2_4_triangle(){ int n; int i,j; while(scanf("%d",&n)){ for(i=n;i>0;i--){ for(j=1;j < n;j++){ if(j+i >= n+1){ printf("#"); }else{ printf(" "); } } printf("#"); for(j=n+1;j < 2*n;j++){ if(j-i <= n-1){ printf("#"); }else{ printf(" "); } } printf("\n"); } } return 0; }
5,输入n个数,比m小的
#include<stdio.h> int main_2_5_stat(){ int arr[1000]; int n,m; int i; int count; while(scanf("%d",&n)){ for(i=0;i<n;i++){ scanf("%d",&arr[i]); } scanf("%d",&m); count=0; for(i=0;i<n;i++){ if(m > arr[i]){ count++; } } printf("%d\n",count); } return 0; }
6,数aabb
#include<stdio.h> #include<math.h> int main_aabb_complete(){ int a,b,n; double m; for(a=1;a<=9;a++){ for(b=0;b<=9;b++){ n=a*1100 + b*11; m=sqrt((double)n); //判断m是否为整数 //进行浮点数比较的时候要考虑浮点误差 if(floor(m+0.5) == m){ printf("%d %lf\n",n,m); } } } getchar(); return 0; }
7,浮点数陷阱
#include<stdio.h> int main_float_trap(){ double i; for(i=9;i!=10;i+=0.1){ printf("%.1lf\n",i); if(i>10){ break; } } return 0; }
8,varble_no_init
#include<stdio.h> int main_varble_no_init(){ int a,b,c; printf("%d %d %d",a,b,c); scanf("%d",&a); return 0; }
9,全排列
#include<stdio.h> int n = 0; void swap(int *a,int *b){ int m = *a; *a = *b; *b = m; } void perm(int list[],int k,int m){ int i; if(k > m){ for(i=0;i<=m;i++){ printf("%d ",list[i]); } printf("\n"); n++; }else{ for(i=k;i<=m;i++){ swap(&list[k],&list[i]); perm(list,k+1,m); swap(&list[k],&list[i]); } } } int main_quan_pai_lie_version_1(){ int list[] = {1,2,3,4,5}; perm(list,0,4); printf("total: %d\n",n); return 0; }
算法竞赛_入门经典_刘汝佳__(2),布布扣,bubuko.com
标签:算法
原文地址:http://blog.csdn.net/xuerenlv123/article/details/25100345