C 经典 习 例题初学者必看

发布 2019-08-11 20:47:55 阅读 4453

1:1~100的偶数累加,奇数相乘

一。请用三种结构实现从1到100之间的偶数累加,奇数相乘,并将偶数累加的结果输出,奇数相乘的结果输出。

1)用for语句完成。

#include

using namespace std;

int main()

int sum=0;

double pro=1;

for(int i=2;i<=100;i=i+2)

sum=sum+i;

pro=pro*(i-1);

cout<<"1到100之间的偶数和为:"

2)用while语句完成。

#include

using namespace std;

int main()

int sum=0;

double pro=1;

int i=2;

while(i<=100)

cout<<"偶数和为:"

3)用do-while语句完成。

#include

using namespace std;

int main()

int sum=0;

double pro=1;

int i=2;

dowhile(i<=100);

cout<<"偶数和为:"

4)以下是我自已用for语句做的,还要修改的。

#include

using namespace std;

int main()

int i,j,n,h;

for(n=1000;n<=9999;n++)

for(h=1;h<=100;h++)

return 0;

2:aabb类的车牌号。

1.一辆卡车违犯交通规则,撞人后逃跑。现场有三人目击事件,但都没有记住车号,只记下车号的一些特征。

甲说:牌照的前两位数字是相同的;乙说:牌照的后两位数字是相同的,但与前两位不同;丙是位数学家,他说:

四位的车号刚好是一个整数的平方。请根据以上线索求出车号。

#include<>

void main()

int a,b,c,d,n;

for(int i=0;i<100;i++)

3:颠倒顺序问题。

一。编写字符串反转函数 mystrrev(),该函数的功能为将指定字符串的字符顺序颠倒排列。然后,再编写主函数验证之,函数声明如下:

void mystrrev(char string)

方法:(1)#include

using namespace std;

#include

void mystrrev(char string)

int x=strlen(string);

int i=0;

while(i

int main()

char string[7]="abcdef";

cout<<"原来的排序是:"

cout<<"反转后的排序是:"

方法:(2)#include

void mystrrev(char string)

int x=strlen(string);

int i=0;

while(i

int main()

cout<<"please input "

cout<<"反转后的排序是:"

4:各种符号=%的使用。

一。各种符号=,*的使用。

#include<>

void main()

int a=20, b=6;

a+=a; cout< a=20; a-=a; cout< a=20; a*=a; cout< a=20; a/=b; cout

a+=a*=a-=a/b; cout<}

5:计算单词的个数。

2. 输入一行字符,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。

输入输出示例。

input words: the sum of the odd numbers

count = 6

#include

using namespace std;

void main()

char a,b;

int c=0;

cout<<"请输入一行单词:"

if(b!=32)

c++;cout<<"单词数为"<}

6:计算各种数符符号的个数。

2)编写程序,不断随机地从键盘输入字符,直到输入字符q或q时退出,分别统计输出一共有多少个数字,多少个小写字母,多少个大写字母及多少个除上述以外的其他字符的个数。(注意:这里最后一个q或q也要计算在内。

#include

using namespace std;

int main()

char ch;

int a=0,b=0,c=0,d=0;

cout<<"请输入你想输入的任意字符"

cin>>ch;

if(ch>='0'&&ch<='9')

a=a+1;

else if(ch>='a'&&ch<='z')

b=b+1;

else if(ch>='a'&&ch<='z')

c=c+1;

elsed=d+1;

while(ch!='q'&&ch!='q');

cout<<"一共输入的数字个数为"

8:计算字符个数。

#include

using namespace std;

int main()

char ch;

int a=0,b=0,c=0,d=0;

cout<<"请输入你想输入的任意字符"

cin>>ch;

if(ch>='0'&&ch<='9')

a=a+1;

else if(ch>='a'&&ch<='z')

b=b+1;

else if(ch>='a'&&ch<='z')

c=c+1;

elsed=d+1;

while(ch!='q'&&ch!='q');

cout<<"一共输入的数字个数为"

9:求三个数的平均数。

3)编写一个程序,要求:提示输入3个数;显示这3个数,求他们的平均值并输出。

#include

using namespace std;

int main ()

double a,b,c,d;

cout<<"pleace input three numbers:";

cin>>a>>b>>c;

cout<<"a="

cout<<"d="

10:求身高有关的问题。

1.身高**。