简单编写!!!

/*#include <stdio.h>
#include <stdlib.h>
int main(){
//整数除以0结果会导致程序运行报错,但是如果编译过程中
//就能知道除数是0;那么编译器就会在编译过程中报错
//int n = 0;
//printf("%d\n",10/n);
//整数除以整数,结果还是整数,除不尽的部分直接舍弃
//当一个表达式的操作数不是相同的类型时,就会发生隐式类型转换
//当double和int一起计算,此时就会先把int隐式转换为double类型
//int ret = 1 / 2;
//printf("%d\n",ret);
//int ret = 1.0 / 2; //两次隐式转换
//double ret = 1.0 / 2;
//float retret = 1.0 / 2;
//printf("%f\n", ret);

//%
//int ret = 10 % 0;
//int ret = 10.0 % 2;
system("pause");
return 0; 

}*/

/#include <stdio.h>
#include <stdlib.h>
int main(){
//按位与& 两个数据都为1,结果为1,否则结果都为0
//按位或| 两个数据结果都为0,结果为0,否则都为1
//按位异或^ 相同位0,相异为1
//按位取反~ 0变1,1变0
//16进制的数字就是4个2进制位
// 0001
//int x = 0x1;
// 0010
//int y = 0x2;
//%x 打印一个无符号的十六进制的整数
//a=1010 b=1011 c=1100 d=1101 e=1110 f=1111
//printf("%x\n", x&y);
//printf("%x\n", x|y);
//printf("%x\n", x^y);
//printf("%x\n", ~x);
//printf("%x\n", ~y);
//给n的某一个二进制位设为1
// 0000 1010
// 0001 0000 => 1 << 4
// 1110 1111
// 0000 1010
int n = 10;
n = n | (1 << 4);
n = n & ~(1 << 4);
printf("%x\n",n);
system(“pause”);
return 0;
}
/
#include <stdio.h>
#include <stdlib.h>
int main(){
int x = 10;
int y = 20;

//x = x + y;   //  30 20
//y = x - y;   //  30 10
//x = x - y;   //  20 10
// 01010 
// 10100 y
// 11110 x
x = x^y;
y = x^y;
x = x^y;
printf("%d %d\n",x,y);
system("pause");
return 0;

}

//作业1.在屏幕上输出以下图案
/#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int i, j;
scanf("%d", &n);
for (i = 1; i < n; i++)
{
for (j = 1; j <= n - i; j++)
{
putchar(’ ‘);
}
for (j = 1; j <= 2 * i - 1; j++)
{
putchar(’
’);
}
putchar(’\n’);
}
//逆序打印
for (i = n; i >= 1; i–)
{
for (j = 1; j <= n - i; j++)
{
putchar(’ ‘);
}
for (j = 1; j <= 2 * i - 1; j++)
{
putchar(’’);
}
putchar(’\n’);
}
system(“pause”);
return 0;
}
/

//2.求出100~999之间的所有“水仙花数”并输出。
//“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,
//如;153=1+5+3 ? ,则153是一个“水仙花数”。
//法一
/#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int i, j;
int a, b, c, sum;
for (i = 100; i < 999;++i){
//取数字n的个位
a = i % 10;
//取数字n的十位
b = i / 10 % 10;
//取数字n的百位
c = i / 100;
sum = a * a * a + b * b * b + c * c * c;
if (sum == i){
printf("%d\n",i);
}
}
system(“pause”);
return 0;
}
/
//法二
/*#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int i, j;
int count = 0, sum = 0;
char a[10] = { 0 };
for (i = 0; i < 1000000000; i++)
{
//for(i=n;i;i/=10) i % 10将会对n的每一位进行遍历
//for(i=n;i;i/=sn) i % sn将会对n在sn进制下的每一位进行遍历
for (j = i; j; j /= 10)
{
a[count] = j % 10;
count++;
}

	for (j = 0; j < count; j++)
	{
		sum += pow(a[j], count);
	}
	if (sum == i)
	{
		printf("%d\n", i);
	}
 sum = 0;
 count = 0;
}
system("pause");
return 0;

}/
//3.求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,
//例如:2 + 22 + 222 + 2222 + 22222
/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int tmp = 0, sum = 0;
int i;
int n;
scanf("%d", &n);
for (i = 0; i < 5; i++)
{
tmp = tmp * 10 + n;
sum += tmp;
}
printf("%d\n", sum);
system(“pause”);
return 0;
}*/

//1.完成猜数字游戏
/*#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Menu(){
printf("\n");
printf(“1.开始游戏\n”);
printf(“0.结束游戏\n”);
printf("
\n");
printf(“请输入您的选项: \n”);
int choice = 0;
scanf("%d", &choice);
return choice;
}

void Game(){
//1.生成一个随机数(1-100)
int to_guess = rand() % 100 + 1;
//2.提示用户输入一个数字
while (1){
printf(“请输入一个整数: “);
int num = 0;
scanf(”%d”,&num);
//3.拿用户准备的数字和随机数进行比较
if (num < to_guess){
printf(“低了!\n”);
}
else if (num>to_guess){
printf(“高了!\n”);
}
else{
printf(“恭喜您猜对了!\n”);
return;
}
}
}
int main(){
//计算机随机生成一个数字(1-100)
//由用户来猜这个数字,如果用户猜对了,提示回答正确
//如果用户猜对了,提示用户"高了/低了"让用户重新输入
//时间戳
//time返回值类型是time_t 有符号的64位整数
//srand 参数 unsigned int 32位的无符号整数
srand((unsigned int)time(0));
while (1){
int choice = Menu();
if (choice==1){
Game();
}
else if (choice == 0){
printf(“goodbye!\n”);
break;
}
else{
printf(“您的输入有误!\n”);
}
}
system(“pause”);
return 0;
}/
//2.写代码可以在整型有序数组中查找想要的数字
//找到了返回下标,找不到返回 - 1.(折半查找)
/
#include <stdio.h>
#include <stdlib.h>
int searchArray(int arr[], int n, int f)
{
int left = 0;
int right = n - 1;
int mid;
while (left <= right)
{
mid = (left + right) / 2;
if (arr[mid] < f)
{
left = mid + 1;
}
else if (arr[mid] > f)
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
}

int main()
{
int a[10] = { 1, 3, 4, 5, 7, 9, 11, 13, 15, 16 };
printf("%d\n", searchArray(a, 10, 13));

system("pause");
return 0;

}/
//3.编写代码模拟三次密码输入的场景
//最多能输入三次密码,密码正确,提示“登录成功”, 密码错误,
//可以重新输入,最多输入三次。三次均错,则提示退出程序。
/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
strcpy(str1 = str2);
//
strcat(str1 += str2);
//
strcmp(str1 > str2);
//
strcasecmp(str1 > str2);
strlen

扫描二维码关注公众号,回复: 5861772 查看本文章

int login(char pwd[])
{
//输入一个长度为
char input[65] = { 0 };
int i;

for (i = 0; i < 3; i++)
{
	scanf("%s", input);
	if (!strcmp(input, pwd))
	{
		return 1;
	}
}
return 0;

}

int main()
{
char pwd[] = “biterocket”;
char input[65] = { 0 };
int i;

if (login(pwd))
{
	printf("登陆成功\n");
}
else
{
	printf("登录失败\n");
}

system("pause");
return 0;

}/
//4.编写一个程序,可以一直接收键盘字符,
//如果是小写字符就输出对应的大写字符,
//如果接收的是大写字符,就输出对应的小写字符,
//如果是数字不输出,如果遇到$就结束
/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#define isSmallLetter(ch) ((ch) <= ‘z’ && (ch) >= ‘a’)
int isBigLetter(char ch){
return ch <= ‘Z’ && ch >= ‘A’;
}

int main(){
char ch;
while (1){
ch = getchar();
if (isSmallLetter(ch))
{
//putchar(ch - (‘a’ - ‘A’));
putchar(ch & ~(1 << 5));
}
else if (isBigLetter(ch))
{
putchar(ch | 1 << 5);
}
else if (isalnum(ch)){
//do nothing
}
else if (ch == ‘$’){
break;
}
else{
putchar(ch);
}
}
system(“pause”);
return 0;
}*/

//1.实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,
//输入9,输出9 * 9口诀表,输入12,输出12 * 12的乘法口诀表。
/#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <windows.h>
#include <string.h>
int fun(a){
int i, j;
for (i = 1; i <= a; ++i){
for (j = 1; j <= i; ++j){
printf("%2d
%2d=%2d",i,j,ij);
}
printf("\n");
}
return(a);
}
int main(){
int a;
printf(“请输入一个数: \n”);
scanf("%d",&a);
printf("",fun(a));
system(“pause”);
return 0;
}
/

//2.使用函数实现两个数的交换
/*#include <stdio.h>
void swapArgs(int * pi, int * pj){
int tmp;
tmp = *pi;
*pi = *pj;
pj = tmp;
}
int main(){
int i = 10, j = 20;
swapArgs(&i, &j);
printf(“i = %d, j = %d\n”, i, j);
system(“pause”);
return 0;
}
/

//3.实现一个函数判断year是不是润年
/#include <stdio.h>
#include <stdlib.h>
int IsLeapYear(int year){
if (year % 100 == 0){
//判定是不是世纪闰年
if (year % 400 == 0){
return 1;
}
else{
return 0;
}
}
else {
//判断是不是普通闰年
if (year % 4 == 0){
return 1;
}
else{
return 0;
}
}
}
int main(){
printf("%d\n",IsLeapYear(2000));
system(“pause”);
return 0;
}
/

//4.创建一个数组
//实现函数init()初始化数组、实现empty()清空数组、
//实现reverse()函数完成数组元素的逆置。
//要求:自己设计函数的参数,返回值
/*#include <stdio.h>
void init(int in[], int data[], int n){
int i;
for (i = 0; i < n; i++){
in[i] = data[i];
}
}

void empty(int data[], int n){
int i;
for (i = 0; i < n; i++){
data[i] = 0;
}
}

void reverse(int data[], int n){
int tmp;
int i, j;
for (i = 0, j = n - 1; i < j; i++, j–){
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
}

void printArray(int data[], int n){
int i;
for (i = 0; i < n; i++){
printf("%d ", data[i]);
}
putchar(’\n’);
}

int main(){
int a[10] = { 0 };
int b[6] = { 1, 2, 3, 4, 5, 6 };
init(a, b, 6);
printArray(a, 10);
reverse(a, 10);
printArray(a, 10);
empty(a, 10);
printArray(a, 10);
system(“pause”);
return 0;
}*/

//5.实现一个函数,判断一个数是不是素数。
#include <stdio.h>
#include <stdlib.h>
//如果是素数,返回1,否则返回0
int IsPrime(int x){
if (x <= 0){
return 0;
}
for (int i = 2; i < x; ++i){
if (x%i == 0){
return 0;
}
}
return 1;
}
int main(){
printf("%d",IsPrime(111));
system(“pause”);
return 0;
}
/*#include <stdio.h>
#include <stdlib.h>
int main(){
//整数除以0结果会导致程序运行报错,但是如果编译过程中
//就能知道除数是0;那么编译器就会在编译过程中报错
//int n = 0;
//printf("%d\n",10/n);
//整数除以整数,结果还是整数,除不尽的部分直接舍弃
//当一个表达式的操作数不是相同的类型时,就会发生隐式类型转换
//当double和int一起计算,此时就会先把int隐式转换为double类型
//int ret = 1 / 2;
//printf("%d\n",ret);
//int ret = 1.0 / 2; //两次隐式转换
//double ret = 1.0 / 2;
//float retret = 1.0 / 2;
//printf("%f\n", ret);

//%
//int ret = 10 % 0;
//int ret = 10.0 % 2;
system(“pause”);
return 0;
}*/

/#include <stdio.h>
#include <stdlib.h>
int main(){
//按位与& 两个数据都为1,结果为1,否则结果都为0
//按位或| 两个数据结果都为0,结果为0,否则都为1
//按位异或^ 相同位0,相异为1
//按位取反~ 0变1,1变0
//16进制的数字就是4个2进制位
// 0001
//int x = 0x1;
// 0010
//int y = 0x2;
//%x 打印一个无符号的十六进制的整数
//a=1010 b=1011 c=1100 d=1101 e=1110 f=1111
//printf("%x\n", x&y);
//printf("%x\n", x|y);
//printf("%x\n", x^y);
//printf("%x\n", ~x);
//printf("%x\n", ~y);
//给n的某一个二进制位设为1
// 0000 1010
// 0001 0000 => 1 << 4
// 1110 1111
// 0000 1010
int n = 10;
n = n | (1 << 4);
n = n & ~(1 << 4);
printf("%x\n",n);
system(“pause”);
return 0;
}
/
/*#include <stdio.h>
#include <stdlib.h>
int main(){
int x = 10;
int y = 20;

//x = x + y;   //  30 20
//y = x - y;   //  30 10
//x = x - y;   //  20 10
// 01010 
// 10100 y
// 11110 x
x = x^y;
y = x^y;
x = x^y;
printf("%d %d\n", x, y);
system("pause");
return 0;

}/
/
#include <stdio.h>
#include <stdlib.h>
//打印1的个数
int CountBit(int num){
int count = 0;
for (int i = 0; i < 32; ++i){
if (num&(1 << i)){
++count;
}
}
return count;
}*/
/int main(){
// 0000 1010
int x = -10;
// f6 ff ff ff
// 1000 0000 0000 0000 0000 0000 0000 1010原码
// 1111 1111 1111 1111 1111 1111 1111 0101 反码
// 1111 1111 1111 1111 1111 1111 1111 0110 补码(反码+1)
// F F F F F F F 6
//字节序
printf("%d\n",CountBit(x));
system(“pause”);
return 0;
}
/
//常量可以初始化 但是不可以赋值
#define and &&
#define or ||
#define 整数 int
#include <stdio.h>
#include <stdlib.h>
int main(){
//常量可以初始化 但是不可以赋值
//const int x = 10;
//x = 20;
//int arr[4] = {1,2,3,4}
//arr = {5,6,7,8}
//int x = 0;
//x += 1;
//x = x + 1;
//int x = 1;
//printf("%d\n", ~x);
//printf("%d\n", !x);
//int arr[10] = { 0 };
// sizeof是编译期求值 编译生成exe文件
//printf("%d\n",sizeof(arr[100]));
//printf("%d\n", sizeof(int));
// 前置++等价代码
//num += 1;
//return num;
//后置++等价代码
//int tmp = num;
//num += 1;
//return tmp;

//unsigned int x = 0x123;
//unsigned char y = 0;
//x = (int)y;
//y = x;
//整数之间的转换
//如果是长度低的变量往长度高的变量转,此时高位补符号位
//如果是长度高的变量往长度低的变量转,此时发生截断,只保留低位
//整数和浮点数内存表示数据的方式是截然不同的
//此时就不是一个简单的截断和补位
//最终的目的是让转换的结果尽可能的正确
// 0001 
// 0010 
// 0011
整数 x = 0x1;
int y = 0x2;
printf("%d\n",x and y);
printf("%d\n", x or y);
printf("%d\n", x & y);
printf("%d\n", x | y);
system("pause");
return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_44770155/article/details/88986719
今日推荐