来一发近100个C言语基本算法案例(低级篇)
使用gcc或g++进行编译,使用gdb进行调试;上面这些C言语基本算法案例都是经由测试和考证过了的,接待列位利用。
本文是该系列的第一篇,都是一些绝对低级的算法,很合适刚入手下手学C言语的同砚。
1、C言语打印一条语句
源代码:
/*CProgramtoprintasentence.*/#include<stdio.h>intmain(){printf("CProgramming");/*printf()printsthecontentinsidequotation*/return0;} 输入:
CProgramming 2、C言语打印用户输出的一个整数
源代码:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;} 输入:
Enterainteger:25Youentered:25 3、C言语完成两个整数相加
源代码:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;} 输入:
Entertwointegers:1211Sum:23 4、C言语完成两个小数相乘
源代码:
/*Cprogramtomultiplyanddisplaytheproductoftwofloatingpointnumbersenteredbyuser.*/#include<stdio.h>intmain(){floatnum1,num2,product;printf("Entertwonumbers:");scanf("%f%f",&num1,&num2);/*Storesthetwofloatingpointnumbersenteredbyuserinvariablenum1andnum2respectively*/product=num1*num2;/*Performsmultiplicationandstoresit*/printf("Product:%f",product);return0;} 输入:
Entertwonumbers:2.41.1Product:2.640000 5、C言语查找字符的ASCII值
源代码:
/*SourcecodetofindASCIIvalueofacharacterenteredbyuser*/#include<stdio.h>intmain(){charc;printf("Enteracharacter:");scanf("%c",&c);/*Takesacharacterfromuser*/printf("ASCIIvalueof%c=%d",c,c);return0;} 输入:
Enteracharacter:GASCIIvalueofG=71 6、C言语依据用户输出的整数做商和余数
源代码:
CProgramming0 输入:
CProgramming1 7、C言语猎取整型、单精度浮点型、双精度浮点型和字符型的长度
基础语法是:
CProgramming2 源代码:
CProgramming3 输入:
CProgramming4注:大概会因为体系的分歧出来的了局也不尽不异。
8、C言语猎取关头字long的长度局限
源代码:
CProgramming5 输入:
CProgramming6 9、C言语互换数值
源代码:
CProgramming7 输入:
CProgramming8 10、C言语反省数值是奇数仍是偶数
源代码:
CProgramming9 输入1:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}0 输入2:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}1 也能够用前提运算符办理:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}2 11、C言语反省是元音仍是子音
源代码:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}3 输入1:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}4 输入2:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}5 也能够用前提运算符办理
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}6 输入了局和下面的程序不异。
12、C言语完成从三个数值中查找最年夜值
完成1:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}7 完成2:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}8 完成3:
#include<stdio.h>intmain(){intnum;printf("Enterainteger:");scanf("%d",&num);/*Storingaintegerenteredbyuserinvariablenum*/printf("Youentered:%d",num);return0;}9 输入了局不异:
Enterainteger:25Youentered:250 13、C言语解一元二次方程
源代码:
Enterainteger:25Youentered:251 输入1:
Enterainteger:25Youentered:252 输入2:
Enterainteger:25Youentered:253 14、C言语反省是不是是闰年
源代码:
Enterainteger:25Youentered:254 输入1:
Enterainteger:25Youentered:255 输入2:
Enterainteger:25Youentered:256 输入3:
Enterainteger:25Youentered:257 15、C言语反省一个数是负数、正数仍是零
源代码:
Enterainteger:25Youentered:258 也能够利用if-else语句
Enterainteger:25Youentered:259 输入1:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}0 输入2:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}1 输入3:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}2 16、C言语反省某字符串是否是字母
源代码:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}3 输入1:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}4 输入2:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}5 17、C言语盘算天然数的和
源代码:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}6 也能够利用for轮回语句
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}7 输入:
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}8 也能够经由过程if-else语句
/*Cprogrammingsourcecodetoaddanddisplaythesumoftwointegersenteredbyuser*/#include<stdio.h>intmain(){intnum1,num2,sum;printf("Entertwointegers:");scanf("%d%d",&num1,&num2);/*Storesthetwointegerenteredbyuserinvariablenum1andnum2*/sum=num1+num2;/*Performsadditionandstoresitinvariablesum*/printf("Sum:%d",sum);/*Displayssum*/return0;}9 18、C言语盘算阶乘
关于恣意负数n,阶乘指的是:
Entertwointegers:1211Sum:230 假如数值是正数,那末阶乘就不存在。而且我们划定,0的阶乘就是1。
源代码:
Entertwointegers:1211Sum:231 输入1:
Entertwointegers:1211Sum:232 输入2:
Entertwointegers:1211Sum:233 先先容到这里,下一篇将分享更多的C言语基本算法,敬请等候。
英文原文:CProgrammingExamplesvia:codeceo
但不会命令而成为高手也是不可能的.这就好比学英语,什么语法都不懂,只捧着单词手册背单词是学不会英语的,但是没有单词词汇量英语水平也提不高的。 之前一直都没有怎么这两个库,特别是vector,一直被难缠的数组死死拉着,而vector正在逐步取代数组的地位,这一点我也深深体会到,在我们项目的封装模块中 最后一部分:算是高级一些的主题,总感觉书的这一部分翻译的不太好,读起来完全没有前面几章顺畅,像多重继承什么的,如果觉得太难理解,看的时候放一放,改天等你用到的时候,再来看看是怎么回事。 虽然还不明确软件技术包含的具体内容,但从C++语言这门课程开始,已发现程序设计的乐趣,在学习C++语言的过程中也学到了许多计算机应用基础知识,对计算机的机体也有了一个大体的了解。 当函数模板遇到重载遇到模板特化无疑会让你傻傻分不清楚,做好笔记,慢慢理解,其实不难。 通过实际操作,学会 C++语言程序编程的基本步骤、基本方法,开发了自己的逻辑思维能力,培养了分析问题、解决问题的能力。深刻体会到“没有做不到的, 在编程序时,我也学会了敢于尝试,“不试不知道,试试就能行”这是我在实训中领悟的道理之一。 用的vector,而很少使用了数组。当然在我现在的能力范围内,还很难驾驭vector,毕竟数组当年也是我的最爱,相比于更加难缠的指针控制来说。
页:
[1]