[轉載] C Primer Plus 第6章 C控制語句 6.16 編程練習及答案

參考鏈接: 用Python打印金字塔圖案的程序

2019獨角獸企業重金招聘Python工程師標準>>>? ?

??

?1、編寫一個程序,創建一個具有26個元素的數組,并在其中存儲26個小寫字母。并讓該程序顯示該數組的內容。?

?#include?

int main (void)

{

? ? int i;

? ? int max=26;

? ? char CH[max];

?

? ? for(i=0;i?

?2、使用嵌套循環產生下列圖案:?

?$?

?$$?

?$$$?

?$$$$?

?$$$$$?

?

#include?

int main (void)

{

? ? int i,j;

?

? ? for(i=0;i<5;i++)

? ? {

? ? ? ? for (j=0;j<=i;j++)

? ? ? ? ? ? printf("$");

? ? ? ? printf("\n");

? ? }

? ? return 0;

}

?

?3、使用嵌套循環產生下列圖案:?

?F?

?FE?

?FED?

?FEDC?

?FEDCB?

?FEDCBA?

?

#include?

int main (void)

{

? ? int i,j;

? ? char ch='F';

?

? ? for(i=0;i<6;i++)

? ? {

? ? ? ? for (j=0;j<=i;j++)

? ? ? ? ? ? printf("%C",ch-j);

? ? ? ? printf("\n");

? ? }

? ? return 0;

}

?

?4、讓程序要求用戶輸入一個大寫字母,使用嵌套循環產生像下面這們的金字塔圖案:?

? ? ?A?

? ? ABA?

? ?ABCBA?

? ABCDCDA?

?ABCDEDCBA?

?這種圖案要擴展到用戶輸入的字符。例如前面的圖案是在輸入E時需要產生的。提示:使用一個外部循環來處理行,在每一行中使用三個內部循環,一個處理空格,一個以升序打印字母,一個以降序打印字母。??

?#include?

int main (void)

{

? ? int i,j,k;

? ? char letter;

?

? ? printf("Please input a capital:");

? ? scanf("%c",&letter);

? ? k=letter-'A'+1;? ? ? ? ? ? ? ? ? ? //計算字符的個數;

? ? for(i=0;i=0;j--)? ? ? ? ? ? //第三個循環,降序打印字母(比升序時少1個字母)

? ? ? ? printf("%c",'A'+j);

? ? ? ? printf("\n");

? ? }? ??

return 0;

}

? ??

? 5、編寫一個程序打印一個表,表的每一行都給出一個整數、它的平方以及它的立方。要求用戶輸入表的上限和下限。使用一個for循環。?

?#include

int main (void)

{

? ? int i,min,max;

? ? printf("Please input the min and the max :");

? ? scanf("%d %d",&min,&max);

? ? printf("%10s%10s%10s\n","num","square","cube");

? ? for(i=min;i<=max;i++)

? ? {

? ? ? ? printf("%10d%10d%10d\n",i,i*i,i*i*i);

? ? }

? ? return 0;

}

?

?6、編寫一個程序把一個單詞讀入一個字符數組,然后反向打印這個詞。提示:使用strlen()計算數組中最后一個字符的索引。?

?#include?

#include? ?//使用strlen()

int main (void)

{

? ? char word[20];

? ? int i;

? ? printf("Please input a word:");

? ? scanf("%s",&word);

? ? for(i=strlen(word)-1;i>=0;i--)? //最后一個字符的下標strlen(word)-1,word是數組名稱

? ? ? ? printf("%c",word[i]);

? ? printf("\n");

? ? return 0;

}?

?7、編寫一個程序要求輸入兩個浮點數,然后打印用兩者的差值除以兩者的乘積所得的結果。在用戶鍵入非數字的輸入之前程序循環處理每對輸入值。?

?#include?

int main (void)

{

? ? float f1,f2;

? ? printf("Please input tow floats:");

? ? while(scanf("%f%f",&f1,&f2)==2)

? ? {

? ? ? ? printf("The result is %.2f\n",(f1-f2)/(f1*f2));

? ? ? ? printf("Please input tow floats:");

? ? }

? ? printf("end\n");

? ? return 0;

}

?

?8、對練習7進行修改,讓它使用一個函數來返回計算值。?

?#include?

float fac(float a,float b);

int main (void)

{

? ? float f1,f2;

? ? printf("Please input tow floats:");

? ? while(scanf("%f%f",&f1,&f2)==2)

? ? {

? ? ? ? printf("The result is %.2f\n",fac(f1,f2));

? ? ? ? printf("Please input tow floats:");

? ? }

? ? printf("end\n");

? ? return 0;

}

float fac(float a,float b)

{

? ? float c;

? ? c=(a-b)/(a*b);

? ? return c ;

}

?

?9、編寫一個程序,要求用戶輸入下限整數和一個上限整數,然后,依次計算從下限到上限的每一個整數的平方的加和,最后顯示結果。程序將不斷提示用戶輸入下限整數和上限整數并顯示出方法,直到用戶輸入的上限整數等于或小于下限整數為止。程序運行結果示例應該如下所示?

?Enter lower and upper integer limits:5 9?

?The sums of the squares from 25 to 81 is 255?

?Enter next set of limits :3 25?

?The sums of the squares from 9 to 625 is 5520?

?Enter next set of limits :5 5??

?Done?

?#include?

int fac(int a,int b);

int main (void)

{

? ? int n1,n2;

? ? printf("Enter lower and upper integer limits:");

? ? scanf("%d %d",&n1,&n2);

? ? while(n1?

?10、編寫一個程序,把8個整數讀入一個數組中,然后以相反的順序打印它們;?

?

#include?

int main (void)

{

? ? int abc[8];

? ? int i;

? ? printf("Please input 8 numbers:");

? ? for(i=0;i<8;i++)? ? ? ? ? //循環讀取輸入

? ? {

? ? ? ? scanf("%d",&abc[i]);

? ? }

? ? for(i=7;i>=0;i--)

? ? {

? ? ? ? printf("The %d is %d\n",i+1,abc[i]);

? ? }

? ? return 0;

}

?

?11、考慮這兩個無限序列:?

?1.0+1.0/2.0+1.0/3.0+1.0/4.0+......?

?1.0-1.0/2.0+1.0/3.0-1.0/4.0+......?

?編寫一個程序來計算這兩個序列不斷變化的總和,直到達到 某個次數。讓用戶交互的輸入這個次數。看看在20次、100次和500次之后 的總和。是否每個序列都看上去要收斂于某個值?提示:奇數個-1的乘積為-1,而偶數個-1的乘積為1。?

?#include

?

int main(void)

{

?double i,sum1,sum2;

?int sign,count;

?printf("Enter the count:");

?scanf("%d",&count);

?for(i=1.0,sign=1,sum1=0,sum2=0;i<=count;i++,sign *= -1)

?{

? sum1 += 1.0 / i;

? sum2 += sign * 1.0 / i;? ? ? //利用sign的變化規律,使運算符在正負間交替變化

?}

?printf("1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 ...(%d terms) = %lf\n",count,sum1);

?printf("1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 ...(%d terms) = %lf\n",count,sum2);

?printf("%d\n",sign);

?return(0);

}?

? 12、編寫一個程序,創建一個8個元素的int數組,并且把元素分別設置為2的前8次冪,然后打印出它們的值。使用for循環來設置值;為了變化,使用do while來顯示這些值。?

?#include

#include

int main(void)

{

? ? int num[8];

? ? int i;

? ? for (i=0;i<8;i++)

? ? {

? ? ? ? num[i]=pow(2,i);? //POW乘冪函數,利用math.h頭文件

? ? }

? ? i=0;? ? ?//使用do while時需將i初始為0;

? ? do

? ? ? ? printf("%d\t",num[i]);

? ? while(++i<8);? //退出條件為++i<8

? ? return 0;

}

/*do while 至少為執行一次,檢測退出條件是否滿足,滿足時返回再次執行,直到while的值為flase*/?

?13、編寫一個程序,創建兩個8元素的double數組,使用一個循環來讓用戶鍵入第一個數組的8個元素的值。程序把第二個數組的元素設置為第一個數組的元素的累積和。例如,第二個數組的第4個元素應該等于第一個數組 的前4個元素的和,第二個數組的第5個元素應該等于第一個數組的前5個元素的和(使用嵌套循環可以做到這一點。不過利用第二個數組的第5個元素等于第二個數組的第4個元素加上第一個數組的第5個元素這個事實,可以避免嵌套,而使用單個循環來完成這個任務)。最后,使用一個循環來顯示這兩個數組的內容,第一個數組在一行中顯示 ,而第二個數組的每個元素在第一個數組的對應元素之下進行顯示。?

?#include

int main(void)

{

? ? double num[8],sum[8];

? ? int i,j;

? ? printf("Please enter 8 numbers:");

? ? for(i=0;i<8;i++)? ? //在一個循環中為兩個數組賦值

? ? {

? ? ? ? ?scanf("%lf",&num[i]);

? ? ? ? ?for(j=0,sum[i]=0;j<=i;j++)? //從數組元素等于主數組元素的累加和;

? ? ? ? ? ? sum[i] += num[j];? /*注意此處的公式,sum數組的第n個元素等于第n-1個元素加num數組的第n元素的值*/

? ? }

? ? for(i=0;i<8;i++)

? ? ? ? printf("%8.3lf",num[i]);

? ? printf("\n");

? ? for(i=0;i<8;i++)

? ? ? ? printf("%8.3lf",sum[i]);

? ? printf("\n");

? ? return 0;

}

?

?14、編寫一個程序讀入一行輸入,然后反向打印該行。您可以把輸入存儲在一個char數組中;假定該行不超過255個字符。回憶一下,您可以使用具有%c說明符的scanf()從輸入中一次讀入一個字符 ,而且當您按下回車鍵時會產生換行符(\n).?

?#include<stdio.h>

#define LENGTH 20

int main(void)

{

? ? char a[255];

? ? int i;

? ? for(i=0;i<LENGTH;i++)

? ? ? ? scanf("%c",&a[i]);

? ? for(i=LENGTH-1;i>=0;i--)? //注意:i=LENGTH-1,而不等于LENGTH

? ? ? ? printf("%c",a[i]);

? ? printf("\n");

? ? return 0;

}

?

?15、Daphne以10%的單利息投資了100美元(也就是說,每年投資贏得的利息等于原始投資的10%)。Deirde則以每年5%的復合利息投資了100美元(也就是說,利息是當前結余的5%,其中包括以前的利息)。編寫一個程序,計算需要多少年Deirdre 的投資才會超過Daphne,并且顯示出到那里兩個人的投資額。?

?#include<stdio.h>

int main(void)

{

? ? double Daphne=100,Deirdre=100;

? ? int i=0;? ? ? ? ? ? ? ? ? ? ? ? ?//不要忘記變量的初始化

? ? while(Daphne>=Deirdre)

? ? ? ? {

? ? ? ? ? ? Daphne+=100*0.1;

? ? ? ? ? ? Deirdre+=Deirdre*0.5;

? ? ? ? ? ? i++;

? ? ? ? }

? ? printf("After %d years,Deirdre's investment is %f ,Daphne's investment is %f \n",

? ? ? ? ? ?i,Deirdre,Daphne);

? ? return 0;

?

}

?

?16、Chuckie Luchy贏了100萬美元,他把它存入每年贏得8%的帳戶。在每年的最后一天,Chuckie取出10萬美元。編寫一個程序,計算需要多少年Chuckie就會清空他的帳戶。?

?#include<stdio.h>

?

int main(void)

{

?double investment = 100;

?int i=0;

?while(investment > 0)

?{

? investment += investment * 0.08;

? investment -= 10;

? i++;

?}

?printf("After %d years,Chuckie Lucky's investment is out\n",i);

?return(0);

}

?

? ?

??

? ?

? ??

? ?

??

?

轉載于:https://my.oschina.net/idreamo/blog/689759

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540493.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540493.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540493.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C# String和string的區別

C#中同時存在String與string MSDN中對string的說明&#xff1a; string is an alias for String in the .NET Framework。string是String的別名而已&#xff0c;string是c#中的類&#xff0c;String是Framework的類&#xff0c;C# string 映射為 Framework的 String。如果用str…

要求用戶在Python中輸入整數| 限制用戶僅輸入整數值

input() function can be used for the input, but it reads the value as a string, then we can use the int() function to convert string value to an integer. input()函數可用于輸入&#xff0c;但它將值讀取為字符串&#xff0c;然后可以使用int()函數將字符串值轉換為…

[轉載] python——if語句、邏輯運算符號

參考鏈接&#xff1a; 用Python鏈接比較運算符 1.if條件判斷語句&#xff1a; if 要判斷的條件(True): 條件成立的時候&#xff0c;要做的事情 elif 要判斷的條件(True): .... elif 要判斷的條件(True): .... else: 條件不成立的時候要做的事情 示例&#xff1a; 判斷學生…

洛谷 P2689 東南西北【模擬/搜索】

題目描述 給出起點和終點的坐標及接下來T個時刻的風向(東南西北)&#xff0c;每次可以選擇順風偏移1個單位或者停在原地。求到達終點的最少時間。 如果無法偏移至終點&#xff0c;輸出“-1”。 輸入輸出格式 輸入格式&#xff1a; 第一行兩個正整數x1,y1&#xff0c;表示小明所…

單鏈表遍歷_單鏈表及其遍歷實現的基本操作

單鏈表遍歷單鏈表 (Single linked list) Single linked list contains a number of nodes where each node has a data field and a pointer to next node. The link of the last node is to NULL, indicates end of list. 單個鏈表包含許多節點&#xff0c;其中每個節點都有一…

[轉載] python中for語句用法_詳解Python中for循環的使用_python

參考鏈接&#xff1a; 在Python中將else條件語句與for循環一起使用 這篇文章主要介紹了Python中for循環的使用,來自于IBM官方網站技術文檔,需要的朋友可以參考下 for 循環 本系列前面 “探索 Python&#xff0c;第 5 部分&#xff1a;用 Python 編程” 一文討論了 if 語句和…

windows 軟鏈接的建立及刪除

在windows服務器上有時有這樣的需求&#xff0c;你的文件在f:\test中&#xff0c;但由于其它原因用戶訪問的是e:\test&#xff0c;如果又希望e:\test 中的文件與f:\test的保持同步&#xff0c;除了用同步軟件來做外&#xff0c;可以用windows 的文件夾映射來做 cmd: mklink /J …

8086簡單的指令流水線_在8086微處理器中執行流水線的指令和概念的步驟

8086簡單的指令流水線Any computer or machine works according to some instructions. These instructions are responsible for all the work that the machine does. But how does a machine work to understand and execute that instruction? 任何計算機或機器都按照某些…

[轉載] 使用Python編寫打字訓練小程序

參考鏈接&#xff1a; 在Python中切換大小寫(替換) 你眼中的程序猿 別人眼中的程序猿&#xff0c;是什么樣子&#xff1f;打字如飛&#xff0c;各種炫酷的頁面切換&#xff0c;一個個好似黑客般的網站破解。可現實呢&#xff1f; 二指禪的敲鍵盤&#xff0c;寫一行代碼&#…

shell兩個數字相乘_使用8086微處理器將兩個16位數字相乘而不帶進位

shell兩個數字相乘Problem statement: 問題陳述&#xff1a; To perform multiplication operation between 2 16bit numbers with carry using 8086 Microprocessor. 使用8086微處理器在2個16位數字之間進行帶進位的乘法運算。 Algorithm: 算法&#xff1a; Load the first…

Dwr 框架簡單實例

Dwr 是一個 Java 開源庫&#xff0c;幫助你實現Ajax網站。 它可以讓你在瀏覽器中的Javascript代碼調用Web服務器上的Java&#xff0c;就像在Java代碼就在瀏覽器中一樣。 Dwr 主要包括兩部分&#xff1a; 在服務器上運行的 Servlet 來處理請求并把結果返回瀏覽器。 運行在瀏覽器…

[轉載] Python進階:設計模式之迭代器模式

參考鏈接&#xff1a; Python中的迭代器 在軟件開發領域中&#xff0c;人們經常會用到這一個概念——“設計模式”&#xff08;design pattern&#xff09;&#xff0c;它是一種針對軟件設計的共性問題而提出的解決方案。在一本圣經級的書籍《設計模式&#xff1a;可復用面向對…

JavaScript | 如何為變量分配十進制,八進制和十六進制值?

Just like C programming language, we can assign integer value in the different format to the variable. 就像C編程語言一樣 &#xff0c;我們可以將不同格式的整數值分配給變量。 Assigning decimal value: It can be assigned simply without using any prefix. 分配十…

路由器DHCP和DHCP中繼的配置

路由器 DHCP和DHCP中繼的配置 路由器作為DHCP服務器&#xff1a; 1.配置router的地址&#xff1a;Route(config)# hostname gateway (更改主機名字) Gateway(config)# interface gigabitethernet 0/0 …

[轉載] 大數據分析Python For循環教程

參考鏈接&#xff1a; Python中的迭代器函數1 大數據分析Python除了循環遍歷列表之外&#xff0c;for循環還有很多其他功能&#xff0c;在現實世界的數據科學工作中&#xff0c;您可能需要將numpy數組和pandas DataFrames用于其他數據結構的循環。 大數據分析Python For循環教…

node.js 爬蟲入門總結

node.js爬蟲 前端同學可能向來對爬蟲不是很感冒&#xff0c;覺得爬蟲需要用偏后端的語言&#xff0c;諸如 php &#xff0c; python 等。當然這是在 nodejs 前了&#xff0c;nodejs 的出現&#xff0c;使得 Javascript 也可以用來寫爬蟲了。由于 nodejs 強大的異步特性&#xf…

數組重復次數最多的元素遞歸_使用遞歸計算鏈接列表中元素的出現次數

數組重復次數最多的元素遞歸Solution: 解&#xff1a; Required function: 所需功能&#xff1a; func_occurence ( node *temp) //recursive functionInput: 輸入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head and…

SecureCRT中文亂碼解決方法

服務端export LANGzh_CN.UTF-8客戶端SecureCRT編碼選擇UTF-8客戶端SecureCRT字體選擇新宋體&#xff0c;字符集選擇中文總結&#xff1a;客戶端和服務端字符編碼一致&#xff0c;客戶端字體字符集支持轉載于:https://blog.51cto.com/leomars/1972669

[轉載] Python 迭代器 深入理解 與應用示例

參考鏈接&#xff1a; Python | 可迭代和迭代器之間的區別 本篇文章簡單談談可迭代對象&#xff0c;迭代器和生成器之間的關系。 三者簡要關系圖 可迭代對象與迭代器 剛開始我認為這兩者是等同的&#xff0c;但后來發現并不是這樣&#xff1b;下面直接拋出結論&#xff1a; 1…

Python程序查找表示O(1)復雜度的數字所需的位數

Problem statement 問題陳述 Find total Number of bits required to represent a number in binary 查找以二進制表示數字所需的總位數 Example 1: 范例1&#xff1a; input : 10output: 4Example 2: 范例2&#xff1a; input : 32output : 6Formula used: 使用的公式&am…