当前位置:网站首页>C language: Exercise 2
C language: Exercise 2
2022-06-26 13:12:00 【No code red】
Hello everyone , I am a Not allowed to . Practice together c Basic questions of language , Consolidate basic knowledge !
List of articles
One 、 fraction
Title Description :
Using the nesting of conditional operators to complete this problem : academic record >=90 It's for the students A Express ,60-89 Use between points B Express ,60 Divide the following uses C Express .
Topic analysis :
(a>b)?a:b This is a basic example of a conditional operator .
Source code ;
#include<stdio.h>
int main(){
int score;
char grade;
printf(" Please enter the score : ");
scanf("%d",&score);
grade=(score>=90)?'A':((score>=60)?'B':'C');
printf("%c\n",grade);
return 0;
}
effect :
Two 、 The common factor 、 Common multiple
Title Description :
Enter two positive integers m and n, Find the greatest common divisor and the least common multiple .
Topic analysis :
(1) Minimum common multiple = The product of the two numbers entered is divided by their greatest common divisor , The key is to find the greatest common divisor ;
(2) Finding the greatest common divisor by rolling Division ( Also known as Euclidean algorithm )
1) prove : set up c yes a and b Maximum common divisor of , Write it down as c=gcd(a,b),a>=b, Make r=a mod b
set up a=kc,b=jc, be k,j Relatively prime , otherwise c It's not the greatest common divisor According to the above ,r=a-mb=kc-mjc=(k-mj)c
You know r It's also c Multiple , And k-mj And j Relatively prime , Otherwise, it is the same as the above k,j Mutual contradiction , Thus we can see that ,b And r The greatest common divisor of c, namely gcd(a,b)=gcd(b,a
mod b), Obtain evidence .2) Algorithm description :
First step :a ÷ b, Make r Is the remainder of the result (0≤r The second step : swap : Set up a←b,b←r, And go back to the first step .
Source code :
#include<stdio.h>
int main()
{
int a,b,t,r,n;
printf(" Please enter two numbers :\n");
scanf("%d %d",&a,&b);
if(a<b)
{
t=b;b=a;a=t;}
r=a%b;
n=a*b;
while(r!=0)
{
a=b;
b=r;
r=a%b;
}
printf(" The greatest common divisor of these two numbers is %d, The minimum common multiple is %d\n",b,n/b);
return 0;
}
effect :
3、 ... and 、 Character statistics
Title Description :
Enter a line of characters , Count out the English letters 、 Space 、 The number of numbers and other characters .
Topic analysis :
utilize while sentence , The condition is that the character entered is not ’\n’.
Source code :
#include<stdio.h>
int main()
{
char c;
int letters=0,spaces=0,digits=0,others=0;
printf(" Please enter some letters :\n");
while((c=getchar())!='\n')
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
letters++;
else if(c>='0'&&c<='9')
digits++;
else if(c==' ')
spaces++;
else
others++;
}
printf(" Letter =%d, Numbers =%d, Space =%d, other =%d\n",letters,digits,spaces,others);
return 0;
}
effect :
Four 、 Sum up
Title Description :
seek s=a+aa+aaa+aaaa+aa…a Value , among a It's a number . for example 2+22+222+2222+22222( At this time, there is 5 Add up the numbers ), A keyboard controls the addition of several numbers .
Topic analysis :
The key is to calculate the value of each item .
Source code ;
#include<stdio.h>
int main()
{
int s=0,a,n,t;
printf(" Please enter a and n:\n");
scanf("%d%d",&a,&n);
t=a;
while(n>0)
{
s+=t;
a=a*10;
t+=a;
n--;
}
printf("a+aa+...=%d\n",s);
return 0;
}
effect :
5、 ... and 、1000 Within completion
Title Description :
If a number is exactly equal to the sum of its factors , This number is called " Complete ". for example 6=1+2+3. Programming to find out 1000 All the completions within .
Topic analysis :
Find all the factors of a number , Add it up , Is it equal to itself .
Source code ;
#include<stdio.h>
#define N 1000
int main()
{
int i,j,k,n,sum;
int a[256];
for(i=2;i<=N;i++)
{
sum=a[0]=1;
k=0;
for(j=2;j<=(i/2);j++)
{
if(i%j==0)
{
sum+=j;
a[++k]=j;
}
}
if(i==sum)
{
printf("%d=%d",i,a[0]);
for(n=1;n<=k;n++)
printf("+%d",a[n]);
printf("\n");
}
}
return 0;
}
effect :
6、 ... and 、 The ball fell freely
Title Description ;
A ball from 100 Free fall at meter height , Jump back to half of the original height after landing ; And then fall , Ask it in the 10 The next landing , How many meters in total ? The first 10 How high is the rebound ?
Source code :
#include<stdio.h>
int main()
{
float h,s;
h=s=100;
h=h/2; // First bounce height
for(int i=2;i<=10;i++)
{
s=s+2*h;
h=h/2;
}
printf(" The first 10 The next landing , Common course %f rice , The first 10 Secondary rebound high %f rice \n",s,h);
return 0;
}
effect :
7、 ... and 、 Monkeys eat peaches
Title Description :
The problem of monkeys eating peaches : The monkey picked some peaches on the first day , Half eaten immediately , It's not addictive , Another one
The next morning I ate half of the rest of the peaches , Another one . Every morning I eat the rest of the day before
One and a half . To the first 10 When I want to eat again in the morning , See there's only one peach left . Ask how much you picked on the first day .
Topic analysis ;
Adopt the method of converse thinking , Infer from back to front .
- set up x1 Count the peaches of the previous day , set up x2 Count the peaches for the next day , be :
x2=x1/2-1, x1=(x2+1)*2
x3=x2/2-1, x2=(x3+1)*2
And so on : x front =(x after +1)*2
- From 10 The day can be analogized to the 1 God , It's a circular process .
Source code :
#include <stdio.h>
#include <stdlib.h>
int main(){
int day, x1 = 0, x2;
day=9;
x2=1;
while(day>0) {
x1=(x2+1)*2; // The number of peaches on the first day was 2 The number of peaches in the sky 1 After 2 times
x2=x1;
day--;
}
printf(" The total number is %d\n",x1);
return 0;
}
effect ;
8、 ... and 、 match
Title Description :
Two ping-pong teams play , Three people each . Team a is for a,b,c Three people , Team B is x,y,z Three people . The match list has been drawn . Someone asked the players for the list of the game .a Say he's not at peace x Than ,c Say he's not at peace x,z Than , Please program to find out the players of the three teams .
Source code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char i,j,k;
for(i='x';i<='z';i++) {
for(j='x';j<='z';j++) {
if(i!=j) {
for(k='x';k<='z';k++) {
if(i!=k&&j!=k) {
if(i!='x'&&k!='x'&&k!='z') {
printf(" In sequence :a--%c\tb--%c\tc--%c\n",i,j,k);
}
}
}
}
}
}
}
effect :
Nine 、 Sum fractions
Title Description :
There's a sequence of scores :2/1,3/2,5/3,8/5,13/8,21/13… Find the front of this sequence 20 Sum of items .
Topic analysis :
Please grasp the law of change of the numerator and denominator .
Source code :
#include <stdio.h>
int main()
{
int i,t;
float sum=0;
float a=2,b=1;
for(i=1;i<=20;i++)
{
sum=sum+a/b;
t=a;
a=a+b;
b=t;
}
printf("%9.6f\n",sum);
}
effect :
Ten 、 Multiplicative multiplication
Title Description :
seek 1+2!+3!+…+20! And .
Topic analysis :
This program just turns accumulation into multiplication .
Source code :
#include <stdio.h>
int main()
{
int i;
double sum,mix;
sum=0,mix=1;
for(i=1;i<=20;i++)
{
mix=mix*i;
sum=sum+mix;
}
printf("%lf\n",sum);
}
effect :
边栏推荐
- Analysis and protection of heart blood dripping vulnerability (cve-2014-0160)
- Machine learning notes - seasonality of time series
- Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
- 别乱用 FULL_CASE 和 PARALLEL_CASE
- J - Wooden Sticks poj 1065
- code force Party Lemonade
- Copy multiple Excel files and name them different
- Deep parsing MySQL binlog
- Processing random generation line animation
- postgis計算角度
猜你喜欢
随机推荐
复制多个excel然后命名不同的名字
Coprime and non coprime of template problems of Chinese remainder theorem
倍福将EtherCAT模块分到多个同步单元运行--Sync Units的使用
倍福EtherCAT Xml描述文件更新和下载
sql 将数据表b字段值赋值到数据表a中某一列
原型模式(prototype)
软件测试报告应该包含的内容?面试必问
B - Bridging signals
Vivado error code [drc pdcn-2721] resolution
P5733 [deep foundation 6. example 1] automatic correction
postgis 地理化函数
Stream learning record
自动化测试的局限性你知道吗?
Processing polyhedron change
Processing random generation line animation
D - skiing
Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan
Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
postgis计算角度
单例的常用创建和使用方式








