时间如白驹过隙,大学时光已匆匆过去两年,如今已是一只脚踏入了大三。谨以此篇博文总结一下大一的C语言编程,望对屏幕前努力奋斗的你们有所帮助,加油,追梦人!
#include
int main()
{
printf("Hello World");
return 0;
}
#include
int main()
{
int number;
// printf() 输出字符串
printf("输入一个整数: ");
// scanf() 格式化输入
scanf_s("%d", &number);
// printf() 显示格式化输入
printf("你输入的整数是: %d", number);
return 0;
}
//使用 printf() 与 %d 格式化输出整数
#include
int main()
{
char c; // 声明 char 变量
c = 'A'; // 定义 char 变量
printf("c 的值为 %c", c);
return 0;
}
// 使用 printf() 与 %c 格式化输出一个字符
#include
int main()
{
float f; // 声明浮点数变量
f = 12.001234; // 定义浮点数变量
printf("f 的值为 %f", f);
return 0;
}
// 使用 printf() 与 %f 输出浮点数
// 输出结果:
// f 的值为 12.001234
#include
int main()
{
double d; // 声明双精度变量
d = 12.001234; // 定义双精度变量
printf("d 的值为 %le", d);
return 0;
}
// 使用 printf() 与 %e 输出双精度数
// 输出结果:
// d 的值为 1.200123e+01
#include
int main()
{
printf(" ****\n");
printf(" *\n");
printf(" * \n");
printf(" ****\n");
}
#include
int main()
{
int num1, num2;
scanf_s("%d%d", &num1, &num2);
printf("两数之和=%d\n", num1 + num2);
printf("两数之差=%d\n", num1 - num2);
printf("两数之积=%d\n", num1 * num2);
printf("两数之商=%d\n", num1 / num2);
return 0;
}
//1.使用临时变量
#include
int main()
{
int num1, num2,temp;
scanf_s("%d%d", &num1, &num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("交换后的值:%d %d", num1, num2);
return 0;
}
/*(2)不使用临时变量
不使用临时变量交换两个整数的值*/
#include
int main() {
int a, b;
a = 11;
b = 99;
printf("交换之前 - \n a = %d, b = %d \n\n", a, b);
a = a + b; // ( 11 + 99 = 110) 此时 a 的变量为两数之和,b 未改变
b = a - b; // ( 110 - 99 = 11)
a = a - b; // ( 110 - 11 = 99)
printf("交换后 - \n a = %d, b = %d \n", a, b);
return 0;
}
#include
int main()
{
int x, y, z, t;
printf("请输入三个数字:\n");
scanf_s("%d%d%d", &x, &y, &z);
if (x > y)
{ /*交换x,y的值*/
t = x; x = y; y = t;
}
if (x > z)
{ /*交换x,z的值*/
t = z; z = x; x = t;
}
if (y > z)
{ /*交换z,y的值*/
t = y; y = z; z = t;
}
printf("从小到大排序: %d %d %d\n", x, y, z);
return 0;
}
#include
int main()
{
char c;
printf("输入一个字符: ");
// 读取用户输入
scanf("%c", &c);
// %d 显示整数
// %c 显示对应字符
printf("%c 的 ASCII 为 %d", c, c);
return 0;
}
// 输出结果:
// 输入一个字符: a
// a 的 ASCII 为 97
#include
#define MAX_STRING_LENGTH 65535 // 最大字符串长度
int main()
{
char s[MAX_STRING_LENGTH];
printf("请输入长度小于 %d 的任意字符:", MAX_STRING_LENGTH);
scanf_s("%s", s,MAX_STRING_LENGTH); // 读取字符串。
for (int i = 0; i<s[i]; i++)
{
printf("%c的ASCII:%d\n", s[i], s[i]);
}
return 0;
}
#include
int main()
{
int a, b;
a = 11;
b = 99;
// 也可以通过以下代码实现让用户在终端输入两个数
// printf("输入第一个值:");
// scanf("%d", &a);
// printf("输入第二个值:");
// scanf("%d", &b);
if (a > b)
printf("a 大于 b");
else
printf("a 小于等于 b");
return 0;
}
//输出结果:
//a 小于等于 b
#include
int main()
{
int a, b, c;
a = 11;
b = 22;
c = 33;
if (a > b && a > c)
printf("%d 最大", a);
else if (b > a && b > c)
printf("%d 最大", b);
else if (c > a && c > b)
printf("%d 最大", c);
else
printf("有两个或三个数值相等");
return 0;
}
//输出结果:
//33 最大
//计算 int, float, double 和 char 字节大小
//使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小
//
//sizeof 是 C 语言的一种单目操作符,如C语言的其他操作符++、--等,它并不是函数
//
//sizeof 操作符以字节形式给出了其操作数的存储大小
#include
int main()
{
int integerType;
float floatType;
double doubleType;
char charType;
// sizeof 操作符用于计算变量的字节大小
printf("Size of int: %ld bytes\n", sizeof(integerType));
printf("Size of float: %ld bytes\n", sizeof(floatType));
printf("Size of double: %ld bytes\n", sizeof(doubleType));
printf("Size of char: %ld byte\n", sizeof(charType));
return 0;
}
//输出结果:
//
//Size of int: 4 bytes
//Size of float : 4 bytes
//Size of double : 8 bytes
//Size of char : 1 byte
#include
int main()
{
int number;
printf("请输入一个整数: ");
scanf_s("%d", &number);
// 判断这个数除以 2 的余数
if (number % 2 == 0)
printf("%d 是偶数。", number);
else
printf("%d 是奇数。", number);
return 0;
}
//输出结果:
//
//请输入一个整数 : 5
//5 是奇数。
求一元二次方程:ax2+bx+c=0 的根。
#include
#include
int main()
{
float a, b, c, x1, x2, d;
printf("输入方程的三个系数:");
scanf_s("%f %f %f", &a, &b, &c);
if (a != 0)
{
d = sqrt(b * b - 4 * a * c);
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);
if (x1 < x2)
printf("%0.2f %0.2f\n", x2, x1);
else
printf("%0.2f %0.2f\n", x1, x2);
}
return 0;
}
#include
int main()
{
int year;
printf("输入年份: ");
scanf_s("%d", &year);
if (year % 4 == 0)
{
if (year % 100 == 0)
{
// 这里如果被 400 整数是闰年
if (year % 400 == 0)
printf("%d 是闰年", year);
else
printf("%d 不是闰年", year);
}
else
printf("%d 是闰年", year);
}
else
printf("%d 不是闰年", year);
return 0;
}
#include
int main()
{
char c;
printf("输入一个字符: ");
scanf_s("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf("%c 是字母", c);
else
printf("%c 不是字母", c);
return 0;
}
#include
int main()
{
int n, i, sum = 0;
printf("输入一个正整数: ");
scanf_s("%d", &n);
for (i = 1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d", sum);
return 0;
}
#include
int main() {
//外层循环变量,控制行
int i = 0;
//内层循环变量,控制列
int j = 0;
for (i = 1; i <= 9; i++) {
for (j = 1; j <= i; j++) {
printf("%dx%d=%d\t", j, i, i * j);
}
//每行输出完后换行
printf("\n");
}
}
//(1)方法一:输出指定数量的斐波那契数列
#include
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("输出几项: ");
scanf_s("%d", &n);
printf("斐波那契数列: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
//(2)方法二:输出指定数字前的斐波那契数列
#include
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("输入一个正数: ");
scanf_s("%d", &n);
// 显示前两项
printf("斐波那契数列: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while (nextTerm <= n)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
#include
int main()
{
int n1, n2, i, gcd;
printf("输入两个正整数,以空格分隔: ");
scanf_s("%d %d", &n1, &n2);
for (i = 1; i <= n1 && i <= n2; ++i)
{
// 判断 i 是否为最大公约数
if (n1 % i == 0 && n2 % i == 0)
gcd = i;
}
printf("%d 和 %d 的最大公约数是 %d", n1, n2, gcd);
return 0;
}
#include
int main()
{
int n1, n2, minMultiple;
printf("输入两个正整数: ");
scanf_s("%d %d", &n1, &n2);
// 判断两数较大的值,并赋值给 minMultiple
minMultiple = (n1 > n2) ? n1 : n2;
// 条件为 true
while (1)
{
if (minMultiple % n1 == 0 && minMultiple % n2 == 0)
{
printf("%d 和 %d 的最小公倍数为 %d", n1, n2, minMultiple);
break;
}
++minMultiple;
}
return 0;
}
一个正整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。
#include
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("输入一个整数: ");
scanf_s("%d", &n);
// 如果输入是负数,显示错误
if (n < 0)
printf("Error! 负数没有阶乘jiechen");
else
{
for (i = 1; i <= n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("%d! = %llu", n, factorial);
}
return 0;
}
#include
int main()
{
char c;
for(c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
return 0;
}
#include
int main()
{
long long n;
int count = 0;
printf("输入一个整数: ");
scanf_s("%lld", &n);
while (n != 0)
{
// n = n/10
n /= 10;
++count;
}
printf("数字是 %d 位数。", count);
}
#include
int main()
{
int base, exponent;
long long result = 1;
printf("基数: ");
scanf_s("%d", &base);
printf("指数: ");
scanf_s("%d", &exponent);
while (exponent != 0)
{
result *= base;
--exponent;
}
printf("结果:%lld", result);
return 0;
}
#include
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("输入一个整数: ");
scanf_s("%d", &n);
originalInteger = n;
// 翻转
while (n != 0)
{
remainder = n % 10;
reversedInteger = reversedInteger * 10 + remainder;
n /= 10;
}
// 判断
if (originalInteger == reversedInteger)
printf("%d 是回文数。", originalInteger);
else
printf("%d 不是回文数。", originalInteger);
return 0;
}
质数(prime number)又称素数,有无限个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数,这样的数称为质数
#include
int main()
{
int n, i, flag = 0;
printf("输入一个正整数: ");
scanf_s("%d", &n);
for (i = 2; i <= n / 2; ++i)
{
// 符合该条件不是素数
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d 是素数", n);
else
printf("%d 不是素数", n);
return 0;
}
假如a*b=c(a、b、c都是整数),那么我们称a和b就是c的因数
#include
int main()
{
int number, i;
printf("输入一个整数: ");
scanf_s("%d", &number);
printf("%d 的因数有: ", number);
for (i = 1; i <= number; ++i)
{
if (number % i == 0)
{
printf("%d ", i);
}
}
return 0;
}
#include
int main()
{
int i, space, rows, k = 0;
printf("Enter number of rows: ");
scanf_s("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space)
{
printf(" ");
}
while (k != 2 * i - 1)
{
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
#include
int main()
{
int rows, coef = 1, space, i, j;
printf("行数: ");
scanf("%d",&rows);
for(i=0; i<rows; i++)
{
for(space=1; space <= rows-i; space++)
printf(" ");
for(j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
#include
#include
int convertBinaryToDecimal(long long n);
int main()
{
long long n;
printf("输入一个二进制数: ");
scanf_s("%lld", &n);
printf("二进制数 %lld 转换为十进制为 %d", n, convertBinaryToDecimal(n));
return 0;
}
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n != 0)
{
remainder = n % 10;
n /= 10;
decimalNumber += remainder * pow(2, i);
++i;
}
return decimalNumber;
}
#include
#include
long long convertDecimalToBinary(int n);
int main()
{
int n;
printf("输入一个十进制数: ");
scanf_s("%d", &n);
printf("十进制数 %d 转换为二进制位 %lld", n, convertDecimalToBinary(n));
return 0;
}
long long convertDecimalToBinary(int n)
{
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n != 0)
{
remainder = n % 2;
printf("Step %d: %d/2, 余数 = %d, 商 = %d\n", step++, n, remainder, n / 2);
n /= 2;
binaryNumber += remainder * i;
i *= 10;
}
return binaryNumber;
}
#include
#include
int convertDecimalToOctal(int decimalNumber);
int main()
{
int decimalNumber;
printf("输入一个十进制数: ");
scanf_s("%d", &decimalNumber);
printf("十进制数 %d 转换为八进制为 %d", decimalNumber, convertDecimalToOctal(decimalNumber));
return 0;
}
int convertDecimalToOctal(int decimalNumber)
{
int octalNumber = 0, i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
#include
#include
long long convertOctalToDecimal(int octalNumber);
int main()
{
int octalNumber;
printf("输入一个八进制数: ");
scanf("%d", &octalNumber);
printf("八进制数 %d 转换为十进制为 %lld", octalNumber, convertOctalToDecimal(octalNumber));
return 0;
}
long long convertOctalToDecimal(int octalNumber)
{
int decimalNumber = 0, i = 0;
while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}
i = 1;
return decimalNumber;
}
#include
#include
int convertBinarytoOctal(long long binaryNumber);
int main()
{
long long binaryNumber;
printf("输入一个二进制数: ");
scanf_s("%lld", &binaryNumber);
printf("二进制数 %lld 转换为八进制为 %d", binaryNumber, convertBinarytoOctal(binaryNumber));
return 0;
}
int convertBinarytoOctal(long long binaryNumber)
{
int octalNumber = 0, decimalNumber = 0, i = 0;
while (binaryNumber != 0)
{
decimalNumber += (binaryNumber % 10) * pow(2, i);
++i;
binaryNumber /= 10;
}
i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
#include
#include
long long convertOctalToBinary(int octalNumber);
int main()
{
int octalNumber;
printf("输入一个八进制数: ");
scanf_s("%d", &octalNumber);
printf("八进制数 %d 转二进制为 %lld", octalNumber, convertOctalToBinary(octalNumber));
return 0;
}
long long convertOctalToBinary(int octalNumber)
{
int decimalNumber = 0, i = 0;
long long binaryNumber = 0;
while (octalNumber != 0)
{
decimalNumber += (octalNumber % 10) * pow(8, i);
++i;
octalNumber /= 10;
}
i = 1;
while (decimalNumber != 0)
{
binaryNumber += (decimalNumber % 2) * i;
decimalNumber /= 2;
i *= 10;
}
return binaryNumber;
}
#include
void reverseSentence();
int main()
{
printf("输入一个字符串: ");
reverseSentence();
return 0;
}
void reverseSentence()
{
char c;
scanf_s("%c", &c);
if (c != '\n')
{
reverseSentence();
printf("%c", c);
}
}
#include
int main()
{
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int loop, largest;
largest = array[0];
for (loop = 1; loop < 10; loop++) {
if (largest < array[loop])
largest = array[loop];
}
printf("最大元素为 %d", largest);
return 0;
}
#include
int main() {
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int loop, smallest;
smallest = array[0];
for (loop = 1; loop < 10; loop++) {
if (smallest > array[loop])
smallest = array[loop];
}
printf("最小元素为 %d", smallest);
return 0;
}
#include
int main()
{
char s1[100], s2[100], i, j;
printf("输入第一个字符串: ");
scanf_s("%s", s1,100);
printf("输入第二个字符串: ");
scanf_s("%s", s2,100);
// 计算字符串 s1 长度
for (i = 0; s1[i] != '\0'; ++i);
for (j = 0; s2[j] != '\0'; ++j, ++i)
{
s1[i] = s2[j];
}
s1[i] = '\0';
printf("连接后: %s", s1);
return 0;
}
#include
#include
int main()
{
char s[1000];
int len;
printf("输入字符串: ");
scanf_s("%s", s,1000);
len = strlen(s);
printf("字符串长度: %d", len);
return 0;
}
#include
int main()
{
int hun, ten, ind, n;
printf("result is:");
for( n=100; n<1000; n++ ) /*整数的取值范围*/
{
hun = n / 100;
ten = (n-hun*100) / 10;
ind = n % 10;
if(n == hun*hun*hun + ten*ten*ten + ind*ind*ind) /*各位上的立方和是否与原数n相等*/
printf("%d ", n);
}
printf("\n");
return 0;
}
#include
int main()
{
int day,x1,x2; /*定义 day、x1、x2 3 个变董为基本整型*/
day=9;
x2=1;
while(day>0)
{
x1=(x2+1)*2; /*第一天的桃子数是第二天桃子数加1后的2倍*/
x2=x1;
day--; /*因为从后向前推所以天数递减*/
}
printf("总数有 %d\n个",x1); /* 输出桃子的总数*/
return 0;
}
#include
int main(void) {
//定义公鸡、母鸡、小鸡的数量变量
int x,y,z;
//定义数量变量和价格变量
int sump,sumc;
int i,j,k;
//公鸡最少1只,最多100/5=20只
for(i = 1; i <= 19; i++){
//母鸡最少1只,最多100/3=33只
for(j = 1; j <= 31; j++){
//小鸡最少1只,最多100-公鸡-母鸡
for(k = 1; k <= 100-i-j; k++){
sump = k/3+j*3+5*i;
sumc = i+j+k;
if(sump == 100 && sumc == 100){
printf("公鸡、母鸡、小鸡有:%d %d %d\n",i,j,k);
}
}
}
}
return 0;
}
# include
int main(void)
{
int a[] = { 900, 2, 3, -58, 34, 76, 32, 43, 56, -70, 35, -234, 532, 543, 2500 };
int n; //存放数组a中元素的个数
int i; //比较的轮数
int j; //每轮比较的次数
int buf; //交换数据时用于存放中间数据
n = sizeof(a) / sizeof(a[0]); /*a[0]是int型, 占4字节, 所以总的字节数除以4等于元素的个数*/
for (i = 0; i < n - 1; ++i) //比较n-1轮
{
for (j = 0; j < n - 1 - i; ++j) //每轮比较n-1-i次,
{
if (a[j] < a[j + 1])
{
buf = a[j];
a[j] = a[j + 1];
a[j + 1] = buf;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\x20", a[i]);
}
printf("\n");
return 0;
}