2014年(2)
题目
完成下列简单程序。
代码
1、输出100以内的能被三整除,且个位数为6的数
#include<iostream>
using namespace std;
int main(){
int i,j;
for(i=0;i<10;i++){
j=i*10+6;
if(j%3!=0) continue;
cout<<j<<" ";
}
}
2、f(x)=x*x+1,用函数求f(0)~f(10)的和
#include<iostream>
using namespace std;
int f(int );
int main(){
int SumFun(int );
cout<<"sum="<<SumFun(10)<<endl;
}
int SumFun(int n){
int x,s=0;
for(x=0;x<=n;x++)
s+=f(x);
return s;
}
int f(int x){
cout<<x*x+1<<endl;
return x*x+1;
}
3、链表,简单输入输出
#include<iostream>
#include<stdlib.h>
using namespace std;
#define N 3
int main(){
struct node{
char name[9];
int salary;
struct node *next;
};
struct node *head,*p;
int i;
head=NULL;
for(i=0;i<N;i++){
p=(struct node*)malloc(sizeof(struct node));
cout<<"please input name:"<<endl;
cin>>p->name;
cout<<"plese input salary:"<<endl;
cin>>p->salary;
p->next=head;
head=p;
}
cout<<endl;
p=head;
while(p!=NULL){
cout<<p->name<<" "<<p->salary<<endl;
p=p->next;
}
}