标签:index 质数 oat bsp sans import pre osi 个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#include<stdio.h> int main() { int x,y,i; y=0; printf ( "请输入x的值\n" ); scanf ( "%d" ,&x); if (x%2==0) { for (i=0;i<=x;i=i+2) { y=y+i; } printf ( "输入的为偶数,y的值为%d\n" ,y); } else if (x%2!=0) { for (i+0;i<=x;i=i+2) { y=y+i; } printf ( "输入的为奇数,y的值为%d\n" ,y); } else { printf ( "输入错误\n" ); } return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include<stdio.h> int main() { double a,b,c,i; a=0; b=0; for (i=1;i<=99;i=i+2) { a=a+1.0/i; } for (i=2;i<=100;i=i+2) { b=b-1.0/i; } c=a+b; printf ( "%0.2lf" ,c); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include<stdio.h> int main() { int a,i; a=0; for (i=2000;i<=3000;i=i+4) { if (i%4==0&&i%100!=0||i%400==0) { a=a+1; printf ( " %d" ,i); if (a%10==0) printf ( "\n" ); } } printf ( "一共有%d个闰年" ,a); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include<stdio.h> int main() { int m,i; double x,y; y=1; printf ( "请输入一个实数x\n" ); scanf ( "%lf" ,&x); printf ( "请输入一个整数m\n" ); scanf ( "%d" ,&m); if (x>0) { for (i=1;i<=m;i=i+1) { y=y*x; } printf ( "%lf" ,y); } else if (x==0) { printf ( "0" ); } else { if (m%2==0) { for (i=1;i<=m;i=i+1) { y=y*x; } printf ( "%lf" ,y); } else if (m%2!=0) { for (i=1;i<=m;i=i+1) { y=-(y*x); } printf ( "%lf" ,-y); } } return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include<stdio.h> int main() { int a,b,c,d; char x; a=0; b=0; c=0; d=0; while ((x= getchar ()) != ‘\n‘ ) { if ( ‘A‘ <=x&&x<= ‘Z‘ || ‘a‘ <=x&&x<= ‘z‘ ) a++; else if (x== ‘ ‘ ) b++; else if ( ‘0‘ <=x&&x<= ‘9‘ ) c++; else d++; } printf ( "字母:%d\n" ,a); printf ( "空格:%d\n" ,b); printf ( "数字:%d\n" ,c); printf ( "其他:%d\n" ,d); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include<stdio.h> int main() { int a,b,x; double y,z; a=0; b=0; while (1) { scanf ( "%d" ,&x); if (x<0) { a++; y=y+x; } else if (x>0) { b++; z=z+x; } else break ; } if (a==0&&b!=0) printf ( "正数的平均值为%lf\n" ,z/b); else if (a!=0&&b==0) printf ( "负数的平均值为%lf\n" ,y/a); else if (a==0&b==0) printf ( "0" ); else if (a!=0&&b!=0) { printf ( "负数的平均值为%lf\n" ,y/a); printf ( "正数的平均值为%lf\n" ,z/b); } return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#include<stdio.h> int main() { int a,b,c,x,flag=0; c=0; for (a=2;a<=1000;a++) { flag=0; for (x=2;x<=a-1;x++) { if (a%x==0) { flag=1; break ; } } if (flag==0) { printf ( "% 4d" ,a); c=c+1; if (c%10==0) { printf ( "\n" ); } } } printf ( "\n一共有%d个质数\n" ,c); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include<stdio.h> int main() { int a,b; for (a=1;a<=5;a=a+1) { for (b=1;b<=a-1;b=b+1) { printf ( " " ); } for (b=1;b<=11-2*a;b=b+1) { printf ( "*" ); } printf ( "\n" ); } return 0; } |
总结1:循环时应找判断条件和变量的关系
2: 三种循环可以互相嵌套,不限层数
3:while 语句为先判断表达式,后执行循环。do whlie 为先执行循环 后判断表达式
#include<stdio.h> int main() { int x,y,z; printf("输入一个整数"); scanf("%d",&x); z=0; if (x%2==0) { for (y=2;y<=x;y+=2) z=z+y; printf("结果为%d",z); } else { for (y=1;y<=x;y+=2) z=z+y; printf("结果为%d",z); } return 0; }
#include<stdio.h> int main() { double a,b,c,d; a=0; b=0; for(d=1;d<=99;d=d+2) { a=a+1/d; } for(d=2;d<=100;d=d+2) { b=b-1/d; } c=a+b; printf("%.2lf",c); return 0; }
#include<stdio.h> int main() { int x,y=0; for(x=2000;x<=3000;x++) { if(x%400==0||x%4==0&&x%100!=0) { printf("%d ",x); y++; if(y%10==0) { printf("\n"); } } } printf("总共有%d个闰年",y); return 0; }
#include<stdio.h> int main() { float x,y=1.0; int a,b; printf("输入一个实数x,和一个整数b"); scanf("%f%d",&x,&b); for(a=1;a<=b;a++) { y=y*x; } printf("%.2f",y); return 0; }
#include<stdio.h> int main() { int a,b,c,d,e; a=0; b=0; c=0; d=0; e=0; printf("请输入一串字符\n"); while(e!=‘\n‘) { scanf("%c",&e); if((e>=‘A‘&&e<=‘Z‘)||(e>=‘a‘&&a<=‘z‘)) { a=a+1; } else if(e>=‘0‘&&e<=‘9‘) { b=b+1; } else if(e==‘ ‘) { c=c+1; } else { d=d+1; } } printf("一共有%d个字符,%d个字母,%d个空格,%d个其他\n",a,b,c,d); return 0; }
#include<stdio.h> int main() { int i=0,n=0; float a,b,c,d,e; for(scanf("%f",&a);a;scanf("%f",&a)) { if(a>0) { i=i+1; d=d+a; } else { n=n+1; e=e+a; } if(i==0) { b=0; } else { b=d/i; } if(n==0) { c=0; } else { c=e/n; } } printf("正数的平均数为;%f\n",b); printf("附属的平均数为;%f\n",c); return 0; }
#include<stdio.h> int main() { int a,b,c,d; b=0; c=0; for(a=1;a<=1000;a=a+1) { b=b+1; for(d=2;d<=b-1;d++) { if(b%d==0) { break; } } if(d==b) { printf(" %03d",d); c=c+1; if(c%10==0) printf("\n"); } } printf("一共有%d个质数\n",c); return 0; }
#include<stdio.h> int main() { int a,b; for(a=1;a<=5;a++) { for(b=1;b<=a-1;b++) printf(" "); for(b=1;b<=11-2*a;b++) printf("*"); printf("\n"); } return 0; }
标签:index 质数 oat bsp sans import pre osi 个数
原文地址:http://www.cnblogs.com/gengqihao/p/6014251.html