C语言——几道习题

//###43.围圈报数

//n个人围成一圈,顺序排号。从第一个人开始报数(从1m报数),凡报到m的人退出圈子,问最后留下的是原来第几号的哪位。(*****

 1 int main()
 2 {
 3     int n=0,m=0;
 4     scanf("%d %d",&n,&m);
 5     int a[100] = {};
 6     int i = 0;
 7     int cnt = 0;//统计数到第几个人,循环m
 8     int count = 0;//循环跳出条件,当到n-1个人时跳出循环
 9     for(i=0;i<n;i++)
10     {
11         a[i] = 1;
12     }
13     i = 0;
14     while(a[i])
15     {
16         cnt++;
17         if(cnt==m)
18         {
19             a[i] = 0; //退出
20             cnt = 0; //重新开始报数
21             count++; //有count个人已经退出
22         }
23         i++;
24         if(i==n)
25         {
26 //          到了数组的末尾,循环边界,赋值从第一个开始遍历
27             i = 0;
28         }
29         if(count > n-1)
30         {
31             break;
32         }
33     }
34     for(int j=0;j<n;j++)
35     {
36         if(a[j])
37         {
38             printf("%d %d",a[j],j);
39             break;
40         }
41     }
42     return 0;
43 }

//###7.计算某个由英文、数字以及标点符号构成的数组的总宽度,其中英文字符的宽度为
//1cm,数字宽度为 0.5cm、标点符号宽度为 0.8cm

//8.接上题,如果规定行的宽度为 10cm,将某个字符长度超过 50 的字符串截断,恰好 使 10cm 宽的行能容纳。输出这个被截断的子数组。

 1 float getCharacterWeidth(char c)
 2 {
 3     if((c>='A' && c<='Z') || (c>='a' && c<='z'))
 4     {
 5         return 1.0;
 6     }else if(c>='0' && c<='9')
 7     {
 8         return 0.5;
 9     }else{
10         return 0.8;
11     }
12 }
13 int main()
14 {
15     char chs[100] = {};
16     int cnt = 0;
17     for(int i=0;i<100;i++)
18     {
19         scanf("%c",&chs[i]);
20         if(chs[i]=='
')
21         {
22             chs[i] = '';
23             break;
24         }
25         cnt++;
26     }
27     float length = 0;
28     for(int i=0;i<cnt;i++)
29     {
30         length += getCharacterWeidth(chs[i]);
31         if(length>10)
32         {
33             chs[i] = '';
34             break;
35         }
36     }
37     printf("%s",chs);
38     return 0;
39 }

 

原文地址:https://www.cnblogs.com/BeyondAverage0908/p/4547500.html