C語言——幾道習題

來源:互聯網
上載者:User

標籤:

//###43.圍圈報數

//有n個人圍成一圈,順序排號。從第一個人開始報數(從1到m報數),凡報到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]==‘\n‘)21         {22             chs[i] = ‘\0‘;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] = ‘\0‘;34             break;35         }36     }37     printf("%s",chs);38     return 0;39 }

 

 

C語言——幾道習題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.