C Language Foundation

Keywords: C Back-end

  • Han Soldiers

Hanxin has a troop of soldiers, at least M. He wanted to know how many people there are, so he asked the soldiers to queue up to report the number. From 1 to 5, he wrote down the number reported by the last soldier to be 1. Then count from 1 to 6, and write down that the last soldier reported 5; From 1 to 7 the last soldier was reported as 4. Finally, from 1 to 11, the last soldier reported 10. Q. How many soldiers does Hanxin have?

#include <stdio.h>

int main()
{
	int m,i;
	scanf("%d",&m);
	for(i=m;;i++)
	{
		if(i%5 == 1 && i%6 == 5 && i%7 == 4 && i%11 == 10)
		{
			printf("%d",i);
			break;
		}
	}
	
	return 0;
}

        Remaining issues

  • Judging Palindromes

Enter any positive integer from the keyboard to determine if it is palindrome. The so-called palindrome number is the same number read from left to right as from right to left.

#include <stdio.h>

int main()
{
	int n,t,a=0,i,m;
	scanf("%d",&n);

	for(i=1;i<=n;i++)
	{
		a=0;
		m=0;
		scanf("%d",&m);
		t = m;
		while(m>0)
		{
			a = a*10+m%10;
			m = m / 10;	
		}
		if(a==t)
			printf("Yes\n");
		else
			printf("No\n");
	}
	
	return 0;
} 
  • Guess Numbers Game

Guess Number Game is a game in which the player randomly generates a positive integer within 100 and the user inputs a number to guess it, requiring you to write a program that automatically compares it to the random number of guesses and prompts if it is bigger ("Too big") or smaller ("Too small"), equally indicating a guess. If you guess, end the program. The program also requires counting the number of guesses. If the number is guessed once, it prompts "Bingo!"; "Lucky You!" is prompted if the number is guessed within three times; "Good Guess!" is prompted if the number is guessed more than three times but less than N (>3), including the Nth time; If you have not guessed it more than N times, prompt "Game Over" and end the program. If before reaching N times, the user enters a negative number, outputs "Game Over", and ends the program.

#include <stdio.h>

int main()
{
	int guess,number,limit,i;
	scanf("%d %d",&number,&limit);
	i = 1;
	while(i<=limit)
	{
		scanf("%d",&guess);
		if(guess<0)
		{
			printf("Game Over\n");
			break;
		}		
		if(guess>number)
		{
			printf("Too big\n");			
		}
		else if(guess<number)
		{
			printf("Too small\n");			
		}
		else if(guess == number)
		{
			if(i==1){				
				printf("Bingo!\n");
				break;
			}
			else if(i<=3){				
				printf("Lucky You!\n");
				break;
			}
			else{
				printf("Good Guess!\n");
				break;					
			}							
		}			
		if(i==limit)
			printf("Game Over\n");
			i++;	
		}


	return 0;
}
  • Rabbit Propagation

#include <stdio.h>

int main()
{
	int n,i=2,a=1,b=1,c=1;
	scanf("%d",&n);
	if(n==1)
		printf("1\n");
	else
	{
		while(n>c)
		{
			i++;
			c=a+b;
			a=b;
			b=c;

		}
		printf("%d",i);
	}
	
	
	return 0;
}

1 2  3  4   5   6   

1 1   2   3     5     8   Fibonacci series

  •   Number of Black Holes

#include <stdio.h>

int main()
{
	int x,i,a,b,c,z,max,min;
	scanf("%d",&x);
	a = x%10;
	b = x/10%10;
	c = x/100;
	if(a==b&&b==c)
	{
		printf("1: %d - %d = 0\n",x,x);
	}
	else
	{
		i = 1;
		do{
			a = x%10;
			b = x/10%10;
			c = x/100;
			if(a>b)
			{
				z = a;
				a = b;
				b = z;
			} 
			if(a>c)
			{
				z = a;
				a = c;
				c = z;
			}
			if(b>c)
			{
				z = b;
				b = c;
				c = z;
			} 	 
			max=c*100+b*10+a;
			min=a*100+b*10+c;
			printf("%d: %d - %d = %d\n",i,max,min,max-min);
			x = max -min;
			i++;
			
		} while(x!=495);		
	}
	
	return 0;
} 

Use the do while loop to make a size arrangement, then assign the maximum and minimum values, subtract them, and assign x again.

Posted by gordsmash on Fri, 05 Nov 2021 09:24:04 -0700