C码字练习.04.For循环,do while循环 - 北方连萌

C码字练习.04.For循环,do while循环

For循环

#include<stdio.h>
int main(void)
{
   char ch;

   for (ch = 'a'; ch <= 'z'; ch++) //一个括号初值终值和增量全给表示了,比while简洁
      printf("The ASCII value for %c is %#x. \n", ch, ch); //%#x表示以0x的前缀显示为16进制数
   
   return 0;
}
The ASCII value for a is 0x61. 
The ASCII value for b is 0x62. 
The ASCII value for c is 0x63.
The ASCII value for d is 0x64.
The ASCII value for e is 0x65.
The ASCII value for f is 0x66.
The ASCII value for g is 0x67.
The ASCII value for h is 0x68.
The ASCII value for i is 0x69.
The ASCII value for j is 0x6a.
The ASCII value for k is 0x6b.
The ASCII value for l is 0x6c.
The ASCII value for m is 0x6d.
The ASCII value for n is 0x6e.
The ASCII value for o is 0x6f.
The ASCII value for p is 0x70.
The ASCII value for q is 0x71.
The ASCII value for r is 0x72.
The ASCII value for s is 0x73.
The ASCII value for t is 0x74.
The ASCII value for u is 0x75.
The ASCII value for v is 0x76.
The ASCII value for w is 0x77.
The ASCII value for x is 0x78.
The ASCII value for y is 0x79.
The ASCII value for z is 0x7a.

数组

#include<stdio.h>
int main(void)
{
   char ch[26]; //数组的容量是26
   int i;
   
   printf("请输入26个字母。\n");
   for(i = 0; i < 26; i++)
      scanf("%c", &ch[i]);
   printf("现在将打印刚刚输入的26个字母。\n");
   for(i = 0; i < 26; i++)
      printf("%c", ch[i]);
   printf("\n");
   return 0;
}
请连续输入26个字母。
qazwsxedcrfvtgbyhnujmikolp
现在将打印刚刚输入的26个字母。
qazwsxedcrfvtgbyhnujmikolp

嵌套循环

内外循环无关

#include<stdio.h>
#define R 8 
/*
这里可以凸显设定常量的便利,
只要这里变动一下就可改变整个嵌套循环,
而不用一个个进块里头改
*/
int main(void)
{
   int row, i;
   
   for(row = 1; row <= R; row++)
   {
      for(i = 1; i <= row; i++)
      {
         printf("%c", '$'); 
      }
      printf("\n");
   }
      
   return 0;
}
$
$$
$$$
$$$$
$$$$$
$$$$$$
$$$$$$$
$$$$$$$$

外部循环控制内部循环

#include<stdio.h>
#define R 6
int main(void)
{
   int row;
   char i;
   
   for(row = 1; row <= R; row++)
   {
      for(i = 'F'; i >= ('G'- row); i--)
      {
         printf("%c", i);
      }
      printf("\n");
   }
      
   return 0;
}
F
FE
FED
FEDC
FEDCB
FEDCBA

追加条件改变内部循环初始值

#include<stdio.h>
#define R 6
int main(void)
{
   int row, sum;
   char i;
   
   sum = 0;
   for(row = 0; row < R; row++)
   {
      sum += row;
      for(i = 'A' + sum; i <= ('A' + sum + row); i++)
      {
         printf("%c", i);
      }
      printf("\n");
   }
      
   return 0;
}
A
BC
DEF
GHIJ
KLMNO
PQRSTU

多个内部循环

基础版

#include<stdio.h>
#define R 10
int main(void)
{
   int row, sum;
   char i, j, k;
   
   for(row = 0; row < R; row++)
   {
      for(k = R - row - 1; k > 0; k--)
      {
         printf(" ");
      }
      for(i = 'A'; i <= ('A' + row); i++)
      {
         printf("%c", i);
      }
      for(j = 'A' + row - 1; j >= 'A'; j--)
      {
         printf("%c", j);
      }
      printf("\n");
   }
      
   return 0;
}
         A
        ABA
       ABCBA
      ABCDCBA
     ABCDEDCBA
    ABCDEFEDCBA
   ABCDEFGFEDCBA
  ABCDEFGHGFEDCBA
 ABCDEFGHIHGFEDCBA
ABCDEFGHIJIHGFEDCBA

添加交互与判断版

使用if和goto,直到用户输入符合要求的内容为止不断跳出提示

#include<stdio.h>
int main(void)
{
   int row, sum, r;
   char i, j, k, h;
   
   test:
   printf("Please enter a CAPITAL letter: ");
   scanf("%s", &h);
   if (sizeof(h) > 1)
   {
      goto test;
   }
   
   else if(h < 65)
   {
      goto test;
   }

   else if(h > 90)
   {
      goto test;
   }

   else
   {
      goto ct;
   }

   ct:
   printf("(If you enter a string with a capital initial,\nthe program will process only this initial.)\n\n");
   r = h - 64;
   for(row = 0; row < r; row++)
   {
      for(k = r - row - 1; k > 0; k--)
      {
         printf(" ");
      }
      for(i = 'A'; i <= ('A' + row); i++)
      {
         printf("%c", i);
      }
      for(j = 'A' + row - 1; j >= 'A'; j--)
      {
         printf("%c", j);
      }
      printf("\n");
   }      
   return 0;
}
Please enter a CAPITAL letter: a
Please enter a CAPITAL letter: c
Please enter a CAPITAL letter: 7
Please enter a CAPITAL letter: +-+
Please enter a CAPITAL letter: &*
Please enter a CAPITAL letter: jij
Please enter a CAPITAL letter: 74kj
Please enter a CAPITAL letter: k7
Please enter a CAPITAL letter: Fast
(If you enter a string with a capital initial,
the program will process only this initial.)

     A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA
ABCDEFEDCBA

制表

#include<stdio.h>
int main(void)
{
   int r, i;
   double a[100];

   printf("将打印一个表格,显示所输入数的1到3次方。\n");
   printf("请输入表格的行数:");
   scanf("%d", &r);
   
   printf("请输入%d个数:", r);
   for(i = 0; i < r; i++)
   {
      scanf("%lf", &a[i]);
   }
   printf("1次方          2次方          3次方\n");
   for(i = 0; i < r; i++)
   {
      printf("%-15.2f%-15.2f%-15.2f\n", a[i], a[i] * a[i], a[i] * a[i] * a[i]);
      printf("\n");
   }


   return 0;
}
将打印一个表格,显示所输入数的1到3次方。
请输入表格的行数:3
请输入3个数:74 85 96
1次方          2次方          3次方
74.00          5476.00        405224.00

85.00          7225.00        614125.00

96.00          9216.00        884736.00

倒序输出

数字

#include<stdio.h>
int main(void)
{
   int a[8], i;

   printf("请输入8个整数:");
   for(i = 0; i < 8; i++)
   {
      scanf("%d",  &a[i]);
   }
   printf("现将倒序打印这8个整数:");
   for(i = 7; i >= 0; i--)
   {
      printf("%d",  a[i]);
   }
   
   return 0;
}
请输入8个整数:1 2 3 4 5 6 7 8
现将倒序打印这8个整数:87654321

字母

#include<stdio.h>
#include<string.h>
int main(void)
{
   char name[20];
   int len, i;

   printf("Please enter your name (up to 20 characters):\n");
   scanf("%s", name);
   len = strlen(name);
   printf("Now something will happen:\n");
   for(i = len - 1; i >= 0; i--)
   {
      printf("%c", name[i]);
   }

   return 0;
}
Please enter your name(up to 20 characters):
SMITH
Now something will happen:
HTIMS

级数

序列1

$$ \sum_{n=1}^{\infty} \frac{1}{n} \to\ \infty $$

序列2

$$ \sum_{n=1}^{\infty} \frac{(-1)^{n+1}}{n}=\log (2) \approx 0.69315 $$

#include<stdio.h>
#include<math.h>
int main(void)
{
   int m, i, j;
   double t1 = 0.0, t2 = 0.0;

   printf("请输入项数:");
   scanf("%d", &m);
   while (m > 0)
   {
      t1 = 0.0;
      for(i = 1; i <= m; i++)
      {
         t1 = t1 + 1.0 / (double)i;
      }
      t2 = 0.0;
      for(j = 1; j <= m; j++)
      {
         t2 = t2 + pow(-1.0, (double)j + 1.0) / (double)j;
      }

      printf("序列1之和(%d项为止)是:%f。\n",  m, t1);
      printf("序列2之和(%d项为止)是:%f。\n",  m, t2);
      printf("请输入项数:");
      scanf("%d", &m);
   }
   printf("你输入了非正数,程序结束。\n");


   return 0;
}
请输入项数:100
序列1之和(100项为止)是:5.187378。
序列2之和(100项为止)是:0.688172。
请输入项数:1000
序列1之和(1000项为止)是:7.485471。
序列2之和(1000项为止)是:0.692647。
请输入项数:10000
序列1之和(10000项为止)是:9.787606。
序列2之和(10000项为止)是:0.693097。
请输入项数:100000
序列1之和(100000项为止)是:12.090146。
序列2之和(100000项为止)是:0.693142。
请输入项数:1000000
序列1之和(1000000项为止)是:14.392727。
序列2之和(1000000项为止)是:0.693147。
请输入项数:10000000
序列1之和(10000000项为止)是:16.695311。
序列2之和(10000000项为止)是:0.693147。
请输入项数:-100
你输入了非正数,程序结束。

Do while循环

注意while外部指定初始值,内部指定迭代的结构

#include<stdio.h>
#include<math.h>
int main(void)
{
   int a[8], i;

   printf("计算2的前8次方。\n");
   
   for(i = 0; i < 8; i++)
   {
      a[i] = pow(2, i + 1);
   }

   i = 0;
   do{
      printf("%d\n", a[i]);
      i++;
   }while(i < 8);

   return 0;
}
计算2的前8次方。
2
4
8
16
32
64
128
256

渐化式

#include<stdio.h>
int main(void)
{
   double a[8], b[8];
   int i;

   printf("请输入8个数。\n");

   for(i = 0; i < 8; i++)
   {
      scanf("%lf", &a[i]);
   }

   for(i = 0; i < 8; i++)
   {
      printf("%-9.2f", a[i]);
   }
   printf("\n");
   b[0] = a[0];
   for(i = 0; i < 8; i++)
   {
      b[i] = b[i - 1] + a[i]; //S(n) = S(n - 1) + a(n)
      printf("%-9.2f", b[i]);
   }

   return 0;
}
请输入8个数。
1 2 3 4 5 6 7 8
1.00     2.00     3.00     4.00     5.00     6.00     7.00     8.00     
1.00     3.00     6.00     10.00    15.00    21.00    28.00    36.00

单利与复利

#include<stdio.h>
#include<math.h>
int main(void)
{
   double a, b;
   int i = 1;
   
   do{
      a = 100 + 10.0 * i;
      b = 100 * pow(1.05, i);
      i++;
   }while(a > b);
   printf("经过%d年,对本金100元,5%%复利(b)的本息合计会大于10%%单利(a)。\n", --i);
   printf("此时a的本息合计是%.2f元,b的本息合计是%.2f元。\n", a, b);


   return 0;
}
经过27年,对本金100元,5%复利(b)的本息合计会大于10%单利(a)。
此时a的本息合计是370.00元,b的本息合计是373.35元。

牛吃草问题

(1)草地100坪,生草速度8坪/单位时间,吃草速度10坪/单位时间,求吃完需要的时间

#include<stdio.h>
int main(void)
{
   double a, b;
   int i = 1;
   
   do{
      a = 100 + 8 * i;
      b = 10 * i;
      i++;
   }while(a > b);
   printf("t = %d\n", --i);


   return 0;
}
t = 50

(2)草地5坪,第n周吃掉n坪的草,剩下的草翻倍成长,求草场面积超过150的耗时

#include<stdio.h>
int main(void)
{
   double a = 5.0;
   int i = 1;
   
   do{
      a = (a - i) * 2;
      printf("In the #%d week, the area will be %.1fpy.\n", i, a);
      i++;
   }while(a <= 150.0);
   printf("In the #%d week, the area will exceed 150.0py.\n", --i);


   return 0;
}
In the #1 week, the area will be 8.0py.
In the #2 week, the area will be 12.0py.
In the #3 week, the area will be 18.0py.
In the #4 week, the area will be 28.0py.
In the #5 week, the area will be 46.0py.
In the #6 week, the area will be 80.0py.
In the #7 week, the area will be 146.0py.
In the #8 week, the area will be 276.0py.
In the #8 week, the area will exceed 150.0py.

添加新评论

电子邮件地址不会被公开,评论内容可能需要管理员审核后显示。