#include<stdio.h>
int main(void){
int a, b, c;
int abc, bcc;for(a =0; a <10; a++){for(b =0; b <10; b++){for(c =0; c <10; c++){
abc = a *100+ b *10+ c;
bcc = b *100+ c *10+ c;if(abc + bcc ==532)printf("%d %d %d\n", a, b, c);}}}//printf("end");return(0);}
2. 猜数游戏
int main(void){
int num =17;
int guess;printf("input the number:");scanf_s("%d",&guess);if(guess == num)printf("right!the num is %d.", guess);else{printf("no,the num is not %d.", guess);}return(0);}
3. 猜数游戏升级版
int main(void){
int num =17;
int guess, count;;printf("input the number:");scanf_s("%d",&guess);for(count =5; count >=0; count--){if(guess == num){printf("right!the num is %d.\n", guess);break;}else{if(guess > num){printf("it is too big!you have only %d times:",count);scanf_s("%d",&guess);}else{printf("it is too small!you have only %d times:",count);scanf_s("%d",&guess);}//printf("no,the num is not %d.", guess);}if(count ==0){printf("fail!the number is %d.\n",num);}}printf("the game is over.\n");return(0);}
4. 1!+2!+…+100!
int main(void){
int i,j;
double sum =0;
double cheng =1;for(i =1; i <=100; i++){for(j =1; j <= i; j++){
cheng = cheng * j;}
sum = sum + cheng;
cheng =1;}printf("1!+2!+...+100!=%e.\n", sum);return0;}
5. 输入数据比大小
int main(void){
int n,i,get;
int max=0;printf("please input the count of numbers:");scanf_s("%d",&n);for(i =1; i <= n; i++){printf("input the number:");scanf_s("%d",&get);if(max <get)
max =get;}printf("the biggest number is %d.\n", max);return0;}
6. 搬砖穷举问题
//man*3 woman*2 kid*1 sum=45
int main(void){
int man, woman, kid;
int zhuan =45;
int times =0;for(man = zhuan /3; man >=0; man--){for(woman = zhuan /2; woman >=0; woman--){for(kid = zhuan; kid >=0; kid--){
times = man *3+ woman *2+ kid;if(times == zhuan)printf("man=%d,woman=%d,kid=%d.\n",man,woman,kid);}}}printf("end.");return0;}
7. 计算体积
#define PI3.1415926
double tiji(double r, double h);
int main(void){
double r, h,volume;printf("input r h:");scanf_s("%lf %lf",&r,&h);
volume =tiji(r, h);printf("the volume is %lf.\n", volume);return0;}
double tiji(double r, double h){
double volume;
volume =PI* r * r * h;return volume;}
8. 素数表
int sushu(int num);
int main(void){
int n,num,i;
int j =0;printf("input the max num you want:");scanf_s("%d",&n);for(i =2; i <= n; i++){
num=sushu(i);if(num==2){printf(" %d ", i);
j++;}if(j >=10){printf("\n");
j =0;}}return0;}
int sushu(int num){
int i;for(i =2; i <=sqrt(num); i++){if(num % i ==0)return(1);}return(2);}
9. 数据交换(不添加中间变量)
int main(void){
int a =17, b =3;printf("before a=%d b=%d\n", a, b);
a ^= b ^= a ^= b;//交换a b的值printf("after a=%d b=%d\n", a, b);return0;}
10. 数组求平均值,并且输出平均值以上的值
int main(void){
int i;
double avg, sum =0;
int a[5];printf("input 5 number:");for(i =0; i <5; i++){scanf_s("%d",&a[i]);
sum = sum + a[i];}
avg = sum /5;printf("the average number is %lf.\n",avg);printf("avg<:\n");for(i =0; i <5; i++){if(a[i]> avg)printf("%d\n", a[i]);}return0;}
11. 数组,斐波那契数列
int main(void){
int i;
int a[10]={1,1};printf("%d %d ", a[0],a[1]);for(i =2; i <10; i++){
a[i]= a[i -1]+ a[i -2];if(i %5==0)printf("\n");printf("%d ", a[i]);}return0;}
12. 输入数组,查找数据并显示位置
int main(void){
int a[5];
int i,num,flag=0;printf("input 5 numbers:");for(i =0; i <5; i++){scanf_s("%d",&a[i]);}printf("input the standard number:");scanf_s("%d",&num);for(i =0; i <5; i++){if(a[i]== num){printf("the squence of %d is %d.\n", num, i +1);
flag =1;}}if(flag==0)printf("404!!!");return0;}
13. 输入n个数存在数组中,找出最小值,并于第一个值交换
int main(void){
int a[10]={0};
int b[10];
int n, i, min,min_num=0;printf("input n:");scanf_s("%d",&n);printf("input the numbers:");for(i =0; i < n; i++){scanf_s("%d",&a[i]);}
min = a[0];for(i =1; i < n; i++){if(a[0]> a[i]){
a[0]= a[i];
min_num = i;}}
a[min_num]= min;printf("the min is %d number.\n", min_num+1);for(i =0; i < n; i++){printf("%d ", a[i]);}return0;}
14. 选择法排序
int main(void){
int a[10]={0};
int b[10];
int n, i, j,min, min_num =0;printf("input n:");scanf_s("%d",&n);printf("input the numbers:");for(i =0; i < n; i++){scanf_s("%d",&a[i]);}for(i =0; i < n-1; i++){
min_num = i;for(j = i+1; j < n; j++){if(a[j]< a[min_num])
min_num = j;}
min = a[min_num];
a[min_num]= a[i];
a[i]= min;}for(i =0; i < n; i++){printf("%d ", a[i]);}return0;}
15. 输入二维数组,寻找最大值
int main(void){
int a[3][2];
int i,j,min,min_hang=0,min_lie=0;printf("input 3*2 matrix:");for(i =0; i <3; i++){for(j =0; j <2; j++){scanf_s("%d",&a[i][j]);}}for(i =0; i <3; i++){for(j =0; j <2; j++){printf("%d ", a[i][j]);}printf("\n");}for(i =0; i <3; i++){for(j =0; j <2; j++){if(a[i][j]> a[min_hang][min_lie]){
min_hang = i;
min_lie = j;}}}printf("the biggest number is a[%d][%d]=%d.", min_hang+1, min_lie+1, a[min_hang][min_lie]);return0;}
16. 记录字符串中数字的个数
int main(void){
int count=0,i=0;
char s[100];printf("input the char:");while((s[i]=getchar())!='\n'){
i++;}
s[i]='\0';for(i =0; s[i]!='\0'; i++){if(s[i]>='0'&& s[i]<='9')
count++;}printf("count=%d\n", count);return0;}
自测练习设a、b、c均是0到9之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值。设a、b、c均是0到9之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值。#include<stdio.h>int main(void){ int a, b, c; int abc, bcc; for (a = 0; a <10; a++) { for