Are some basic exercises in the Blue Bridge Cup , Let's work together ~

1. Sequence summation

seek 1+2+3+...+n Value of .
#include<stdio.h> int main() { long long n,sum=0; scanf("%I64d",&n);
sum=(1+n)*n/2; printf("%I64d",sum); return 0; }
2. Area of circle

/* Radius of a given circle r, Find the area of a circle .*/
#include<stdio.h> #define PI 3.14159265358979323 int main() { int n;
scanf("%d",&n); printf("%.7f",PI*n*n); return 0; }
3. Fibonacci number

/*Fibonacci The recurrence formula of the sequence is :Fn=Fn-1+Fn-2, among F1=F2=1.

When n Relatively large ,Fn It's also very big , Now we want to know ,Fn divide 10007 What is the remainder of .*/
#include<stdio.h> int main() { long a,b,fn,n,i; scanf("%d",&n); a=1;b=1;
for(i=0;i<n-2;i++) { fn=(a+b)%10007; a=b;b=fn; } printf("%d",fn); return 0; }
4.01 strand

/* For length 5 One of the bits 01 strand , Everyone could be 0 or 1, Altogether 32 Kinds of possibility . The first few of them are :

00000

00001

00010

00011

00100

Please output this in descending order 32 species 01 strand .*/
#include<stdio.h> int main() { int i,j,a[5],v,k=0; for(i=0;i<32;i++) { v=i;
for(j=0;j<5;j++) { a[j]=v%2; v=v/2; } for(j=4;j>=0;j--)printf("%d",a[j]);
printf("\n"); } return 0; }
5. Alphabetic graphics

/* Using letters can form some beautiful graphics , An example is given below :

ABCDEFG

BABCDEF

CBABCDE

DCBABCD

EDCBABC

This is a 5 that 's ok 7 Column graph , Please find out the law of this figure , And output a n that 's ok m Column graph .*/
#include<stdio.h> int main() { int i,j,n,m; char a[100][100]; scanf("%d
%d",&n,&m); char
b[27]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(i==0) a[0][j]=b[j]; else
{a[i][j]=a[i-1][j-1];a[i][0]=b[i];} printf("%c",a[i][j]); } printf("\n"); }
return 0; }
6. Sequence characteristics

/* give n number , Find this n Maximum number of , minimum value , and .*/
#include<stdio.h> int main() { int n,i,a[10000],min,max,h=0; scanf("%d",&n);
for (i=0;i<n;i++) scanf("%d",&a[i]); max=min=a[0]; for(i=0;i<n;i++) {
if(min>a[i])min=a[i]; else if(max<a[i])max=a[i]; h+=a[i]; } printf("%d\n",max);
printf("%d\n",min); printf("%d\n",h); return 0; }
7. Find integer

/* Give a containing n A sequence of integers , Ask integer a What is the first occurrence in the sequence .*/
#include<stdio.h> int main() { int n,a[1000],i,m,k=-1; scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(i=1;i<=n;i++) {
if(m==a[i]) {k=i; break;} } printf("%d",k); return 0; }
8. Yanghui triangle

/* Yang Hui triangle, also known as Pascal triangle , Its second i+1 Yes (a+b)i Coefficient of the expansion of . 

One of its important properties is : Each number in a triangle is equal to the sum of the numbers on its two shoulders .

The front of Yang Hui triangle is given below 4 that 's ok :

1  

1 1  

1 2 1  

1 3 3 1

give n, Before outputting it n that 's ok .*/
#include<stdio.h> int main() { int n,i,a[100][1000],j; a[0][0]=1;
scanf("%d",&n); printf("%d \n",a[0][0]); for(i=1;i<n;i++) { for(j=0;j<i+1;j++)
{ a[i][j]=a[i-1][j-1]+a[i-1][j]; printf("%d ",a[i][j]); } printf("\n"); }
return 0; }
9. Special number

/*153 Is a very special number , It is equal to the cubic sum of each of its numbers , Namely 153=1*1*1+5*5*5+3*3*3.

Program to find all three decimal numbers that meet this condition .*/
#include<stdio.h> int main() { int a,b,c,i,j,k; int sum,t[1000];
for(i=1;i<10;i++) { a=i; for(j=0;j<10;j++) { b=j; for(k=0;k<10;k++) { c=k;
sum=a*100+b*10+c; if(sum==a*a*a+b*b*b+c*c*c) printf("%d\n",sum); } } } return
0; }
10. Palindrome number

/*1221 Is a very special number , It reads from the left as it reads from the right , Programming for all such four digit decimal numbers .*/
#include<stdio.h> int main() { int a,b,c,d,i,j,k,l; for(i=1;i<10;i++) { a=i;
for(j=0;j<10;j++) { b=j; for(k=0;k<10;k++) { c=k; for(l=0;l<10;l++) { d=l;
if(a==d&&b==c)printf("%d%d%d%d\n",a,b,c,d); } } } } return 0; }
10. Special palindrome

/*123321 Is a very special number , It reads from the left as it reads from the right .

Enter a positive integer n, Program to find all such five and six digit decimal numbers , Satisfy that the sum of your numbers is equal to n .*/
#include<stdio.h> int main() { int a,b,c,d,e,f,i,j,k,l,p,q,n,sum,sun;
scanf("%d",&n); for(i=0;i<10;i++) { a=i; for(j=0;j<10;j++) { b=j;
for(k=0;k<10;k++) { c=k; for(l=0;l<10;l++) { d=l; for(p=0;p<10;p++) { e=p;
for(q=0;q<10;q++) { f=q; sum=a+b+c+d+e+f; sun=b+c+d+e+f;
if(a==f&&a!=0&&b==e&&c==d&&sum==n)printf("%d%d%d%d%d%d\n",a,b,c,d,e,f); else
if(a==0&&b==f&&c==e&&sun==n)printf("%d%d%d%d%d\n",b,c,d,e,f); } } } } } }
return 0; }
11. Decimal to hexadecimal

// Hexadecimal number is an integer representation that is often used in programming . It has 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F common 16 Symbols , Each represents a decimal number 0 to 15. The hexadecimal count method is full 16 enter 1, So decimal numbers 16 Yes in hexadecimal 10, And decimal 17 Yes in hexadecimal 11, and so on , Decimal 30 Yes in hexadecimal 1E.

Give a nonnegative integer , Express it in hexadecimal form .
#include<stdio.h> int main() { int n,i=0,j; char a[100]; scanf("%d",&n);
if(n==0)printf("%d",n); else { while(n!=0) { switch(n%16) { case
0:a[i]='0';break; case 1:a[i]='1';break; case 2:a[i]='2';break; case
3:a[i]='3';break; case 4:a[i]='4';break; case 5:a[i]='5';break; case
6:a[i]='6';break; case 7:a[i]='7';break; case 8:a[i]='8';break; case
9:a[i]='9';break; case 10:a[i]='A';break; case 11:a[i]='B';break; case
12:a[i]='C';break; case 13:a[i]='D';break; case 14:a[i]='E';break; case
15:a[i]='F';break; } n=n/16; i++; } for(j=i-1;j>=0;j--)printf("%c",a[j]); }
return 0; }
12. Hex to decimal

/* Enter a value from the keyboard that does not exceed 8 Bit positive hexadecimal digit string , Convert it to a positive decimal number and output it .

notes : In hexadecimal numbers 10~15 Use capital letters respectively A,B,C,D,E,F express .*/
#include<stdio.h> #include<string.h> #include<math.h> int main() { char
a[100]; int i; long long n=0; scanf("%s",a); for (i=0;a[i]!='\0';i++) {
if(a[i]>='A' && a[i]<='F') a[i]=(a[i]-'A')+10+'0';
n+=((a[i]-'0')*(pow(16,strlen(a)-1-i))); } printf("%lld",n); return 0; }
13. Sequence sorting

/* Given a length of n Sequence of , Arrange the sequence from small to large .1<=n<=200*/
#include<stdio.h> int main() { int n,a[1000],k,temp=0,i,j; scanf("%d",&n);
if(n<1||n>200)return 0; for(i=0;i<n;i++) { scanf("%d",&a[i]); }
for(i=0;i<n-1;i++) { k=i; for(j=i+1;j<n;j++) { if(a[j]<a[k])k=j; } temp=a[i];
a[i]=a[k]; a[k]=temp; } for(i=0;i<n;i++) printf("%d ",a[i]); return 0; }
I hope it will help you ~

Technology