品牌网站建设預定大蝌蚪,同一个服务器可以做多个网站,诸城 网站 建设,wordpress在线充值今天找了20道c的经典题型#xff0c;看这一篇就够了#xff0c;全是干货 目录
1、题目#xff1a;有一对兔子#xff0c;从出生后第3个月起每个月都生一对兔子#xff0c;小兔子长到第三个月后每个月又生一对兔子#xff0c;假如兔子都不死#xff0c;问每个月的兔子总…今天找了20道c的经典题型看这一篇就够了全是干货 目录
1、题目有一对兔子从出生后第3个月起每个月都生一对兔子小兔子长到第三个月后每个月又生一对兔子假如兔子都不死问每个月的兔子总数为多少 2、题目判断101-200之间有多少个素数并输出所有素数。 3、题目打印出所有的“水仙花数”所谓“水仙花数”是指一个三位数其各位数字立方和等于该数本身。例如153是一个“水仙花数”因为1531的三次方5的三次方3的三次方。 4、题目将一个正整数分解质因数。例如输入90,打印出902*3*3*5。
5、题目利用条件运算符的嵌套来完成此题学习成绩90分的同学用A表示60-89分之间的用B表示60分以下的用C表示。
6、题目输入两个正整数m和n求其最大公约数和最小公倍数。
7、题目输入一行字符分别统计出其中英文字母、空格、数字和其它字符的个数。
8、题目求saaaaaaaaaaaa...a的值其中a是一个数字。例如222222222222222(此时共有5个数相加)几个数相加有键盘控制。
9、题目一个数如果恰好等于它的因子之和这个数就称为“完数”。例如6123.编程 找出1000以内的所有完数。
10、题目一球从100米高度自由落下每次落地后反跳回原高度的一半再落下求它在第10次落地时共经过多少米第10次反弹多高
11、题目有1、2、3、4个数字能组成多少个互不相同且无重复数字的三位数都是多少
14、题目输入某年某月某日判断这一天是这一年的第几天
15、题目输入三个整数x,y,z请把这三个数由小到大输出。
16、题目用*号输出字母C的图案。
17、题目输出9*9口诀。
18、题目要求输出国际象棋棋盘。
19、题目猴子吃桃问题猴子第一天摘下若干个桃子当即吃了一半还不瘾又多吃了一个第二天早上又将剩下的桃子吃掉一半又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时见只剩下一个桃子了。求第一天共摘了多少。
20、题目两个乒乓球队进行比赛各出三人。甲队为a,b,c三人乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比c说他不和x,z比请编程序找出三队赛手的名单。 1、题目有一对兔子从出生后第3个月起每个月都生一对兔子小兔子长到第三个月后每个月又生一对兔子假如兔子都不死问每个月的兔子总数为多少
1.程序分析 兔子的规律为数列1,1,2,3,5,8,13,21....
2.程序源代码
#includeiostream
using namespace std;
int main()
{long f1;long f2;f1 f2 1;for (int i 1; i 20; i){cout \tf1 \t f2;if (i % 2 0){cout \n ;}f1 f2 f1;f2 f1 f2;}getchar();return 0;
}
结果 2、题目判断101-200之间有多少个素数并输出所有素数。
1.程序分析判断素数的方法用一个数分别去除2到sqrt(这个数)如果能被整除则表明此数不是素数反之是素数。
2、源代码
#includeiostream
#includemath.h
using namespace std;
int main()
{bool flag true;int count 0;cout 素数 endl;for (int i 101; i 200; i){for (int j2; j sqrt(i); j){if (i % j 0){flag false;break;}}if (flag){count;if (count % 4 0){cout \n;}cout \t i \t;}flag true;}cout \n 总数为: count;getchar();return 0;
}
结果 3、题目打印出所有的“水仙花数”所谓“水仙花数”是指一个三位数其各位数字立方和等于该数本身。例如153是一个“水仙花数”因为1531的三次方5的三次方3的三次方。
1.程序分析利用for循环控制100-999个数每个数分解出个位十位百位。
2.程序源代码
#includeiostream
#includemath.h
using namespace std;
int main()
{int a, b, c;int count 0;cout 水仙花数为 endl;for (int i 100; i 999; i){a i / 100;b i / 10 % 10;c i % 10;if (i a * a * a b * b * b c * c * c){count;if (count % 5 0){cout \n;}cout \t i \t;}}cout 总数为 count;getchar();return 0;
}
结果 4、题目将一个正整数分解质因数。例如输入90,打印出902*3*3*5。
程序分析对n进行分解质因数应先找到一个最小的质数k然后按下述步骤完成
(1)如果这个质数恰等于n则说明分解质因数的过程已经结束打印出即可。
(2)如果nk但n能被k整除则应打印出k的值并用n除以k的商,作为新的正整数你n,重复执行第一步。
(3)如果n不能被k整除则用k1作为k的值,重复执行第一步。
2.程序源代码
#includeiostream
#includemath.h
using namespace std;
int main()
{int a;cout 请输入一个正整数 endl;cin a;cout a ;for (int i 2; i a; i){while (i ! a){if (a % i 0){cout i *;a a / i;}else{break;}}}cout a;getchar();return 0;
} 结果 5、题目利用条件运算符的嵌套来完成此题学习成绩90分的同学用A表示60-89分之间的用B表示60分以下的用C表示。
1.程序分析(ab)?a:b这是条件运算符的基本例子。
2.程序源代码
#includeiostream
#includemath.h
using namespace std;
int main()
{int score;char grade;cout 请输入学生的成绩 endl;cin score;grade score 90 ? A : (score 60 ? C : B);cout 学生的成绩等级为 grade endl;system(pause);return 0;
}
结果 6、题目输入两个正整数m和n求其最大公约数和最小公倍数。
1.程序分析利用辗除法。
2.程序源代码
#includeiostream
#includemath.h
using namespace std;
int main()
{int m, n,a,b,tmp;cout 请输入两个正整数 endl;cin m n;if (m n){tmp m;m n;n tmp;}a m;b n;while (b ! 0){tmp a % b;a b;b tmp;}cout 最大公约数为 a endl;cout 最小公倍数为 m * n / a endl;system(pause);return 0;
}
结果 7、题目输入一行字符分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析利用while语句,条件为输入的字符不为\n.
2.程序源代码
#includeiostream
#includemath.h
#includestring.h
using namespace std;
int main()
{char a;int num0, cha0, oth0,spa0;cout 请输入一行字符。 endl;while ((agetchar()) ! \n){if (a 0 a 9){num;}else if ((a a a z) || (a A a Z)){cha;}else if(a ){spa;}else{oth;}}cout 数字个数 num 字母个数 cha 空格个数为spa 其他字符个数 oth endl;system(pause);return 0;
}
结果 8、题目求saaaaaaaaaaaa...a的值其中a是一个数字。例如222222222222222(此时共有5个数相加)几个数相加有键盘控制。
1.程序分析关键是计算出每一项的值。
2.程序源代码
#includeiostream
#includemath.h
#includestring.h
using namespace std;
int main()
{int n,a,b;int j 0;cout 请输入相加个数n和加数a endl;cin na;b a;cout s;for (int i 0; i n; i){cout a ;if (j n){cout *;}a b a * 10;}system(pause);return 0;
}
结果 9、题目一个数如果恰好等于它的因子之和这个数就称为“完数”。例如6123.编程 找出1000以内的所有完数。
1. 程序分析请参照题目4.
2.程序源代码
#includeiostream
#includemath.h
#includestring.h
using namespace std;
int main()
{int k[10];int n, s, i, m;for (n 2; n 1000; n) {i -1;s n;for (m 1; m n; m) {if (n%m 0) {i;s s - m;k[i] m;}}if (s 0) {printf(%d is a wanshu\n, n);}}system(pause);return 0;
}
结果 10、题目一球从100米高度自由落下每次落地后反跳回原高度的一半再落下求它在第10次落地时共经过多少米第10次反弹多高
1.程序分析见下面注释
2.程序源代码
#includeiostream
#includemath.h
#includestring.h
using namespace std;
int main()
{double s 100;double hs/2;double count 100;for (int i 1; i 10; i){count h count;h h / 2;}cout 第10次落地时共经过 count endl;cout 第10次反弹: h endl;system(pause);return 0;
}
结果 11、题目有1、2、3、4个数字能组成多少个互不相同且无重复数字的三位数都是多少
1.程序分析可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。
2.程序源代码
#includeiostream
using namespace std;
int main()
{int number[] { 1,2,3,4 };int count 0;cout 能够组成的三位数有\n;for (int i 0; i sizeof(number) / sizeof(int); i){for (int j 0; j sizeof(number) / sizeof(int); j){for (int k 0; k sizeof(number) / sizeof(int); k){if (i ! j i ! k j!k){cout number[i] * 100 number[j] * 10 number[k] \t;count;}}cout \n;}cout \n;}cout \n;cout 一共有 count endl;;system(pause);return 0;
}
结果 14、题目输入某年某月某日判断这一天是这一年的第几天
1.程序分析以3月5日为例应该先把前两个月的加起来然后再加上5天即本年的第几天特殊情况闰年且输入月份大于3时需考虑多加一天。
公历闰年的简单计算方法符合以下条件之一的年份即为闰年
1.能被4整除而不能被100整除。
2.能被100整除也能被400整除。
1696至2032年中为闰年的有
1696年 1704年 1708年 1712年 1716年 1720年 1724年 1728年 1732年 1736年 1740年1744年 1748年 1752年 1756年 1760年 1764年 1768年 1772年 1776年 1780年 1784年1788年 1792年
1798年 1804年 1808年 1812年 1816年 1820年 1824年 1828年 1832年 1836年,1840年, 1844年 1848年 1852年 1856年 1860年 1864年 1868年 1872年 1876年 1880年,1884年1888年 1892年
1896年 1904年 1908年 1912年 1916年 1920年 1924年 1928年 1932年 1936年 1940年1944年 1948年 1952年 1956年 1960年 1964年 1968年 1972年 1976年 1980年 1984年 1988年 1992年 1996年 2000年 2004年 2008年 2012年 2016年 2020年 2024年 2028年2032年。
除去闰年剩下的全部都是平年。
闰年二月有29天平年有28天闰年共有366天1-12月分别为31天29天31天30天31天30天31天31天30天31天30天31天
2.程序源代码 #includeiostream#includemath.h
using namespace std;
int main()
{int year,month, day;long int count_day 0;cout 请输入某年某月某日;cin year month day;if ((year % 4 0 year % 100 ! 0) || (year % 100 0 year % 400 0)){//闰年而二月29日switch (month){case 1:count_day day;break;case 2:count_day 1 * 31 day;break;case 3:count_day 1 * 31 1 * 29 day;break;case 4:count_day 2 * 31 1 * 29 day;break;case 5:count_day 2 * 31 1*301 * 29 day;break;case 6:count_day 3 * 31 1 * 30 1 * 29 day;break;case 7:count_day 3 * 31 2 * 30 1 * 29 day;break;case 8:count_day 4 * 31 2 * 30 1 * 29 day;break;case 9:count_day 5 * 31 2 * 30 1 * 29 day;break;case 10:count_day 5 * 31 3 * 30 1 * 29 day;break;case 11:count_day 6 * 31 3 * 30 1 * 29 day;break;case 12:count_day 6 * 31 4 * 29 1 * 29 day;}}else{switch (month){case 1:count_day day;break;case 2:count_day 1 * 31 day;break;case 3:count_day 1 * 31 1 * 28 day;break;case 4:count_day 2 * 31 1 * 28 day;break;case 5:count_day 2 * 31 1 * 30 1 * 28 day;break;case 6:count_day 3 * 31 1 * 30 1 * 28 day;break;case 7:count_day 3 * 31 2 * 30 1 * 28 day;break;case 8:count_day 4 * 31 2 * 30 1 * 28 day;break;case 9:count_day 5 * 31 2 * 30 1 * 28 day;break;case 10:count_day 5 * 31 3 * 30 1 * 28 day;break;case 11:count_day 6 * 31 3 * 30 1 * 28 day;break;case 12:count_day 6 * 31 4 * 29 1 * 28 day;}}cout 这一天是year - month - day 第count_day天 endl;system(pause);return 0;
} 15、题目输入三个整数x,y,z请把这三个数由小到大输出。
1.程序分析我们想办法把最小的数放到x上先将x与y进行比较如果xy则将x与y的值进行交换然后再用x与z进行比较如果xz则将x与z的值进行交换这样能使x最小。
2.程序源代码
#includeiostream
#includemath.husing namespace std;void swap(int* a, int* b);int main()
{int x, y, z;cout 请输入三个数 endl;cin x y z;if (x y){swap(x, y);}if (x z){swap(x, z);}if ( y z){swap(y, z);}cout 从小到大排序为 endl;cout x y z endl;;system(pause);return 0;
}
void swap(int* a, int* b)
{int tmp;tmp *a;*a *b;*b tmp;
} 16、题目用*号输出字母C的图案。
1.程序分析可先用*号在纸上写出字母C再分行输出。
2.程序源代码
#includeiostream
#includemath.husing namespace std;int main()
{cout ****\n;cout *\n;cout *\n;cout ****\n;system(pause);return 0;
} 17、题目输出9*9口诀。
1.程序分析分行与列考虑共9行9列i控制行j控制列。
2.程序源代码
#includeiostream
#includemath.husing namespace std;int main()
{for (int i 1; i 9; i){for (int j 1; j i; j){cout j * i j * i ;}cout \n;}system(pause);return 0;
} 18、题目要求输出国际象棋棋盘。
1.程序分析用i控制行j来控制列根据ij的和的变化来控制输出黑方格还是白方格。
2.程序源代码
#includeiostream
#includemath.husing namespace std;int main()
{int i, j;for (i 0; i 8; i) {for (j 0; j 8; j) {if ((i j) % 2 0){printf(%c%c, 219, 219);}else{printf( );}}printf(\n);}system(pause);return 0;
} 19、题目猴子吃桃问题猴子第一天摘下若干个桃子当即吃了一半还不瘾又多吃了一个第二天早上又将剩下的桃子吃掉一半又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时见只剩下一个桃子了。求第一天共摘了多少。
1.程序分析采取逆向思维的方法从后往前推断。
2.程序源代码
#includeiostream
#includemath.husing namespace std;int main()
{int day, x1, x2 1;for (day 9; day 0; day--) {x1 (x2 1) * 2; //第一天的桃子数是第2天桃子数加1后的2倍x2 x1;}cout 第一天共摘了: x1 endl;;system(pause);return 0;
} 20、题目两个乒乓球队进行比赛各出三人。甲队为a,b,c三人乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比c说他不和x,z比请编程序找出三队赛手的名单。
1.程序分析判断素数的方法用一个数分别去除2到sqrt(这个数)如果能被整除则表明此数不是素数反之是素数。
2.程序源代码
#includeiostream
using namespace std;int main()
{char i, j, k; //i是a的对手j是b的对手k是c的对手for (i x; i z; i) {for (j x; j z; j) {if (i ! j) {for (k x; k z; k){if (i ! k j ! k) {if (i ! xk ! xk ! z)printf(order is a--%c\tb--%c\tc--%c\n, i, j, k);}}}}}system(pause);return 0;
}
结果 过几天我再去找点题型给大家做拜拜~