calculation n factorial
int main() { int i=0; int ret=1; scanf("%d",&i); for(i=1;i<=n;i++) {
ret=ret*i; } printf("%d\n",ret); return 0; }
calculation 1!+2!+3!+.....+10!
int main() { calculation 1!+2!+3!+.....+10! int i = 0; int j = 0; int sum = 0; int ret =
1; for (i = 1; i <= 3; i++) { ret = 1; for (j = 1; j <= i; j++) { ret = ret *
j; }sum += ret; } printf("%d\n", sum); return 0; }// Inefficient algorithm // Efficient methods int main() {
int n=0; int sum = 0; int ret = 1;// The initial value cannot be assigned as 0,0 Multiply by any number 0 for(n=1;n<=10;n++) {
ret*=n;// Factorial sum+=ret;// Summation } printf("%d\n",sum); reurn 0; }
Find a specific number in an ordered array n
Half find / Binary search ( The precondition is that the array is ordered )
left mid right
arr[mid] Intermediate element
int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; int
k=10;// lookup k,k The value of can be any number in the array int sz = sizeof(arr) / sizeof(arr[0]);//40/4 int left
= 0; int right = sz - 1;//-1 Because the array subscript from 0 start while (left <= right)// Don't miss it = { int mid
= left + (right-left) / 2;// Intermediate elements must be placed in the loop //int mid = (left + right) / 2; if
(arr[mid] < k) { left = mid + 1; } else if (arr[mid] > k) { right = mid - 1; }
else { printf(" Found subscript is :%d\n", mid); break; } } if (left > right) {
printf(" Can not find \n"); } return 0; }
Writing code , Demonstrate the convergence of multiple characters from both ends to the middle
#include<string.h> #include<windows.h> #include<stdio.h> int main() {
// Subscript from 0 Start and subtract \0 So you have to subtract 2 //sizeof(arr1)/sizeof(arr[0])-2;//4 //char arr[] = "abc";
//[a b c \0] char arr1[] = "welcome to bit!!!!!"; char arr2[] =
"###################"; int left = 0; //int right =
sizeof(arr1)/sizeof(arr[0])-2; int right strlen(arr1)-1; while (left <= right)
{ arr2[left] = arr1[left]; arr2[right] = arr1[right]; printf("%s\n",arr2);
Sleep(1000);// In milliseconds system("cls"); left++; right--; } printf("%s\n",arr2);
return 0; }
Simulate user login scenarios
Enter the password up to three times ,, If it is correct, it will prompt that the input is successful , Exit the program after three input errors
Two character comparison cannot be used ==, Should use strcmp
#include<string.h> #include<stdio.h> int main() { int i = 0; char password[20]
= {0}; // Assume the correct password is “abcdef” for(i = 0; i < 3; i++) { printf(" Please input a password :");
scanf("%s",passsword); if(strcmp(password,"abcdef")==0) { printf(" Correct password \n");
break; } else { printf(" Password error , Reenter \n"); } } if (i == 3) {
printf(" Wrong password for three times , Exit program \n"); } return 0; }
Technology
Daily Recommendation