当前位置:网站首页>C语言必背代码大全
C语言必背代码大全
2022-07-06 09:25:00 【编程小鱼六六六】
对于刚学计算机编程的同学来说,每一个编程知识都觉得很重要。下面小编为大家整理了c语言必背代码,希望大家喜欢。

1、/*输出9*9口诀。共9行9列,i控制行,j控制列。*/
#include "stdio.h"
main()
{int i,j,result;
for (i=1;i<10;i++)
{ for(j=1;j<10;j++)
{
result=i*j;
printf("%d*%d=%-3d",i,j,result);/*-3d表示左对齐,占3位*/
}
printf("\n");/*每一行后换行*/
}
}
2、/*古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
兔子的规律为数列1,1,2,3,5,8,13,21....*/
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; /*前两个月加起来赋值给第三个月*/
}
}
3、/*判断101-200之间有多少个素数,并输出所有素数及素数的个数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。*/
#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) /*内循环结束后,leap依然为1,则m是素数*/
{printf("%-4d",m);h++;
if(h%10==0)
printf("\n");
}
leap=1;
}
printf("\nThe total is %d",h);
}
4、/*一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程
找出1000以内的所有完数。*/
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]);
}
}
}
5、/*下面程序的功能是将一个4×4的数组进行逆时针旋转90度后输出,要求原始数组的数据随机输入,新数组以4行4列的方式输出,
请在空白处完善程序。*/
main()
{ int a[4][4],b[4][4],i,j; /*a存放原始数组数据,b存放旋转后数组数据*/
printf("input 16 numbers: ");
/*输入一组数据存放到数组a中,然后旋转存放到b数组中*/
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{ scanf("%d",&a[i][j]);
b[3-j][i]=a[i][j];
}
printf("array b:\n");
for(i=0;i<4;i++)
{ for(j=0;j<4;j++)
printf("%6d",b[i][j]);
printf("\n");
}
}
6、/*编程打印直角杨辉三角形*/
main()
{int i,j,a[6][6];
for(i=0;i<=5;i++)
{a[i][i]=1;a[i][0]=1;}
for(i=2;i<=5;i++)
for(j=1;j<=i-1;j++)
a[i][j]=a[i-1][j]+a[i-1][j-1];
for(i=0;i<=5;i++)
{for(j=0;j<=i;j++)
printf("%4d",a[i][j]);
printf("\n");}
}
7、/*通过键盘输入3名学生4门课程的成绩,
分别求每个学生的平均成绩和每门课程的平均成绩。
要求所有成绩均放入一个4行5列的数组中,输入时同一人数据间用空格,不同人用回车
其中最后一列和最后一行分别放每个学生的平均成绩、每门课程的平均成绩及班级总平均分。*/
#include <stdio.h>
#include <stdlib.h>
main()
{ float a[4][5],sum1,sum2;
int i,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
scanf("%f",&a[i][j]);
for(i=0;i<3;i++)
{ sum1=0;
for(j=0;j<4;j++)
sum1+=a[i][j];
a[i][4]=sum1/4;
}
for(j=0;j<5;j++)
{ sum2=0;
for(i=0;i<3;i++)
sum2+=a[i][j];
a[3][j]=sum2/3;
}
for(i=0;i<4;i++)
{ for(j=0;j<5;j++)
printf("%6.2f",a[i][j]);
printf("\n");
}
}
8、/*完善程序,实现将输入的字符串反序输出,
如输入windows 输出swodniw。*/
#include <string.h>
main()
{ char c[200],c1;
int i,j,k;
printf("Enter a string: ");
scanf("%s",c);
k=strlen(c);
for (i=0,j=k-1;i<k/2;i++,j--)
{ c1=c[i];c[i]=c[j];c[j]=c1; }
printf("%s\n",c);
}
指针法:
void invert(char *s)
{int i,j,k;
char t;
k=strlen(s);
for(i=0,j=k-1;i<k/2;i++,j--)
{ t=*(s+i); *(s+i)=*(s+j); *(s+j)=t; }
}
main()
{ FILE *fp;
char str[200],*p,i,j;
if((fp=fopen("p9_2.out","w"))==NULL)
{ printf("cannot open the file\n");
exit(0);
}
printf("input str:\n");
gets(str);
printf("\n%s",str);
fprintf(fp,"%s",str);
invert(str);
printf("\n%s",str);
fprintf(fp,"\n%s",str);
fclose(fp);
}
9、/*下面程序的功能是从字符数组s中删除存放在c中的字符。*/
#include <stdio.h>
main()
{ char s[80],c;
int j,k;
printf("\nEnter a string: ");
gets(s);
printf("\nEnter a character: ");
c=getchar( );
for(j=k=0;s[j]!= '\0';j++)
if(s[j]!=c)
s[k++]=s[j];
s[k]= '\0';
printf("\n%s",s);
}
10、/*编写一个void sort(int *x,int n)实现将x数组中的n个数据从大到小
排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out中*/
#include<stdio.h>
void sort(int *x,int n)
{
int i,j,k,t;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(x[j]>x[k]) k=j;
if(k!=i)
{
t=x[i];
x[i]=x[k];
x[k]=t;
}
}
}
void main()
{FILE *fp;
int *p,i,a[10];
fp=fopen("p9_1.out","w");
p=a;
printf("Input 10 numbers:");
for(i=0;i<10;i++)
scanf("%d",p++);
p=a;
sort(p,10);
for(;p<a+10;p++)
{ printf("%d ",*p);
fprintf(fp,"%d ",*p); }
system("pause");
fclose(fp);
}
1、scanf(“a=%d,b=%d”,&a,&b) 考试超级重点!一定要记住是以第一部分的格式在终端输入数据。考试核心为:一模一样。在黑色屏幕上面输入的为 a=12,b=34才可以把12和34正确给a和b 。有一点不同也不行。
2、scanf(“%d,%d”,x,y);这种写法绝对错误,scanf的第二个部分一定要是地址!scanf(“%d,%d”,&x,&y);注意写成这样才可以!
3、特别注意指针在scanf的考察例如: int x=2;int *p=&x;scanf(“%d”,x); 错误 scanf(“%d”,p);正确scanf(“%d”,&p); 错误 scanf(“%d”,*p)错误。
4、指定输入的长度 (考试重点)终端输入:1234567scanf(“-M%d”,&x,&y,&z);x为12,y为3456,z为7终端输入:1 234567 由于1和2中间有空格,所以只有1位给xscanf(“-M%d”,&x,&y,&z);x为1,y为2345,z为67。
5、字符和整型是近亲:int x=97;printf(“%d”,x); 结果为97printf(“%c”,x); 结果为 a。
边栏推荐
- 学习记录:使用STM32F1看门狗
- UCORE lab5 user process management experiment report
- Crawler series (9): item+pipeline data storage
- 学习记录:理解 SysTick系统定时器,编写延时函数
- Learning record: use stm32f1 watchdog
- China medical check valve market trend report, technical dynamic innovation and market forecast
- Es6--- two methods of capturing promise status as failed
- Threads et pools de threads
- Cost accounting [20]
- How to write the bug report of software test?
猜你喜欢

学习记录:使用STM32外部输入中断
![[pytorch] simple use of interpolate](/img/16/87aa8a49e60801404822fe644e70c8.jpg)
[pytorch] simple use of interpolate

线程及线程池
Knowledge that you need to know when changing to software testing

Threads and thread pools

What are the commonly used SQL statements in software testing?

学习记录:使用STM32F1看门狗

Example 071 simulates a vending machine, designs a program of the vending machine, runs the program, prompts the user, enters the options to be selected, and prompts the selected content after the use

UCORE LaB6 scheduler experiment report

学习记录:STM32F103 时钟系统概述工作原理
随机推荐
Threads et pools de threads
JS --- JS function and scope (II)
STM32學習記錄:輸入捕獲應用
UCORE Lab 1 system software startup process
Preface to the foundations of Hilbert geometry
How to become a good software tester? A secret that most people don't know
Indonesian medical sensor Industry Research Report - market status analysis and development prospect forecast
学习记录:串口通信和遇到的错误解决方法
Learning record: Tim - capacitive key detection
Flex --- detailed explanation of flex layout attributes
Do you know the advantages and disadvantages of several open source automated testing frameworks?
LeetCode#198. raid homes and plunder houses
学习记录:理解 SysTick系统定时器,编写延时函数
ucorelab3
LeetCode#62. Different paths
Hospital privacy screen Industry Research Report - market status analysis and development prospect forecast
Visual analysis of data related to crawling cat's eye essays "sadness flows upstream into a river" | the most moving film of Guo Jingming's five years
How to build a nail robot that can automatically reply
动态规划前路径问题优化方式
Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast