Last active
May 18, 2018 10:08
-
-
Save ryuunoakaihitomi/1f50b449b24fe1049c020e8d934836fa to your computer and use it in GitHub Desktop.
C++作业
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int in; | |
cin>>in; | |
if(in>=1000||in<0) | |
cout<<"invalid input!"; | |
else | |
{ | |
int digit,hundred,ten,one; | |
hundred=in/100; | |
ten=in/10%10; | |
one=in-100*hundred-10*ten; | |
cout<<hundred<<"*100+" | |
<<ten<<"*10+" | |
<<one<<"*1"; | |
digit=(in/100!=0)+(in/10!=0)+(in!=0); | |
cout<<"\ndigit:"<<digit; | |
} | |
cout<<endl; | |
return 0; | |
} | |
//2 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
char in; | |
cin>>in; | |
switch(in) | |
{ | |
case 'A': | |
cout<<"90以上"; | |
break; | |
case 'B': | |
cout<<"80-90"; | |
break; | |
case 'C': | |
cout<<"70-80"; | |
break; | |
case 'D': | |
cout<<"60-70"; | |
break; | |
case 'E': | |
cout<<"60以下"; | |
break; | |
default: | |
cout<<"其他"; | |
} | |
return 0; | |
} | |
//3 | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int main() | |
{ | |
for(int i=100; i<1000; i++) | |
//为Code::Blocks 17.12自带编译器的bug(不出407)做出了优化 | |
if(i==(int)(pow(i/100,3)+pow(i/10%10,3)+pow(i%10,3))) | |
cout<<i<<endl; | |
return 0; | |
} | |
//4 | |
#include <iostream> | |
void repeat_output(char ch,int i) | |
{ | |
for(int x=0; x<i; x++) | |
std::cout<<ch; | |
} | |
void a_line(int i,int j) | |
{ | |
repeat_output(' ',i-j); | |
repeat_output('*',2*j-1); | |
std::cout<<'\n'; | |
} | |
int main() | |
{ | |
for(;;) | |
{ | |
int i,j; | |
std::cin>>i; | |
if(i<0) | |
break; | |
for(j=1; j<i+1; j++) | |
a_line(i,j); | |
for(j-=2; j>0; j--) | |
a_line(i,j); | |
} | |
return 0; | |
} | |
//自由项目1 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int i; | |
cin>>i; | |
int count=0; | |
for(int x=2; x<=i; x++) | |
{ | |
int c=0; | |
for(int y=1; y<=x; y++) | |
if(x%y==0&&!(y>x/y)) | |
c++; | |
if(c==1) | |
{ | |
count++; | |
cout<<count<<':'<<x<<endl; | |
} | |
} | |
return 0; | |
} | |
//5 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int s = 0; | |
for (int ps = 1; ps <= 10; ps++) | |
{ | |
int ms = 1; | |
for (int i = 1; i <= ps; i++) | |
ms *= i; | |
s += ms; | |
} | |
cout << s; | |
return 0; | |
} | |
//5(同学做的正常版本,不应该双for的。。。) | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int i=1,s=1,sum=0; | |
for (; i<=10; i++) | |
{ | |
s*=i; | |
sum+=s; | |
} | |
cout<<sum<<endl; | |
return 0; | |
} | |
//6 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int in; | |
cout<<"prime check:"; | |
cin>>in; | |
int fl=0; | |
for(int f=1; f<=in; f++) | |
if(in%f==0&&!(f>in/f)) | |
fl++; | |
if(fl==1) | |
cout<<in<<" is A prime"<<endl; | |
else | |
cout<<in<<" is NOT a prime"<<endl; | |
return 0; | |
} | |
//课堂作业1 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int i; | |
cin>>i; | |
if(i<0||1>100) | |
cout<<"Invalid input!"; | |
else if(i>=90) | |
cout<<"A"; | |
else if(i>=80) | |
cout<<"B"; | |
else if(i>=70) | |
cout<<"C"; | |
else if(i>=60) | |
cout<<"D"; | |
else | |
cout<<"E"; | |
return 0; | |
} | |
//课堂作业1(另) | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int i; | |
cin>>i; | |
switch((int)i/10) | |
{ | |
case 9: | |
cout<<"A"; | |
break; | |
case 8: | |
cout<<"B"; | |
break; | |
case 7: | |
cout<<"C"; | |
break; | |
case 6: | |
cout<<"D"; | |
break; | |
default: | |
cout<<"E"; | |
} | |
return 0; | |
} | |
//课堂作业2 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int line_num; | |
cin>>line_num; | |
for(int i=1; i<=line_num; i++) | |
{ | |
for(int j=line_num-i; j>=1; j--) | |
cout<<' '; | |
for(int k=1; k<=2*i-1; k++) | |
cout<<'*'; | |
cout<<endl; | |
} | |
return 0; | |
} | |
//课堂作业3 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int n=1,c=10; | |
do | |
{ | |
n=2*(n+1); | |
c--; | |
} | |
while(c!=1); | |
cout<<n; | |
return 0; | |
} | |
//课堂作业4 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int a,n; | |
cin>>a>>n; | |
int r=0; | |
for(int x=1; x<=n; x++) | |
{ | |
int b=a,v=a; | |
for(int i=1; i<x; i++) | |
{ | |
v*=10; | |
b+=v; | |
} | |
r+=b; | |
} | |
cout<<r; | |
return 0; | |
} | |
//课堂作业4(另) | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int a,n,i=1,sn=0,tn=0; | |
cin>>a>>n; | |
while(i<=n) | |
{ | |
tn+=a; | |
sn+=tn; | |
a*=10; | |
++i; | |
} | |
cout<<sn; | |
return 0; | |
} | |
//7 | |
#include <iostream> | |
#include <iomanip> | |
using namespace std; | |
int main() | |
{ | |
int i[10] = {2, 3, 5, 7, 11, 101, 131, 151, 181, 191} | |
,c = 5; | |
while (c) | |
{ | |
cout<<setw(4) << i[5 - c]; | |
c--; | |
} | |
cout << endl; | |
while (c < 5) | |
{ | |
cout <<setw(4)<< i[5 + c]; | |
c++; | |
} | |
return 0; | |
} | |
//8 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int i[5][5] = { 2, 3, 5, 7, 11, | |
101, 131, 151, 181, 191, | |
313, 353, 373, 383, 727, | |
757, 787, 797, 919, 929, | |
10301,10501,10601,11311,11411 | |
}, | |
s = 0; | |
for (int x = 0; x < 5; ++x) | |
s += i[x][x]; | |
cout << s; | |
return 0; | |
} | |
//自由项目2 | |
#include <iostream> | |
#include <stdlib.h> | |
using namespace std; | |
int main() | |
{ | |
system("color 1F"); | |
int i[10] = { 2,3,5,7,11,13,17,19,23,29 }; | |
for (int n = 0; n < sizeof(i) / sizeof(int); n++) | |
cout << &i[n] << ':' << *(i + n) << endl; | |
return 0; | |
} | |
//课堂作业5 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int a[9][9]= {}; | |
for(int i=0; i<9; i++) | |
a[i][i]=1; | |
for(int i=0; i<9; i++) | |
{ | |
for(int j=0; j<9; j++) | |
cout<<a[i][j]; | |
cout<<endl; | |
} | |
} | |
//课堂作业6 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int ip[4][5]= {}; | |
for(int i=0; i<4; i++) | |
for(int j=0; j<5; j++) | |
cin>>ip[i][j]; | |
for(int i=0; i<4; i++) | |
for(int j=0; j<5; j++) | |
if(ip[i][j]<0) | |
cout<<i+1<<','<<j+1<<':'<<ip[i][j]<<" <0"<<endl; | |
} | |
//课堂作业6(改题目) | |
#include <iostream> | |
#include <climits> | |
using namespace std; | |
int main() | |
{ | |
int ip[4][5]= {}; | |
for(int i=0; i<4; i++) | |
for(int j=0; j<5; j++) | |
cin>>ip[i][j]; | |
int x=INT_MIN,mi=0,mj=0; | |
for(int i=0; i<4; i++) | |
for(int j=0; j<5; j++) | |
if(x<ip[i][j]) | |
{ | |
x=ip[i][j]; | |
mi=i; | |
mj=j; | |
} | |
cout<<mi+1<<','<<mj+1<<':'<<x<<" max"<<endl; | |
} | |
//课堂作业7 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int i[7]= {1,4,6,2,5,7,3}; | |
for(int d=0; d<5; d++) | |
for(int c=0,tmp; c<6; c++) | |
{ | |
if(i[c+1]<i[c]) | |
{ | |
tmp=i[c]; | |
i[c]=i[c+1]; | |
i[c+1]=tmp; | |
} | |
} | |
for(int n=0; n<7; n++) | |
cout<<i[n]; | |
} | |
//课堂作业8 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int ixi; | |
cout<<"?x?:"; | |
cin>>ixi; | |
int ip[ixi][ixi]= {}; | |
for(int i=0; i<ixi; i++) | |
for(int j=0; j<ixi; j++) | |
cin>>ip[i][j]; | |
int sum=0; | |
for(int i=0; i<ixi; i++) | |
sum+=ip[0][i]; | |
for(int i=1,x=0; i<ixi; i++) | |
{ | |
for(int j=0; j<ixi; j++) | |
x+=ip[i][j]; | |
if(x!=sum) | |
return -1; | |
x=0; | |
} | |
for(int i=0,x=0; i<ixi; i++) | |
{ | |
for(int j=0; j<ixi; j++) | |
x+=ip[j][i]; | |
if(x!=sum) | |
return -2; | |
x=0; | |
} | |
int u=0; | |
for(int i=0; i<ixi; i++) | |
u+=ip[i][i]; | |
if(u!=sum) | |
return -3; | |
u=0; | |
for(int i=0; i<ixi; i++) | |
u+=ip[i][ixi-i-1]; | |
if(u!=sum) | |
return -4; | |
return 0; | |
} | |
//9 | |
//不能用VC6.0编译,但可以用codeblocks编译 | |
#include <iomanip> | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int w=6; | |
int c; | |
cin>>c; | |
int m[c][c]= {}; | |
for (int i=0; i<c; i++) | |
m[i][0]=1; | |
for (int y=1; y<c; y++) | |
for (int x=1; x<c; x++) | |
m[y][x]=m[y-1][x-1]+m[y-1][x]; | |
for (int y=0; y<c; y++) | |
{ | |
for (int x=0; x<c; x++) | |
if (m[y][x]!=0) | |
cout<<setw(w)<< m[y][x]; | |
cout<<endl; | |
} | |
return 0; | |
} | |
//9(修改,顺便记录以下VS2017以及VC需要注意的) | |
//VS2017编译用库 | |
//#include "stdafx.h" | |
#include <iomanip> | |
#include <cmath> | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
/*VC6没有变量作用域的概念,在不同域中定义重名变量也会出现redefinition错误。这非常 | |
糟糕,不得不将所有要用的变量一块定义,且都可以全局访问,容易引起混淆,很不方便。*/ | |
//坐等老师讲到类和对象引入权限修饰符 | |
int w, c, x, y; | |
cin >> c; | |
if (c < 1) | |
return -1; | |
//直接定义动态数组只在codeblocks可用,这里用指针数组 | |
//int m[c][c]= {}; | |
int **m = new int*[c]; | |
for (x = 0; x < c; x++) | |
m[x] = new int[c]; | |
//也不能直接赋空值,通过循环实现填充0 | |
//m= {}; | |
for (y = 0; y < c; y++) | |
for (x = 0; x < c; x++) | |
m[y][x] = 0; | |
//首列填充1,作为计算的基础 | |
for (x = 0; x < c; x++) | |
m[x][0] = 1; | |
//通过相对位置(上一行和列以及同列上一行的值的和)取值 | |
for (y = 1; y < c; y++) | |
for (x = 1; x < c; x++) | |
m[y][x] = m[y - 1][x - 1] + m[y - 1][x]; | |
/*取最大值(底部中间)用来设置最佳行宽 | |
至少间隔一格*/ | |
w = log10(m[c - 1][c / 2]) + 2; | |
//输出(不输出0) | |
for (y = 0; y < c; y++) | |
{ | |
for (x = 0; x < c; x++) | |
if (m[y][x] != 0) | |
cout << setw(w) << m[y][x]; | |
cout << endl; | |
} | |
//释放指针 | |
for (x = 0; x < c; x++) | |
delete[] m[x]; | |
delete[] m; | |
//在VS2017上暂停 | |
cin.get(); | |
cin.get(); | |
return 0; | |
} | |
//10 | |
#include <iostream> | |
using namespace std; | |
void factor_output(int); | |
int main() | |
{ | |
int ip; | |
cin>>ip; | |
factor_output(ip); | |
} | |
void factor_output(int ip) | |
{ | |
for(int i=1; i<=ip; i++) | |
if(ip%i==0) | |
cout<<i<<' '; | |
} | |
//课堂作业9 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
char c; | |
cin>>c; | |
char ca[128]; | |
int i; | |
for(i=-1; i<2; i++) | |
ca[i+1]=(char)(c+i); | |
for(i=0; i<3; i++) | |
{ | |
switch(ca[i]) | |
{ | |
case 96: | |
ca[i]='z'; | |
break; | |
case 123: | |
ca[i]='a'; | |
break; | |
case 64: | |
ca[i]='Z'; | |
break; | |
case 91: | |
ca[i]='A'; | |
break; | |
} | |
cout<<ca[i]<<endl; | |
} | |
return 0; | |
} | |
//课堂作业10 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
char c[10000],cc[10000],s; | |
int ws,p,i; | |
cin>>ws>>p>>s; | |
if(p>ws) | |
return -1; | |
for(i=0; i<ws; i++) | |
cin>>c[i]; | |
for(i=0; i<p; i++) | |
cc[i]=c[i]; | |
cc[p]=s; | |
for(i=p; i<ws+1; i++) | |
cc[i+1]=c[i]; | |
for(i=0; i<ws+1; i++) | |
cout<<cc[i]; | |
return 0; | |
} | |
//课堂作业11 | |
#include <iostream> | |
using namespace std; | |
int string_length(char[]); | |
int main() | |
{ | |
char c[]="qwertyuiopasdfghjklzxcvbnm"; | |
cout<<string_length(c); | |
return 0; | |
} | |
/** | |
取字符串长度 | |
@param char[] 待测量的字符串 | |
@return 长度 | |
*/ | |
int string_length(char c[]) | |
{ | |
int i=0; | |
while(c[i]!='\0') | |
i++; | |
return i; | |
} | |
//课堂作业12 | |
#include <climits> | |
#include <iostream> | |
using namespace std; | |
int min_val(int ip[],int n) | |
{ | |
int tmp=INT_MAX; | |
for(int c=0; c<n; c++) | |
if(ip[c]<tmp) | |
tmp=ip[c]; | |
return tmp; | |
} | |
int main() | |
{ | |
int i[]={435,2345,567,123,-324,34,45},len=sizeof(i)/sizeof(int); | |
cout<<min_val(i,len); | |
return 0; | |
} | |
//课堂13 | |
#include <iostream> | |
using namespace std; | |
bool range_test(int,int,int); | |
int main() | |
{ | |
int i,a,b; | |
cin>>i>>a>>b; | |
cout<<range_test(i,a,b); | |
return 0; | |
} | |
bool range_test(int val,int low,int high) | |
{ | |
if(val>=low&&val<=high) | |
return true; | |
return false; | |
} | |
//课堂14 | |
#include <iostream> | |
using namespace std; | |
int words_num(char[]); | |
int main() | |
{ | |
char ip[10000]="alpha beta gamma delta epsilon zeta"; | |
cout<<words_num(ip); | |
return 0; | |
} | |
int words_num(char ch[]) | |
{ | |
int len=0; | |
while(ch[len]!='\0') | |
len++; | |
int i=0,c=1,status=0; | |
for(; i<len-1; i++) | |
{ | |
if(ch[i]==' ') | |
status=1; | |
if(ch[i]!=' '&&status==1) | |
{ | |
c++; | |
status=0; | |
} | |
} | |
return c; | |
} | |
//11 | |
#include <climits> | |
#include <iostream> | |
using namespace std; | |
int max_val(int ip[],int n) | |
{ | |
int tmp=INT_MIN; | |
for(int c=0; c<n; c++) | |
if(ip[c]>tmp) | |
tmp=ip[c]; | |
return tmp; | |
} | |
int main() | |
{ | |
int i[]={435,2345,567,123,-324,34,45},len=sizeof(i)/sizeof(int); | |
cout<<max_val(i,len); | |
return 0; | |
} | |
//12 | |
#include <iostream> | |
using namespace std; | |
class Box | |
{ | |
int length; | |
int width; | |
int height; | |
public: | |
Box() | |
{ | |
length=0; | |
width=0; | |
height=0; | |
}; | |
Box(int _length,int _width,int _height) | |
{ | |
length=_length; | |
width=_width; | |
height=_height; | |
}; | |
int volume() | |
{ | |
return length*width*height; | |
} | |
}; | |
int main() | |
{ | |
Box b=Box(12,20,25); | |
cout<<b.volume(); | |
return 0; | |
} | |
//13 | |
#include <iostream> | |
using namespace std; | |
//Obj statement | |
class student | |
{ | |
int id; | |
int mark; | |
public: | |
student(int id,int mark); | |
void show(); | |
}; | |
//Obj definition | |
student::student(int id,int mark) | |
{ | |
(*this).id=id; | |
this->mark=mark; | |
} | |
void student::show() | |
{ | |
cout<<"id:"<<id<<endl<<"mark:"<<mark<<endl; | |
} | |
//Main | |
int main() | |
{ | |
//Initialize obj array and memory address. | |
student s[5]= | |
{ | |
student(1,2), | |
student(2,4), | |
student(3,6), | |
student(4,8), | |
student(5,10), | |
},*p=s; | |
//Output | |
for(int i=0; i<3; i++) | |
(p+i*2)->show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment