经典C语言程序100例(31-40)---------------------转自C语言经典论坛

【程序31】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
   判断第二个字母。
1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
2.程序源代码:
#include <stdio.h>
void main()
{
char letter;
printf("please input the first letter of someday/n");
while ((letter=getch())!='Y')/*当所按字母为Y时才结束*/
{ switch (letter)
{case 'S':printf("please input second letter/n");
     if((letter=getch())=='a')
      printf("saturday/n");
     else if ((letter=getch())=='u')
         printf("sunday/n");
       else printf("data error/n");
     break;
case 'F':printf("friday/n");break;
case 'M':printf("monday/n");break;
case 'T':printf("please input second letter/n");
     if((letter=getch())=='u')
      printf("tuesday/n");
     else if ((letter=getch())=='h')
         printf("thursday/n");
       else printf("data error/n");
     break;
case 'W':printf("wednesday/n");break;
default: printf("data error/n");
  }
 }
}
==============================================================
【程序32】
题目:Press any key to change color, do you want to try it. Please hurry up!
1.程序分析:            
2.程序源代码:
#include <conio.h>
void main(void)
{
int color;
for (color = 0; color < 8; color++)
 {
 textbackground(color);/*设置文本的背景颜色*/
 cprintf("This is color %d/r/n", color);
 cprintf("Press any key to continue/r/n");
 getch();/*输入字符看不见*/
 }
}
==============================================================
【程序33】
题目:学习gotoxy()与clrscr()函数   
1.程序分析:
2.程序源代码:
#include <conio.h>
void main(void)
{
clrscr();/*清屏函数*/
textbackground(2);
gotoxy(1, 5);/*定位函数*/
cprintf("Output at row 5 column 1/n");
textbackground(3);
gotoxy(20, 10);
cprintf("Output at row 10 column 20/n");
}
==============================================================
【程序34】
题目:练习函数调用
1. 程序分析:
2.程序源代码:
#include <stdio.h>
void hello_world(void)
{
printf("Hello, world!/n");
}
void three_hellos(void)
{
int counter;
for (counter = 1; counter <= 3; counter++)
hello_world();/*调用此函数*/
}
void main(void)
{
three_hellos();/*调用此函数*/
}
==============================================================
【程序35】
题目:文本颜色设置
1.程序分析:
2.程序源代码:
#include <conio.h>
void main(void)
{
int color;
for (color = 1; color < 16; color++)
 {
 textcolor(color);/*设置文本颜色*/
 cprintf("This is color %d/r/n", color);
 }
textcolor(128 + 15);
cprintf("This is blinking/r/n");
}
==============================================================
【程序36】
题目:求100之内的素数   
1.程序分析:
2.程序源代码:
#include <stdio.h>
#include "math.h"
#define N 101
main()
{
int i,j,line,a[N];
for(i=2;i<N;i++) a[i]=i;
for(i=2;i<sqrt(N);i++)
 for(j=i+1;j<N;j++)
 {
  if(a[i]!=0&&a[j]!=0)
  if(a[j]%a[i]==0)
  a[j]=0;}
printf("/n");
for(i=2,line=0;i<N;i++)
{
 if(a[i]!=0)
 {printf("%5d",a[i]);
 line++;}
 if(line==10)
 {printf("/n");
line=0;}
}
}
==============================================================
【程序37】
题目:对10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,
      下次类推,即用第二个元素与后8个进行比较,并进行交换。       
2.程序源代码:
#define N 10
main()
{int i,j,min,tem,a[N];
/*input data*/
printf("please input ten num:/n");
for(i=0;i<N;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);}
printf("/n");
for(i=0;i<N;i++)
printf("%5d",a[i]);
printf("/n");
/*sort ten num*/
for(i=0;i<N-1;i++)
{min=i;
for(j=i+1;j<N;j++)
if(a[min]>a[j]) min=j;
tem=a[i];
a[i]=a[min];
a[min]=tem;
}
/*output data*/
printf("After sorted /n");
for(i=0;i<N;i++)
printf("%5d",a[i]);
}
==============================================================
【程序38】
题目:求一个3*3矩阵对角线元素之和
1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
2.程序源代码:
main()
{
float a[3][3],sum=0;
int i,j;
printf("please input rectangle element:/n");
for(i=0;i<3;i++)
 for(j=0;j<3;j++)
 scanf("%f",&a[i][j]);
for(i=0;i<3;i++)
 sum=sum+a[i][i];
printf("duijiaoxian he is %6.2f",sum);
}
==============================================================
【程序39】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后
     此元素之后的数,依次后移一个位置。
2.程序源代码:
main()
{
int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number,end,i,j;
printf("original array is:/n");
for(i=0;i<10;i++)
 printf("%5d",a[i]);
printf("/n");
printf("insert a new number:");
scanf("%d",&number);
end=a[9];
if(number>end)
 a[10]=number;
else
 {for(i=0;i<10;i++)
  { if(a[i]>number)
   {temp1=a[i];
    a[i]=number;
   for(j=i+1;j<11;j++)
   {temp2=a[j];
    a[j]=temp1;
    temp1=temp2;
   }
   break;
   }
  }
}
for(i=0;i<11;i++)
 printf("%6d",a[i]);
}
==============================================================
【程序40】
题目:将一个数组逆序输出。
1.程序分析:用第一个与最后一个交换。
2.程序源代码:
#define N 5
main()
{ int a[N]={9,6,5,4,1},i,temp;
 printf("/n original array:/n");
 for(i=0;i<N;i++)
 printf("%4d",a[i]);
 for(i=0;i<N/2;i++)
 {temp=a[i];
  a[i]=a[N-i-1];
  a[N-i-1]=temp;
 }
printf("/n sorted array:/n");
for(i=0;i<N;i++)
 printf("%4d",a[i]);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一篇 基础知识篇 实1 数据类型转换 实2 转义字符 实3 关系和逻辑运算 实4 自增自减 实5 普通位运算 实6 位移运算 实7 字符译码 实8 指针操作符 实9 if判断语句 实10 else-if语句 实11 嵌套if语句 实12 switch语句 实13 for语句 实14 while语句 实15 do-while语句 实16 break和continue语句 // 实17 exit()语句 // 实18 综合实19 一维数组 实20 二维数组 实21 字符数组 // 实22 数组初始化 // 实23 数组应用 实24 函数的值调用 实25 函数的引用调用 //swap 实26 数组函数的调用 // 实27 命令行变元 // 实28 函数的返回值 实29 函数的嵌套调用 实30 函数的递归调用 实31 局部和全局变量 实32 变量的存储类别 实33 内部和外部函数 实34 综合实1 实35 综合实2 实36 变量的指针 实37 一维数组指针 实38 二维数组指针 实39 字符串指针 实40 函数指针 实41 指针数组 实42 二维指针 实43 指针的初始化 实44 综合实 第二篇 深入提高篇 实45 结构体变量 实46 结构体数组 实47 结构体指针变量 实48 结构体指针数组 实49 共用体变量 实50 枚举类型 实51 读写字符 实52 读写字符串 实53 格式化输出函数 实54 格式化输入函数 实55 打开和关闭文件 实56 fputc()和fgetc() 实57 函数rewind() 实58 fread()和fwrite() 实59 fprintf()和fscanf() 实60 随机存取 实61 错误处理 实62 综合实63 动态分配函数 实64 常用时间函数 实65 转换函数 实66 查找函数 实67 跳转函数 实68 排序函数 实69 伪随机数生成 实70 可变数目变元 第三篇 常用算法篇 实71 链表的建立 实72 链表的基本操作 实73 队列的应用 实74 堆栈的应用 实75 串的应用 实76 树的基本操作 实77 冒泡排序法 实78 堆排序 实79 归并排序 实80 磁盘文件排序 实81 顺序查找 实82 二分法查找 实83 树的动态查找 实84 二分法求解方程 实85 牛顿迭代法求解方程 实86 弦截法求解方程 实87 拉格朗日插值 // 实88 最小二乘法拟合 ?? 实89 辛普生数值积分 实90 改进欧拉法 实91 龙格-库塔法 实92 高斯消去法 实93 正定矩阵求逆 第四篇 综合应用篇 实94 用C语言实现遗传算法 实95 人工神经网络的C语言实现 实96 K_均值算法 实97 ISODATA算法 实98 快速傅立叶变换 实99 求解野人与传教士问题 实100 简单专家系统
适用于初学者    经典c程序100例==11--20 【程序11】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月    后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... 2.程序源代码: #include "stdio.h" #include "conio.h" main() { long f1,f2; int i; f1=f2=1; for(i=1;i<=20;i++) { printf("%12ld %12ld",f1,f2); if(i%2==0) printf("\n"); /*控制输出,每行四个*/ f1=f1+f2; /*前两个月加起来赋值给第三个月*/ f2=f1+f2; /*前两个月加起来赋值给第三个月*/ } getch(); } ============================================================== 【程序12】 题目:判断101-200之间有多少个素数,并输出所有素数。 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,       则表明此数不是素数,反之是素数。        2.程序源代码: #include "stdio.h" #include "conio.h" #include "math.h" main() { int m,i,k,h=0,leap=1; printf("\n"); for(m=101;m<=200;m++) { k=sqrt(m+1); for(i=2;i<=k;i++) if(m%i==0) { leap=0; break; } if(leap) { printf("%-4d",m); h++; if(h%10==0) printf("\n"); } leap=1; } printf("\nThe total is %d",h); getch(); } ============================================================== 【程序13】 题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数    本身。如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 2.程序源代码: #include "stdio.h" #include "conio.h" main() { int i,j,k,n; printf("'water flower'number is:"); for(n=100;n<1000;n++) { i=n/100;/*分解出百位*/ j=n/10%10;/*分解出十位*/ k=n%10;/*分解出个位*/ if(i*100+j*10+k==i*i*i+j*j*j+k*k*k) printf("%-5d",n); } getch(); } ============================================================== 【程序14】 题目:将一个正整数分解质因数。如:输入90,打印出90=2*3*3*5。 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 (2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,  重复执行第一步。 (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 2.程序源代码: /* zheng int is divided yinshu*/ #include "stdio.h" #include "conio.h" main() { int n,i; printf("\nplease input a number:\n"); scanf("%d",&n); printf("%d=",n); for(i=2;i<=n;i++) while(n!=i) { if(n%i==0) { printf("%d*",i); n=n/i; } else break; } printf("%d",n); getch(); } ============================================================== 【程序15】 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,    60分以下的用C表示。 1.程序分析:(a>b)?a:b这是条件运算符的基本子。 2.程序源代码: #include "stdio.h" #include "conio.h" main() { int score; char grade; printf("please input a score\n"); scanf("%d",&score); grade=score>=90?'A':(score>=60?'B':'C'); printf("%d belongs to %c",score,grade); getch(); } ============================================================== 【程序16】 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 1.程序分析:利用辗除法。 2.程序源代码: #include "stdio.h" #include "conio.h" main() { int a,b,num1,num2,temp; printf("please input two numbers:\n"); scanf("%d,%d",&num1,&num2); if(num1<num2)/*交换两个数,使大数放在num1上*/ { temp=num1; num1=num2; num2=temp; } a=num1;b=num2; while(b!=0)/*利用辗除法,直到b为0为止*/ { temp=a%b; a=b; b=temp; } printf("gongyueshu:%d\n",a); printf("gongbeishu:%d\n",num1*num2/a); getch(); } ============================================================== 【程序17】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用while语句,条件为输入的字符不为'\n'.        2.程序源代码: #include "stdio.h" #include "conio.h" main() { char c; int letters=0,space=0,digit=0,others=0; printf("please input some characters\n"); while((c=getchar())!='\n') { if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++; else if(c==' ') space++; else if(c>='0'&&c<='9') digit++; else others++; } printf("all in all:char=%d space=%d digit=%d others=%d\n",letters, space,digit,others); getch(); } ============================================================== 【程序18】 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。如2+22+222+2222+22222(此时    共有5个数相加),几个数相加有键盘控制。 1.程序分析:关键是计算出每一项的值。 2.程序源代码: #include "stdio.h" #include "conio.h" main() { int a,n,count=1; long int sn=0,tn=0; printf("please input a and n\n"); scanf("%d,%d",&a,&n); printf("a=%d,n=%d\n",a,n); while(count<=n) { tn=tn+a; sn=sn+tn; a=a*10; ++count; } printf("a+aa+...=%ld\n",sn); getch(); } ============================================================== 【程序19】 题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。如6=1+2+3.编程    找出1000以内的所有完数。 1. 程序分析:请参照程序<--上页程序14. 2.程序源代码: #include "stdio.h" #include "conio.h" main() { static int k[10]; int i,j,n,s; for(j=2;j<1000;j++) { n=-1; s=j; for(i=1;i<j;i++) { if((j%i)==0) { n++; s=s-i; k[n]=i; } } if(s==0) { printf("%d is a wanshu",j); for(i=0;i<n;i++) printf("%d,",k[i]); printf("%d\n",k[n]); } } getch(); } ============================================================== 【程序20】 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在    第10次落地时,共经过多少米?第10次反弹多高? 1.程序分析:见下面注释 2.程序源代码: #include "stdio.h" #include "stdio.h" main() { float sn=100.0,hn=sn/2; int n; for(n=2;n<=10;n++) { sn=sn+2*hn;/*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/ } printf("the total of road is %f\n",sn); printf("the tenth is %f meter\n",hn); getch(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值