鹏哥手把手带我刷好题 · 编程练习 · II
大家好,我是安然無虞。
目錄
1.判斷字母
2.字符圣誕樹
3.ASCII碼
4.出生日期的輸入輸出
5.2的n次方計算
6.按照格式輸入并交換輸出
7.字符轉ASCII碼
8.計算表達式的值
9.計算帶余除法
10.計算體重指數
11.計算三角形的周長和面積
12.計算球體的體積
結語:遇見安然遇見你,不負代碼不負卿!
【前言】
題目比較簡單,130來題,二刷了,因為之前寫的很多解法太繁瑣,所以用幾天的時間重新過一遍。?
1.判斷字母
原題鏈接:判斷字母_牛客題霸_牛客網
題目描述:
代碼執(zhí)行:
代碼1:
#include<stdio.h> int main() {int ch = 0;//多組輸入while((ch=getchar())!=EOF){if(ch>='A' && ch<='Z' || ch>='a' && ch<='z'){printf("YES\n");}else{printf("NO\n");}getchar();//一定要記得清除緩沖區(qū)}return 0; }代碼2:
//isplpha - 是專門用來判斷一個字符是不是字母的庫函數 //是字母則返回非0的值,不是字母則返回0 //需要引頭文件#include<ctype.h> #include<stdio.h> #include<ctype.h>int main() {int ch = 0;//多組輸入while((ch = getchar())!=EOF){//判斷并輸出if(isalpha(ch)){printf("YES\n");}else{printf("NO\n");}getchar();//清理緩沖區(qū)\n}return 0; }2.字符圣誕樹
原題鏈接:字符圣誕樹_牛客題霸_牛客網
題目描述:
示例:
輸入:1 輸出: 11 11 1 11 1 1 1 1 1 1 1 1代碼執(zhí)行:
#include<stdio.h>int main() {char ch = 0;ch = getchar();int i = 0;//每循環(huán)一次,打印一行//每一行由兩部分組成:空格和字符for (i = 0; i < 5; i++){//打印空格int j = 0;for (j = 0; j < 5 - 1 - i; j++){printf(" ");}//打印字符for (j = 0; j <= i; j++){printf("%c ", ch);}printf("\n");}return 0; }3.ASCII碼
原題鏈接:ASCII碼_牛客題霸_牛客網
題目描述:
代碼執(zhí)行:
#include<stdio.h> int main() {char arr[] = {73,32,99,97,110,32,100,111,32,105,116,33};//arr是一個數組,數組是使用下標來訪問的//計算數組元素多少int sz = sizeof(arr) / sizeof(arr[0]);int i = 0;for(i = 0; i < sz; i++){printf("%c", arr[i]);}return 0; }4.出生日期的輸入輸出
原題鏈接:出生日期輸入輸出_牛客題霸_牛客網
【敲黑板】:本題需要注意輸入輸出格式。?
題目描述:
示例:
輸入:20130225 輸出: year=2013 month=02 date=25注意:?
通過scanf函數的%m格式控制可以指定輸入域寬,輸入數據域寬(列數),按此寬度截取所需數據;通過printf函數的%0格式控制符,輸出數值時指定左面不使用的空位置自動填0。
代碼執(zhí)行:
#include<stdio.h>int main() {int year = 0;int month = 0;int date = 0;//按照格式輸入scanf("%4d%2d%2d", &year, &month, &date);//輸出printf("year=%4d\n", year);printf("month=%02d\n", month);printf("date=%02d\n", date);return 0; }5.2的n次方計算
原題鏈接:2的n次方計算_牛客題霸_牛客網
題目描述:
?大家想深入學習的話可以看看我這篇文章:
藍橋杯算法競賽系列第一章——位運算的奇巧淫技及其實戰(zhàn)_安然無虞的博客-CSDN博客【聲明】:在接下來的兩個月中,博主持續(xù)推出兩個系列的博文,有關零基礎搞定C語言,藍橋杯算法競賽,歡迎讀者發(fā)表自己的想法,期待您的留言評論。https://blog.csdn.net/weixin_57544072/article/details/120798996
代碼執(zhí)行:
#include<stdio.h>int main() {int n = 0;//多組輸入while(scanf("%d", &n)!=EOF){printf("%d\n", 1<<n);}return 0; }6.按照格式輸入并交換輸出
原題鏈接:按照格式輸入并交換輸出_牛客題霸_牛客網
題目描述:
示例:
輸入:a=1,b=2 輸出:a=2,b=1代碼執(zhí)行:
#include<stdio.h>int main() {int a = 0;int b = 0;int c = 0;scanf("a=%d,b=%d", &a, &b);//交換//想象成一瓶醬油和一瓶醋,再拿一個空瓶進行交換c = a;a = b;b = c;//輸出printf("a=%d,b=%d\n", a, b);return 0; }補充一道變態(tài)的筆試題:
不能創(chuàng)建臨時變量(第三個變量),實現兩個數的交換:
方法一:加減法
a = a + b;
b = a - b;
a = a - b;
這樣就解決了,但是有不好的地方,如果a, b 很大,就可能會導致溢出,所以還有一種方法,就是利用異或法
方法二:異或法
a = a ^ b;
b = a ^ b;
a = a ^ b;
這樣就交換完成了,但是在實際運用當中,習慣定義第三個變量來實現兩個數的交換,因為鴨,上面的代碼可讀性都不高!?
7.字符轉ASCII碼
原題鏈接:字符轉ASCII碼_牛客題霸_牛客網?
題目描述:
示例:
輸入:a 輸出:97代碼執(zhí)行:
#include<stdio.h> int main() {char ch = 0;ch = getchar();printf("%d\n",ch);return 0; }8.計算表達式的值
原題鏈接:計算表達式的值_牛客題霸_牛客網
題目描述:
代碼執(zhí)行:
#include<stdio.h> int main() {int a = 40;int c = 212;printf("%d\n", (-8+22)*a - 10 + c / 2);return 0; }9.計算帶余除法
原題鏈接:計算帶余除法_牛客題霸_牛客網
題目描述:
示例:
輸入:15 2 輸出:7 1代碼執(zhí)行:
#include<stdio.h>int main() {int a = 0;int b = 0;scanf("%d %d", &a, &b);// / 除法操作符 得到的是商// % 取余(取模)操作符,得到的是余數int c = a/ b;int d = a % b;printf("%d %d\n", c, d);return 0; }10.計算體重指數
原題鏈接:計算體重指數_牛客題霸_牛客網
題目描述:
示例:
輸入:70 170 輸出:24.22代碼執(zhí)行:
#include<stdio.h> int main() {int weight = 0;int height = 0;double bmi = 0.0;//輸入scanf("%d %d", &weight, &height);//計算BMI//之所以除以100.0,是因為除號兩邊都是整型,要得到小數,必須保證兩邊有一個是浮點數bmi = weight / ((height / 100.0) * (height / 100.0));//輸出printf("%.2lf\n", bmi);return 0; }11.計算三角形的周長和面積
原題鏈接:計算三角形的周長和面積_牛客題霸_牛客網
題目描述:
示例:
輸入:3 3 3 輸出:circumference=9.00 area=3.90注意:給出三角形的三條邊讓我們計算三角形面積,那么我們需要運用海倫公式,題目就迎刃而解了。?
代碼執(zhí)行:
#include<stdio.h> #include<math.h>int main() {double a = 0.0;double b = 0.0;double c = 0.0;double circumference = 0.0;//周長double area = 0.0;//面積//輸入scanf("%lf %lf %lf", &a,&b,&c);//計算circumference = a + b + c;double p = (a + b + c) / 2;area = sqrt(p * (p - a) * (p - b) * (p - c));printf("circumference=%.2lf area=%.2lf\n",circumference, area);return 0; }12.計算球體的體積
原題鏈接:計算球體的體積_牛客題霸_牛客網
題目描述:
代碼執(zhí)行:
#include<stdio.h> #include<math.h> #include<math.h> #define PI 3.1415926 int main() {double r = 0.0;double v = 0.0;//輸入scanf("%lf", &r);//計算v = 4.0 / 3 * PI * pow(r, 3);//輸出printf("%.3lf\n", v);return 0; }結語:遇見安然遇見你,不負代碼不負卿!
求求了,來個三連吧。總結
以上是生活随笔為你收集整理的鹏哥手把手带我刷好题 · 编程练习 · II的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: visual studio2017登录时
- 下一篇: 一键修改手机DNS的bat文件