この記事には、次の 6 つのパート (各パート 5 つ) の C 言語の小さな練習問題が含まれています。
1.
1.1 韓信の兵士 ------------ 1.2 蘭州ビスケット -------- - - 1.3 16 進数変換 --------- 1.4 日 ----------- 1.5 スコア変換
2.
2.1 実数の絶対値を求める ----2 2.2 実数の体積を計算するボール -------- 2.3 2 点間の距離 --------- 2.4 ASCII コードのソート - 2.5 数値統計
3.
3.2 公約数と公倍数 ---- 3.2 5 つの数値の最大値を求める - ---- 3.3 素数ふるいのアルゴリズム----3.4 得点の加算と減算-----3.5 生徒の平均成績を求める配列
4.3
パリティ交換 ------ 4.4 コインの統計 ------ -- 4.5 漢字の統計
5.
5.1 偶数の和 ----------- 5.2 陽輝三角形 ----- ------ 5.3 文字の統計 -------- - 5.4 完全な数 ------------ 5.5 プライム回文
6.
6.1 クイックソート -------- ---- 6.2 ドアを開ける人、ドアを閉じる人-- 6.3 同じ檻の中のニワトリとウサギ -------- 6.4 日付の計算 ------- 6.5 電気をつけるときの問題
1.1 韓信は
兵士たちに3人、5人、7人ずつの列を作るよう命じ、全体の人数は列の最後尾の人数で分かる(合計数は10~100人にする)。
各フォーメーションの終了時の人数(a<3、b<5、c<7)、つまり余りを示す 3 つの非負の整数 a、b、c を入力し、その最小値を出力します。人の総数 (または解決策を報告しない)。
#include <stdio.h>
int main()
{
int a,b,c,i;
re_input:printf("\n");
printf("Please input the value of a,b and c with space between them:");
scanf("%d%d%d",&a,&b,&c);
if(a>=3 || a<0 || b>=5 || b<0 || c>=7 || c<0)
{
printf("Input is wrong, please re-input.");
goto re_input;
}
for(i=10;i<=100;i++)
if(i%3==a && i%5==b && i%7==c)
{
printf("This army has %d people.",i);
break;
}
if(i>100)
printf("No answer!");
}
.
.
1.2 蘭州ビスケット
ビスケットには両面があり、蘭州ビスケットを作るには両面を加熱する必要があります。こんなに大きな鍋があり、蘭州ビスケットも同時に入れることができます。
片面は 1 分で準備できますが、蘭州ビスケットは n 枚あります。すべてを準備するには少なくとも何分かかりますか?
#include <stdio.h>
int main()
{
int n,k,total,result;
printf("Please input the value of k and n with space between them:");
scanf("%d%d",&k,&n);
for(;;)
{
total=2*n;
if(n<=k)
{
printf("It takes only two minutes."); break;}
result=total/k;
if(total%k==0)
{
printf("It takes %d minutes.",result); break;}
else
{
printf("It takes %d minutes.",++result); break;}
}
}
.
.
1.3 16進数変換
10進数nを入力し、m進数に変換して出力します(最大16進数)。
#include <stdio.h>
#include <string.h>
char trans(int n,int m);
int main()
{
int n,m,i,t;
char a[50];
re_input:printf("\n");
printf("Please input a decimalism number n:");
scanf("%d",&n);
printf("Please input another scale number m:");
scanf("%d",&m);
if(n<0 || m<0 || m>16) {
printf("Your input is wrong, please try it again."); goto re_input;}
for(i=0;i<50;i++)
{
a[i]=trans(n,m);
n=n/m;
if(n==0) {
t=i; break;}
}
for(i=t;i>=0;i--)
printf("%c",a[i]);
goto re_reput;
}
char trans(int n,int m)
{
switch(n%m)
{
case 0: return '0'; break;
case 1: return '1'; break;
case 2: return '2'; break;
case 3: return '3'; break;
case 4: return '4'; break;
case 5: return '5'; break;
case 6: return '6'; break;
case 7: return '7'; break;
case 8: return '8'; break;
case 9: return '9'; break;
case 10: return 'A'; break;
case 11: return 'B'; break;
case 12: return 'C'; break;
case 13: return 'D'; break;
case 14: return 'E'; break;
case 15: return 'F'; break;
}
return 0;
}
.
.
1.4 何の日?
日付を指定すると、その日付が何日かを出力します。
#include <stdio.h>
#include <string.h>
int trans(int mon, int m);
int main()
{
int year,mon,day,i,m=0,sum=0;
re_input:printf("\n");
printf("Please input the value of year, mon and day:");
scanf("%d%d%d",&year,&mon,&day);
if((year%4==0 && year%100!=0) || year%400==0) m=1;
if(year<0 || mon<0 || mon>12 || day<0 || day>31 || mon==2 &&((m==1 && day>29) || (m==0 && day>28)))
{
printf("Your input is wrong, please try it again."); goto re_input;}
for(i=1;i<mon;i++)
sum=sum+trans(i,m);
sum=sum+day;
printf("%d-%d-%d is the %dth day of the year.",year,mon,day,sum);
}
int trans(int mon, int m)
{
switch(mon)
{
case 1: return 31; break;
case 2:
{
if(m==0) return 28;
else return 29;
break;
}
case 3: return 31; break;
case 4: return 30; break;
case 5: return 31; break;
case 6: return 30; break;
case 7: return 31; break;
case 8: return 31; break;
case 9: return 30; break;
case 10: return 31; break;
case 11: return 30; break;
case 12: return 31; break;
}
}
.. 1.5 スコア変換
5パーセンタイルのスコアを (ランダムに) 入力し、対応するグレードに変換します。具体的な変換ルールは次のとおりです: 90 100は A; 80 89 は B; 70 79 は C; 60 69 は D; 0 ~ 59 Eの場合
#include <stdio.h>
#include <string.h>
#include <time.h>
char trans(int score);
int main()
{
int score[5],i;
memset(score,0,sizeof(score));
srand(time(0));
for(i=0;i<5;i++)
{
re_input:printf("\n");
printf("Please input the score of No.%d student:",i+1);
scanf("%d",&score[i]);
if(score[i]>100 || score[i]<0)
{
printf("Your input is wrong, please try it again."); goto re_input;}
//score[i]=rand()%101; //随机获得五个0-100之间的整数值,可代替上4行。
printf("The score of No.%d student is %d, corresponding level is:%c.\n",i+1,score[i],trans(score[i]));
}
}
char trans(int score)
{
if(score>=90) return 'A';
else if(score>=80) return 'B';
else if(score>=70) return 'C';
else if(score>=60) return 'D';
else if(score>=0) return 'E';
}
.
.
2.1 実数の絶対値を求める
7 つの数値を入力し、その絶対値を出力し、小数点以下 2 桁を保持します。
#include <stdio.h>
int main()
{
double num,tru;
int i;
for(i=1;i<=7;i++)
{
if(i==1) printf("Please the %dst number:",i);
else if(i==2) printf("Please the %dnd number:",i);
else if(i==3) printf("Please the %drd number:",i);
else printf("Please the %dth number:",i);
scanf("%lf",&num);
if(num<0) tru=-num;
else tru=num;
printf("|%lf|=%.2lf\n",num,tru);
}
}
.
.
2.2 球の表面積と体積を計算する
入力された半径値に従って、球の表面積と体積を計算します。
#include <stdio.h>
#include <math.h>
#define PI 3.1415927
int main()
{
double R,A,V;
printf("Please enter the radius of sphere:");
scanf("%lf",&R);
A=4*PI*pow(R,2);
V=4*PI*pow(R,3)/3;
printf("The Area of sphere is %.3lf\nThe Volume of sphere is %.3lf",A,V);
}
.
.
2.3 2点間距離
2点(X1,Y1)、(X2,Y2)の座標(0<=x1,x2,y1,y2<=1000)を入力し、2点間の距離を計算して出力します。
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
double x[2],y[2],dist;
printf("Please enter the x and y coordinate of the first point:");
scanf("%lf%lf",&x[0],&y[0]);
printf("Please enter the x and y coordinate of the second point:");
scanf("%lf%lf",&x[1],&y[1]);
dist=pow((pow(x[1]-x[0],2)+pow(y[1]-y[0],2)),0.5);
printf("The distance of the first point and the second point is %.3lf:",dist);
}
。
。
2.4ASCIIコードソート
3文字(繰り返し可)を入力後、その3文字を各文字のASCIIコードに従って昇順に出力します。
#include <stdio.h>
void main()
{
char a,b,c,t;
printf("Please enter three characters:");
scanf("%c%c%c",&a,&b,&c);
if(a>b) {
t=a; a=b; b=t;}
if(a>c) {
t=a; a=c; c=t;}
if(b>c) {
t=b; b=c; c=t;}
printf("The result of sort:%c < %c <%c",a,b,c);
}
。
。
2.5 数値統計
与えられた n 個の数値のうち、負の数、ゼロ、正の数の数を数えます。
#include <stdio.h>
#include <string.h>
#include <math.h>
void main()
{
int n,i,pos=0,zer=0,neg=0;
srand(time(0));
n=rand()%100+1;
int a[n];
printf("Total number: %d\n",n);
for(i=0;i<n;i++)
{
a[i]=rand()%100-50;
printf("%6d",a[i]);
if((i+1)%10==0) printf("\n");
if(a[i]>0) pos++;
else if(a[i]==0) zer++;
else neg++;
}
printf("\nThe number of positive is: %d",pos);
printf("\n The number of 0 is: %d",zer);
printf("\nThe number of negative is: %d\n",neg);
}
.
.
3.1 公約数と公倍数
2 つの正の整数を入力し、その最大公約数と最小公倍数を求めます。
#include <stdio.h>
void main()
{
int m,n,i,t;
printf("Please enter two numbers:");
scanf("%d%d",&m,&n);
for(i=1;i<=m*n;i++)
{
if(m%i==0 && n%i==0) t=i;
if(i%m==0 && i%n==0)
{
printf("%3d is the greatest common factor of %d and %d\n",t,m,n);
printf("%3d is the lowest common multiple of %d and %d",i,m,n);
break;
}
}
}
。
。
3.2 5 つの数値の最大値を見つける
5 つのランダムな整数から最小値と最大値を取得します。
#include <stdio.h>
#include <time.h>
void main()
{
int a[5],i,t;
srand(time(0));
for(i=0;i<5;i++)
{
a[i]=rand()%100-50; printf("%-4d",a[i]);}
for(i=1;i<5;i++)
if(a[0]>a[i]) {
t=a[0]; a[0]=a[i]; a[i]=t;}
for(i=0;i<4;i++)
if(a[4]<a[i]) {
t=a[4]; a[4]=a[i]; a[i]=t;}
printf("\n%3d is the min\n%3d is the max",a[0],a[4]);
}
.
.
3.3 素数アルゴリズム 正
の整数 N を入力し、数値 2…N の中のすべての素数を求めます。
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int n,i,j,ct=0;
printf("Please enter the up limited value n:");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
for(j=2;j<i;j++)
if(i%j==0)
break;
if(j==i)
{
printf("%-4d ",i);
ct++;
if(ct%10==0)
printf("%\n");
}
}
}
。
。
3.4 スコアの足し算と引き算
プラスチック データを使用して、2 つの分数の足し算と引き算を実現します。
#include <stdio.h>
int main()
{
int a,b,c,d,denom,numer,i,t;
char fh;
printf("Please enter the fraction equation(like a/b ± c/d):");
re_input:printf("\n");
scanf("%d/%d%c%d/%d",&a,&b,&fh,&c,&d);
denom=b*d;
if(denom==0) {
printf("Your input is wrong, please try it again.\n"); goto re_input;}
if(fh=='+') {
numer=a*d+b*c; printf("The original answer is %d/%d",numer,denom);}
if(fh=='-') {
numer=a*d-b*c; printf("The original answer is %d/%d",numer,denom);}
for(i=1;i<=(abs(denom)<abs(numer)?abs(denom):abs(numer));i++)
if(denom%i==0 && numer%i==0)
t=i;
numer=numer/t;denom=denom/t;
if(numer%denom==0) printf("\nThe simplified answer is %d",numer/denom);
if(numer*denom<0) printf("\nThe simplified answer is -%d/%d",abs(numer),abs(denom));
if(numer*denom>0) printf("\nThe simplified answer is %d/%d",abs(numer),abs(denom));
}
.
. .
3.5 配列内の生徒の平均成績を求める
2 つの学年、3 つのクラス、各クラスに 4 人の生徒がいる学校があると仮定し、クラスの平均成績、学年の平均成績、および学校の平均成績をそれぞれ求めます。
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int i,j,k,g=2,c=3,s=4;
double sc,ss,sum=0,a[s];//a[g][c][s];
memset(a,0,sizeof(a));
for(i=0;i<g;i++)
{
sc=0;
for(j=0;j<c;j++)
{
ss=0;
for(k=0;k<s;k++)
{
printf("请输入年级%d,%d班第%d名学生的成绩:",i+1,j+1,k+1);
scanf("%lf",&a[k]);
ss=ss+a[k];//a[i][j][k]
}
printf("\n年级%d,%d班学生的平均成绩为:%f\n\n",i+1,j+1,ss/s);
sc=sc+ss;
}
printf("\n年级%d学生的平均成绩为:%f\n\n\n",i+1,sc/(s*c));
sum=sum+sc;
}
printf("\n全校学生的平均成绩为:%f\n",sum/(s*c*g));
return 0;
}
。
。
4.1 m 番目に小さい整数
n 個のランダムな (自分で入力することもできます) 整数の中から m 番目に小さい数を見つけます。
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
int n,m;
srand(time(0));
printf("Please enter the total number and the mth smallest number:");
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m>n) {
printf("Your input is wrong, please try it again.\n"); continue;}
int a[n],i,j,t;
for(i=0;i<n;i++)
{
a[i]=rand()%101-50; printf("%5d",a[i]);}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j]) {
t=a[i]; a[i]=a[j]; a[j]=t;}
printf("\n");
for(i=0;i<n;i++)
printf("%5d ",a[i]);
printf("\nThe %dth smallest number is %d:",m,a[m-1]);
}
}
.
. .
4.2 奇数と偶数の分離
100以内の正の整数nをランダムに与え、1からnまでのすべての奇数と偶数を小さい順に出力します。
#include <stdio.h>
#include <time.h>
void main()
{
int i,n;
srand(time(0));
n=2*(rand()%51);
printf("n=%d\n",n);
printf("The even number:\n");
for(i=0;i<n;i+=2)
{
printf("%3d",i);
if((i+2)%20==0) printf("\n");
}
printf("\nThe odd number:\n");
for(i=1;i<n;i+=2)
{
printf("%3d",i);
if((i+1)%20==0) printf("\n");
}
}
.
.
4.3 文字列パリティ交換
偶数桁の文字列(漢字を除く)を入力し、文字列パリティ交換を実現するようにプログラムしてください。
#include <stdio.h>
#include <string.h>
int main()
{
int n,i;
char s[100];
memset(s,0,100);
printf("Please enter the character string:");
while(scanf("%s",s)!=EOF)
{
n=strlen(s);
if(n%2!=0) {
printf("Your input is wrong, Please try it again.\n"); continue;}
int t;
for(i=0;i<n;i+=2) {
t=s[i]; s[i]=s[i+1]; s[i+1]=t;}
for(i=0;i<n;i++) printf("%c",s[i]);
}
}
.
. .
4.4 統計的なコイン
1 セント、2 セント、5 セントからなる n 枚のコインの山の合計額面が m セントであると仮定し、考えられる組み合わせが何通りあるかを調べます (特定のコインの数が 0 になる可能性もあります)。
#include <stdio.h>
int main()
{
int i,j,k,n,m;
printf("Please enter the total quantity and the total value of coin:");
while(scanf("%d%d",&n,&m)!=EOF)
{
int ct=0;
if(n>m || 5*n<m) {
printf("Your input is wrong, Please try it again.\n"); continue;}
printf("One cent\t Two cents\t Five cents\t Total value");
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
for(k=0;k<=n;k++)
if(i+j+k==n && i+2*j+5*k==m)
{
printf("\n %-8d%11d%16d%18d\n",i,j,k,m); ct++;}
if(ct==0) printf("\n\nNo solution.\n");
}
}
.
. .
4.5 中国語の文字の数を数える
指定されたテキスト内の中国語の文字の数を数えます (英語を混ぜることはできますが、スペースは許可されません)。
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
memset(s,0,1000);
printf("Please enter the character string without space:"); //不带空格,否则字符串结束。
while(scanf("%s",s)!=EOF)
{
int n=strlen(s),i,ct=0;
for(i=0;i<n;i+=2)
{
if(i==0 && s[i]>=0 && s[i]<=127) {
i--; continue;}
if((s[i]>127 || s[i]<0) && (s[i+1]>127 || s[i+1]<0)) ct++;
else i--;
}
printf("There are %d Chinese characters.\n",ct);
}
}
.
. .
5.1 偶数列の平均数を計算する
偶数列 (0 ~ 200) は、最初の n 個の数の m 個ごとの平均数を計算し、最後の数が m 個未満の場合は、実際の数の平均をとります。番号。
#include <stdio.h>
int main()
{
int a[101],n,m,i,j;
for(i=0;i<101;i++)
{
a[i]=2*i; printf("%-4d",a[i]);}
printf("\nPlease enter the anterior number n and the average number m:");
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<=n/m;i++)
{
int sum=0,t;
if(i==n/m) t=n-i*m;
else t=m;
for(j=0;j<t;j++) sum=sum+a[i*m+j];
if(t!=0) printf("%-4d",sum/t);
}
printf("\n");
}
}
.
.
5.2 陽輝三角形
陽輝三角形のグラフィックを n 行印刷します。
#include <stdio.h>
int main()
{
int n,i,j;
printf("Please enter the row quantity of Yang hui's triangle:");
scanf("%d",&n);
int a[n][n];
for(i=0;i<n;i++)
for(j=0;j<=i;j++)
{
if(j==0 || i==j)
{
a[i][j]=1; printf("%-6d",a[i][j]);}
else if(i>1 && j>0)
{
a[i][j]=a[i-1][j-1]+a[i-1][j]; printf("%-6d",a[i][j]);}
if(i==j) printf("\n");
}
}
.
. .
5.3 文字統計
文字列 s2 の各文字が s1 に出現する回数をカウントします (スペースを含む)。
#include <stdio.h>
#include <string.h>
int main()
{
int i,j,n,m=100;
char s1[m],s2[m];
memset(s1,0,100);
memset(s2,0,100);
printf("Please enter the character string s1:");
fgets(s1,100,stdin);
printf("Please enter the character string s2:");
fgets(s2,100,stdin);
n=strlen(s2);
int a[n];
memset(a,0,sizeof(a));
for(i=0;i<n;i++)
{
if(s2[i+1]=='\0') break; //fgets(str,n,*p)的长度比scanf多1,末尾的\0也计入长度,因此需要判断跳出循环。
for(j=0;j<m;j++)
if(s2[i]==s1[j]) a[i]++;
printf("\nCharacter '%c' in s2 appears -%d- times in s1.\n",s2[i],a[i]);
}
}
.
. .
5.4 完全数
6=1+2+3 など、1 ~ n (n を含む) の完全数をすべて求めます。1 は完全数ではありません。
#include <stdio.h>
#include <string.h>
int main()
{
int n;
printf("Please enter the upper limited value n:");
while(scanf("%d",&n)!=EOF)
{
int i,j,ct=0;
for(i=2;i<=n;i++)
{
int sum=0;
for(j=1;j<i;j++)
if(i%j==0) sum=sum+j;
if(sum==i) {
printf("%d is a perfect number.\n",i); ct++;}
if(i==n-1 && ct==0) printf("There are zero perfect number between 1 and %d.\n",n);
}
printf("\n");
}
}
.
.
5.5 素数回文
a~b (b および b<=100000 を含む) のすべての素数を出力し、回文数となります。
#include <stdio.h>
#include <string.h>
int main()
{
int a,b;
printf("Please enter the number a and b:\n");
while(scanf("%d%d",&a,&b)!=EOF)
{
if(b<=0 || a>100000 || a>b)
{
printf("Your input is wrong, please try it again:\n"); continue;}
if(a<=0) a=1;
int i,j,ct=0;
for(i=a;i<=b;i++)
{
for(j=2;j<i;j++)
if(i%j==0) break;
if(i==j)
{
// It can be considered to use loop bodies to replace this loop body, to make a simpler program probably.
if(i<10)
{
printf("%-6d",i); ct++; if(ct%10==0) printf("\n");}
else if(i<100 && i/10==i%10)
{
printf("%-6d",i); ct++; if(ct%10==0) printf("\n");}
else if(i<1000 && i/100==i%10)
{
printf("%-6d",i); ct++; if(ct%10==0) printf("\n");}
else if(i<10000 && i/1000==i%10 && (i/10)%10==(i%1000)/100)
{
printf("%-6d",i); ct++; if(ct%10==0) printf("\n");}
else if(i/10000==i%10 && (i/10)%10==(i%10000)/1000)
{
printf("%-6d",i); ct++; if(ct%10==0) printf("\n");}
}
}
printf("\n");
}
}
.
.
6.1 単純な選択ソート
0 から 100 までの n 個の数値を選択してソートします (インターネット上には多くのソート方法がありますが、ここでは単純な選択ソート方法を示します)。
#include <stdio.h>
#include <string.h>
#include <time.h>
void main()
{
srand(time(0));
int n,i,j;
printf("Please enter the number n:");
scanf("%d",&n); // It's better to make the n smaller than 100 to avoid generating too many repetitive numbers.
int a[n];
memset(a,0,n);
printf("Original data:\n");
for(i=0;i<n;i++)
{
a[i]=rand()%101; printf("%-4d",a[i]);}
for(i=0;i<n;i++)
{
int k=i,temp;
for(j=i+1;j<n;j++)
if(a[k]>a[j]) k=j;
temp=a[i];a[i]=a[k];a[k]=temp;
}
printf("\nSorted data:\n");
for(i=0;i<n;i++)
printf("%-4d",a[i]);
}
。
。
6.2 早着者と遅刻者
従業員の数を入力し、名前番号と早着者と遅刻者を入力して、誰が最も早く到着し、誰が最も遅く退社するかを決定します。
当時の入力形式の判断により、この段落は非常に複雑に書きました。
#include<stdio.h>
#include<string.h>
int trans(char at,char lt,int b,int c);
int ctoin(char c,int a);
struct staff
{
char name[1000];
char at[1000];
char lt[1000];
}st[1000];
void main()
{
int a_time[1000],l_time[1000];
memset(a_time,0,1000);
memset(l_time,0,1000);
memset(st,0,1000);
int n,i,j;
printf("Please enter the number of staff:");
scanf("%d",&n);
printf("name format:whatever\n");
printf("time format:22:22\n\n");
for(i=0;i<n;i++)
{
printf("Please enter the name,arrival time and leaving time of No.%d staff:",i+1);
scanf("%s%s%s",&st[i].name,&st[i].at,&st[i].lt);
// 对时间格式进行严格判断。
if(strlen(st[i].at)!=5 || strlen(st[i].lt)!=5 // 确保是五个字符
|| trans(st[i].at[0], st[i].lt[0],48,50) // 确保time第一个字符只能是'0~2'
|| trans(st[i].at[1], st[i].lt[1],48,57) // 确保time第一个字符只能是'0~9'
|| st[i].at[2]!=58 || st[i].lt[2]!=58 // 确保time第一个字符只能是': '
|| trans(st[i].at[3], st[i].lt[3],48,54) // 确保time第一个字符只能是'0~6'
|| trans(st[i].at[4], st[i].lt[4],48,57)) // 确保time第一个字符只能是'0~9'
//上述代码是调用函数实现,也可直接使用下五行代码实现,但是会比较复杂。
// || st[i].at[0]<48 || st[i].lt[0]<48 || st[i].at[0]>50 || st[i].lt[0]>50
// || st[i].at[1]<48 || st[i].lt[1]<48 || st[i].at[1]>57 || st[i].lt[1]>57
// || st[i].at[2]!=58 || st[i].lt[2]!=58
// || st[i].at[3]<48 || st[i].lt[3]<48 || st[i].at[3]>54 || st[i].lt[3]>54
// || st[i].at[4]<48 || st[i].lt[4]<48 || st[i].at[4]>57 || st[i].lt[4]>57)
{
printf("Your input is wrong, please try it again.\n");
i--; continue;
}
else
for(j=0;j<5;j++)
{
if(j==2) continue;
a_time[i]=a_time[i]+ctoin(st[i].at[j],j);
l_time[i]=l_time[i]+ctoin(st[i].lt[j],j);
}
}
for(i=0;i<n;i++)
{
int cta=0,ctl=0;
for(j=0;j<n;j++)
{
if(a_time[i]<=a_time[j]) cta++;
if(l_time[i]>=l_time[j]) ctl++;
}
if(cta==n) printf("\n%s was the first one to arrive, the arrival time is %s.\n",st[i].name,st[i].at);
if(ctl==n) printf("\n%s was the last one to leave, the leaving time is %s.\n",st[i].name,st[i].lt);
}
}
// 子函数部分
int trans(char at,char lt,int b,int c)
{
return(at<b || lt< b || at>c || lt>c);
}
int ctoin(char c,int a)
{
if(a==0) return(10*(c-48)*3600);
if(a==1) return( (c-48)*3600);
if(a==3) return(10*(c-48)*60);
if(a==4) return( (c-48)*60);
}
。
。
6.3 同じかごの中のニワトリとウサギ
ニワトリとウサギの総数は n で、足の総数は m であることがわかっています。nとmを入力して、ニワトリとウサギの数を順番に出力します。
#include <stdio.h>
int main()
{
int n,m,i,j;
printf("Please enter the total number and total legs number:");
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n>m/2) {
printf("Incorrect input! please try it again."); continue;}
if(m%2!=0){
printf("No solution."); continue;}
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
if(i+j==n && 2*i+4*j==m) printf("%d chickens and %d rabbits.",i,j); }
}
}
.
.
6.4 日付の計算
(前日と同様、詳細については前のコードを参照してください。そのため、コードはここでは示しません。)
2010 10 24 の形式で日付を入力し、この日が今年の何日であるかを確認します。空。
.
.
6.5 照明をつける問題
1 ~ n までの番号が付けられた n 個の照明があり、最初の人がすべての照明を点け、次に i 番目の人が i の倍数の番号が付けられたすべてのスイッチを押す、というようになります。合計 k 人、入力: n と
k 、最後にどのライトが点灯したままになったかを尋ねます。k≤n≤1000
#include <stdio.h>
int main()
{
int n,k,i,j;
printf("Please enter the number of light and people:");
while(scanf("%d%d",&n,&k)!=EOF)
{
if(k>n) {
printf("Incorrect input! please try it again."); continue;}
int light[n],ct=0;
for(i=0;i<n;i++)
light[i]=1;
// 纵向思路
for(i=0;i<n;i++)
for(j=2;j<=k;j++)
if((i+1)%j==0) light[i]=!light[i];
// 横向思路
//for(i=2;i<=k;i++)
// for(j=0;j<n;j++)
// if((j+1)%i==0) light[j]=!light[j];
for(i=0;i<n;i++)
{
if(light[i]==1)
{
printf("%-5d",i+1);
ct++;
if(ct%10==0) printf("\n");
}
}
}
}
各プログラムについて異なる意見やより良いアイデアがある場合は、コメント欄にメッセージを残して一緒に議論してください。
プログラムにバグやエラーを見つけた場合は、すぐに指摘して修正してください。
.
.
**免責事項:** 3.5 を除き、この記事のすべての演習は https://bingyishow.top/Technical-article/16.html の最初の 6 つの部分から引用されており、合計 9 つの部分があります
。この記事の内容と解決策は、記事中の質問も理解できなかったので私が一人で書いたものですので、興味のある方はTaさんのプログラムコードを見てみてください。· ·それでは最後の3部分を書く時間がないので、この6部分を先に投稿します。