第六周作业1(例题照搬)

例4.1

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
	int a[10];
	int i;
	for(i=0;i<10;i++)
		a[i]=i*2+2;
	for(i=0;i<10;i++)
	{
		cout<<a[i]<<'\t';
		if((i+1)%5==0)
			cout<<endl;
	}
	
	return 0;
}



例4.2

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    int i,math[40],n;
	float aver=0.0;
	int unpasseducount=0;
	int highscorecount=0;
	cout<<"请输入学生人数:";
	cin>>n;
	cout<<"请输入成绩:";
	for(i=0;i<n;i++)
	{
		cin>>math[i];
		aver+=math[i];
	}
	aver/=n;
	for(i=0;i<n;i++)
	{
		if(math[i]<60) unpasseducount++;
		if(math[i]>=90) highscorecount++;
	}
	cout<<"平均分为:"<<aver<<endl;
	cout<<"90分以上人数为:"<< highscorecount<<endl;
	cout<<"不及格人数为:"<<unpasseducount<<endl;
	
	return 0;
}



4.3

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
	int a[10],i,big;
	cout<<"please input 10 numbers:\n";
	for(i=0;i<10;i++)
		cin>>a[i];
	cout<<"the numbers are:";
	for(i=0;i<10;i++)
		cout<<setw(4)<<a[i];
	cout<<endl;
    big=a[0];
	for(i=1;i<10;i++)
		if(a[i]>big)
			big=a[i];
	cout<<"the big number is:"<<big<<endl;
	
	return 0;
}



4.4

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    int a[10];
	int i,j,t;
	cout<<"please input 10 numbers:\n";
	for(i=0;i<10;i++)
		cin>>a[i];
	cout<<"the numbers are:";
	for(i=0;i<10;i++)
		cout<<setw(4)<<a[i];
	cout<<endl;
	for(i=0;i<9;i++)
	for(j=0;j<9-i;j++)
	if(a[j]>a[j+1])
	{
		t=a[j];
	    a[j]=a[j+1];
    	a[j+1]=t;
	}
	cout<<"the sorted numbers are:";
	for(i=0;i<10;i++)
		cout<<setw(4)<<a[i];
	cout<<endl;
	
	return 0;
}


4.5

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    int i;
	int f[40]={1,1};
	for(i=2;i<40;i++)
		f[i]=f[i-2]+f[i-1];
	for(i=0;i<40;i++)
	{
		if(i%4==0)
			cout<<endl;
		cout<<setw(12)<<f[i];
	}
	cout<<endl;
	
	return 0;
}



4.6

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    int i,j;
	int a[5][5];
	for(i=0;i<5;i++)
	{
		for(j=0;j<5;j++)
		{
			if(i%2==0)
				a[i][j]=i*5+j+1;
			else
				a[i][4-j]=i*5+j+1;
		}
	}
	for(i=0;i<5;i++)
	{
		for(j=0;j<5;j++)
			cout<<setw(4)<<a[i][j];
		cout<<endl;
	}

	return 0;
}


4.8

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
        char str[50];
	cout<<"please input strings:";
	cin.get(str,50);
	cout<<"the string is:";
	cout<<str<<endl;

	return 0;
}


4.9

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;

int main()
{
    char str[100];
	cout<<"请输入一个字符串:";
	cin.get(str,100);
	cout<<"字符串"<<str<<"的反向字符串为:";
	for(int i=strlen(str)-1;i>=0;i--)
		cout<<str[i];
	cout<<endl;

	return 0;
}


4.10

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    char s[]="this is C programming test.";
	int i=0,pLen=0,maxLen=0,pSeat=0;
	while(s[i]!='\0')
	{
		while(s[i]!=' '&&s[i]!='\0')
	{
		pLen++;
		i++;
	}
	if(pLen>maxLen)
	{
		pSeat=i-pLen;
		maxLen=pLen;
	}
	while(s[i]==' ')
	    i++;
	pLen=0;
}
	cout<<"最长的单词为:";
	for(i=0;i<maxLen;i++)
		cout<<s[pSeat+i];
	cout<<endl;
    

	return 0;
}


4.11

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;

int main()
{
    char str[50];
	cout<<"Please input a string:";
	cin.get(str,50);
	cout<<"The length of string"<<str<<" is "<<strlen(str)<<endl;
    

	return 0;
}


4.12

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;

int main()
{
    char str[10];
	cout<<"请输入字符串,直到输入hello后结束程序:"<<endl;
	do{
		cin>>str;
	}
	while(strcmp(str,"hello")!=0);
    

	return 0;
}


4.13

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;

int main()
{
    char str[50];
	int len=0;
	cout<<"请输入一个字符串:";
	cin.get(str,50);
	while(str[len]!='\0')
		len++;
	cout<<"字符串"<<str<<"的长度为:"<<len<<endl;
    

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值