#include<cstring>#include<stdlib.h>#include<stdio.h>#include<math.h>intmain(){int n,a,i,j,s=0;long sum =0;scanf("%d%d",&a,&n);for(i=0;i<n;i++){
s = s*10+ a;
sum += s;}printf("%ld ",sum);return0;}
intmain(){int i ;float s =1;for(i =2;i<=100;i=i+2){
s = s *( i * i)/((i-1)*(i+1));}printf("%f",s*2);return0;}
利用泰勒级数计算sin(x)的值
x^3 x^5 x^7 x^9
sin(x)= x - — + — - — + — - ...
3! 5! 7!9!
要求最后一项的绝对值 fabs() 小于 1e-5 并统计出此时累计了多少项
note:
初始化第一项为 x
分子: x的n次方,从x开始 保留结果 每项 * x * x (每次加两个x)
分母: 1*2*3 *4*5 *6*7 保留结果
code:
tmp = x
tmp = -tmp * x * x / ((n+1)*(n+2))
intmain(){double sum,tmp;float x;int i,n =1;scanf("%f",&x);for( i =1,sum = x,tmp = x;fabs(tmp)>=1e-5;i++){
tmp =-tmp*x*x/((n+1)*(n+2));
n = n +2;
sum += tmp;}printf("sin( %f ) = %f, count = %d",x,sum,i+1);return0;}
打印三位水仙花数
例如: 135=1^3 + 3^3 + 5^3
135是水仙花数
#include<cstring>#include<stdlib.h>#include<stdio.h>#include<math.h>intmain(){int i,a,b,c;for(i =100;i<1000;i++){
a = i/100;
c = i%10;
b = i/10%10;if(i ==(a*a*a + b*b*b + c*c*c)){printf("%d\n",i);}}return0;}
输入 : 1234 分离出 1 2 3 4 四个数 输出 1 + 2 + 3 + 4 = 10
code :
input : n
tmp = n % 10;sum+= tmp;
n /=10;
#include<cstring>#include<stdlib.h>#include<stdio.h>#include<math.h>intmain(){int n,i,tmp,sum =0;scanf("%d",&n);for(i =0;n>0;i++){
tmp = n%10;
sum += tmp;
n /=10;}printf("%d",sum);return0;}
回文数
123214004
从左到右读 == 从右到左读
note:
将数 从左到右重新组合,若与原数相等 即为回文数
12341 ——>1432112321 ——>12321
code:
input :s
r = s % 10;
m =10 * m + r;
s = s / 10;
#include<cstring>#include<stdlib.h>#include<stdio.h>#include<math.h>intmain(){int n , m =0,s,r;scanf("%d",&n);
s = n;while(s !=0){
r = s%10;
m =10*m+r;
s = s /10;}if(m == n){printf("yes");}else{printf("no");}return0;}
#include<cstring>#include<stdlib.h>#include<stdio.h>#include<math.h>intmain(){int x,y,z,count =0;for(x=1;x<=29;x++)for(y =1;y<=72;y++){
z =100-x -y;if(5*x +2*y + z ==150){
count++;printf("%02d,%02d,%02d ",x,y,z);if(count %6==0){printf("\n");}}}printf("count = %d\n",count);return0;}
四个学生 A B C D中一个孬 看谁是 孬
校长问 谁孬
ABCD一人一个理由
四个理由 三个真 一个假
枚举 A B C D孬
当满足三个理由为真 一个假
揪出孬娃
code:
thisman ='A' + k;sum=(条件1) + (条件2) + (条件3) + (条件4)sum=3true
#include<cstring>#include<stdlib.h>#include<stdio.h>#include<math.h>intmain(){int k =0,sum =0,g =0;char thisman =' ';for(k =0;k<=3;k++){
thisman ='A'+ k;
sum =(thisman!='A')+(thisman =='C')+(thisman =='D')+(thisman !='D');if(sum ==3){printf("This man is %c\n",thisman);
g =1;}}if(g!=1)printf("NONONO");return0;}
#include<cstring>#include<stdlib.h>#include<stdio.h>#include<math.h>#defineNUM10intmain(){int flag =0,a[NUM]={1,29,3,4,5,6,7,8,9,10},i,j,t;for(i =1;i<NUM;i++){for(j =0;j<NUM-i;j++)if(a[j]> a[j+1]){
t = a[j];
a[j]= a[j+1];
a[j+1]= t;
flag =1;}if(flag ==0){break;}
flag =0;}for(i =0;i<NUM;i++)printf("%d ",a[i]);return0;}
选择排序
找出每次排序中最小的数放在最前面
第二次排序 排除首位,向前移
code:
for(i =0;i<num-1;i++){
k = i;
for(j = i+1;j<10;j++){
if(a[j]< a[k]){
k = j;}}
if(k != i){
t = a[k];
a[k]= a[i];
a[i]= t;}}
#include<cstring>#include<stdlib.h>#include<stdio.h>#include<math.h>intmain(){int min,i,j,k,t;int a[10]={898,456,23,65,1896,35,4285,685,3,856};for(i =0;i<9;i++){
k = i;for(j = i+1;j<10;j++){if(a[j]< a[k]){
k = j;}}if(k != i){
t = a[k];
a[k]= a[i];
a[i]= t;}}for(i =0;i<10;i++)printf("%d ",a[i]);return0;}
输出如下:
please input the mark of 1thcourseif 1th student:96
please input the mark of 2thcourseif 1th student:89
please input the mark of 3thcourseif 1th student:95
please input the mark of 4thcourseif 1th student:96
please input the mark of 1thcourseif 2th student:98
please input the mark of 2thcourseif 2th student:86
please input the mark of 3thcourseif 2th student:85
please input the mark of 4thcourseif 2th student:84
please input the mark of 1thcourseif 3th student:82
please input the mark of 2thcourseif 3th student:75
please input the mark of 3thcourseif 3th student:76
please input the mark of 4thcourseif 3th student:74
please input the mark of 1thcourseif 4th student:72
please input the mark of 2thcourseif 4th student:95
please input the mark of 3thcourseif 4th student:83
please input the mark of 4thcourseif 4th student:69
please input the mark of 1thcourseif 5th student:94
please input the mark of 2thcourseif 5th student:75
please input the mark of 3thcourseif 5th student:62
please input the mark of 4thcourseif 5th student:71
NO. C1 C2 C3 C4 AVER
STD1 96.089.095.096.094.0
STD2 98.086.085.084.088.2
STD3 82.075.076.074.076.8
STD4 72.095.083.069.079.8
STD5 94.075.062.071.075.5
------------------------------------------------------
AVER_C 88.484.080.278.8
#defineNUM_std5#defineNUM_course4intmain(){int i,j;float score[NUM_std+1][NUM_course+1]={0};for(i =0;i<NUM_std;i++)for(j =0;j<NUM_course;j++){printf("please input the mark of %dthcourseif %dth student:",j+1,i+1);scanf("%f",&score[i][j]);}for(i =0;i<NUM_std;i++){for(j =0;j<NUM_course;j++){
score[i][NUM_course]+= score[i][j];
score[NUM_std][j]+=score[i][j];}
score[i][NUM_course]/=NUM_course;//个人平均成绩}for(j=0;j<NUM_course;j++)
score[NUM_std][j]/= NUM_std;//课程平均成绩 printf(" NO. C1 C2 C3 C4 AVER\n");for(i=0;i<NUM_std;i++){printf("STD%d\t",i+1);for(j =0;j<NUM_course+1;j++){printf("%6.1lf\t\t",score[i][j]);}printf("\n");}printf("------------------------------------------------------");printf("\nAVER_C ");for(i =0;i<NUM_course;i++){printf("%6.1lf\t\t",score[NUM_std][i]);}printf("\n");return0;}
统计单词的个数
i am shabby Y.
there are 4 words
--------------------------------
Process exited after 13.28 seconds with return value 0
请按任意键继续. ..
#defineIN1#defineOUT0intmain(){int word = OUT, num =0,i;char a[80],c;gets(a);//输入字符 包含空格 遇到回车停止for(i =0;(c = a[i])!='\0';i++)if(c==' ')
word = OUT;elseif(word == OUT){
word = IN;
num++;}printf("there are %d words",num);return0;}
三阶幻方
#defineMAX15intmain(){int m,mm,i,j,k,ni,nj;int mangic[MAX][MAX];printf("INPUT:");scanf("%d",&m);if((m<=0)||(m%2==0)){printf("Error in input data..\n");return0;}
mm = m*m;
i =0;
j = m/2;for(k=1;k<=mm;k++){
mangic[i][j]= k;if(i==0)
ni = m-1;else
ni = i -1;if(j==m-1)
nj=0;else
nj=j+1;if(mangic[ni][nj]==0){
i = ni;
j = nj;}else{
i++;}for(i =0;i<m;i++){for(j =0;j<m;j++)printf("%4d",mangic[i][j]);printf("\n");}return0;}