upload :)

This commit is contained in:
miam1gh0st 2023-10-18 16:17:32 +08:00
parent 2061a0732b
commit e150ec5613
1076 changed files with 12341 additions and 0 deletions

1
c/README.md Normal file
View file

@ -0,0 +1 @@
All the C source files gathered here.

View file

@ -0,0 +1,201 @@
/*
E. Toocold之区间最大子段和
Description
Toocold学会了怎样求区间最大子段和,.n个数,l限定在[ll,rr], r限定在[ll2,rr2]?
Input
cas(1<=cas<=10)cas
cas的第一行是一个数n(1<=n<=100000)n个数
n个数,a1,a2,a3...an(-1000<=ai<=1000)
q(1<=q<=100000)q个询问
q行每行四个数字ll, rr, ll2, rr2.(1<=ll,rr,ll2,rr2<=n)(ll<=rr)(ll2<=rr2)(rr2>=ll)l的范围和右端点r的范围
Output
Sample Input
1
6
4 -3 1 -3 5 -2
2
1 4 2 5
1 1 3 4
Sample Output
4
2
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100015;
int n,m;
int a[MAXN],pre_sum[MAXN],suf_sum[MAXN],l1,l2,r1,r2;
struct tnode
{
int sum,lmax,rmax,ans;
int l,r;
};
tnode operator + (const tnode& a,const tnode& b)
{
tnode c;
c.l=a.l;
c.r=b.r;
c.sum=a.sum+b.sum;
c.lmax=max(a.lmax,a.sum+b.lmax);
c.rmax=max(a.rmax+b.sum,b.rmax);
c.ans=max(a.ans,b.ans);
c.ans=max(c.ans,a.rmax+b.lmax);
return c;
}
struct Segment_Tree
{
tnode t[4*MAXN];
void update(int root)
{
int ch=root<<1;
t[root]=t[ch]+t[ch+1];
}
void buildt(int root,int l,int r,int A[])
{
if(l!=r)
{
int mid=(l+r)>>1;
int ch=root<<1;
buildt(ch,l,mid,A);
buildt(ch+1,mid+1,r,A);
update(root);
}
else
{
t[root].ans=t[root].lmax=t[root].rmax=t[root].sum=A[l];
t[root].l=t[root].r=l;
}
}
tnode query(int root,int l,int r)
{
if(t[root].l==l&&t[root].r==r)
{
return t[root];
}
int mid=(t[root].l+t[root].r)>>1;
int ch=root<<1;
if(r<=mid)return query(ch,l,r);
else if(l>mid)return query(ch+1,l,r);
else return query(ch,l,mid)+query(ch+1,mid+1,r);
}
};
Segment_Tree ST1;
struct tnodes
{
int Max;
int l,r;
};
struct Segment_Tree_Max
{
tnodes t[4*MAXN];
void buildt(int root,int l,int r,int A[])
{
t[root].l=l;
t[root].r=r;
if(l!=r)
{
int mid=(l+r)>>1;
int ch=root<<1;
buildt(ch,l,mid,A);
buildt(ch+1,mid+1,r,A);
t[root].Max=max(t[ch].Max,t[ch+1].Max);
}
else t[root].Max=A[l];
}
int query(int root,int l,int r)
{
if(t[root].l==l&&t[root].r==r)
{
return t[root].Max;
}
int mid=(t[root].l+t[root].r)>>1;
int ch=root<<1;
if(r<=mid)return query(ch,l,r);
else if(l>mid)return query(ch+1,l,r);
else return max(query(ch,l,mid),query(ch+1,mid+1,r));
}
};
Segment_Tree_Max ST2,ST3;
int get_sum(int l,int r)
{
if(l>r)return 0;
return pre_sum[r]-pre_sum[l-1];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
}
ST1.buildt(1,1,n,a);
pre_sum[0]=suf_sum[n+1]=0;
for(int i=1;i<=n;++i)
{
pre_sum[i]=pre_sum[i-1]+a[i];
}
for(int i=n;i;--i)
{
suf_sum[i]=suf_sum[i+1]+a[i];
}
ST2.buildt(1,1,n,pre_sum);
ST3.buildt(1,1,n,suf_sum);
scanf("%d",&m);
while(m--)
{
scanf("%d %d %d %d",&l1,&r1,&l2,&r2);
if(r1<l2)///3 无交
{
printf("%d\n",ST3.query(1,l1,r1)-pre_sum[n]+ST2.query(1,l2,r2));
}
else if(l2<=l1&&l1<=r2&&r2<=r1)///2
{
printf("%d\n",ST1.query(1,l1,r2).ans);
}
else if(l1<=l2&&l2<=r1&&r1<=r2)///1
{
int temp=ST1.query(1,l2,r1).ans;
if(l2-1>=l1)
{
temp=max(temp,ST3.query(1,l1,l2-1)-pre_sum[n]+ST2.query(1,l2,r2));
}
if(r1+1<=r2)
{
temp=max(temp,ST2.query(1,r1+1,r2)-pre_sum[n]+ST3.query(1,l1,r1));
}
printf("%d\n",temp);
}
else if(l1<=l2&&l2<=r2&&r2<=r1)///4
{
int temp=ST1.query(1,l2,r2).ans;
if(l2-1>=l1)
{
temp=max(temp,ST3.query(1,l1,l2-1)-pre_sum[n]+ST2.query(1,l2,r2));
}
printf("%d\n",temp);
}
else if(l2<=l1&&l1<=r1&&r1<=r2)///5
{
int temp=ST1.query(1,l1,r1).ans;
if(r1+1<=r2)
{
temp=max(temp,ST2.query(1,r1+1,r2)-pre_sum[n]+ST3.query(1,l1,r1));
}
printf("%d\n",temp);
}
}
}
return 0;
}

View file

@ -0,0 +1,62 @@
/*
C.
Description
便
Input
N1<=N<=1000N个正整数M1<=M<=1000
Output
Sample Input
5
14 12 2 18 15
6
4 9 0 16 16 12
Sample Output
4 83
4 70
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1010];
int main()
{
int n;
while(cin>>n)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int ans=INT_MAX;
int pos=0;
for(int i=1;i<=n;i++)
{
int cnt=0;
for(int j=1;j<=n;j++)
{
cnt+=a[j]*(abs(j-i));
}
if(cnt<ans)
{
ans=cnt;
pos=i;
}
}
cout<<pos<<' '<<ans<<endl;
}
return 0;
}

View file

@ -0,0 +1,70 @@
/*
G.
Description
n[-1000,1000] -1000~1000
:
5 5-7 7
Input
n n
Output
Sample Input
10
-562 232 969 201 -111 378 -610 127 245 932
10
868 -838 -958 200 867 -920 -493 114 -800 757
10
-607 -260 -270 -833 560 -280 404 -542 560 -115
Sample Output
2363
2828
1970
Hint
1 232+969+201-111+378-610+127+245+932=2363 2363
2 -838+-958+200+867+-920+-493+114+-800= -2828 2828
20% n<=10
50% n<=100
70% n<=1000
100% n<=1000000
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1000005;
const long long inf=(1LL<<60);
int n;
long long pre_sum[MAXN],pre_min[MAXN],pre_max[MAXN],ans,a[MAXN];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
pre_sum[i]=pre_sum[i-1]+a[i];
}
for(int i=1;i<=n;i++)
{
pre_max[i]=max(pre_max[i-1],pre_sum[i-1]);
pre_min[i]=min(pre_min[i-1],pre_sum[i-1]);
ans=max(ans,abs(pre_sum[i]-pre_min[i]));
ans=max(ans,abs(pre_sum[i]-pre_max[i]));
}
printf("%d\n",ans);
}

View file

@ -0,0 +1,75 @@
/*
Description
N个数字,
Input
T(1<=T<=20) .
T , N(1<=N<=100000), N个整数.int范围内
Output
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
14
7
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
int n;
long long a[MAXN],sum,ansum;
pair<int,int>ans;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%lld",&a[i]);
}
bool flag=false;
for(int i=1;i<=n;++i)
{
if(a[i]>=0)flag=true;
}
if(!flag)
{
printf("0\n");
continue;
}
int pos=0;
ansum=-1;
sum=0;
for(int i=1;i<=n;++i)
{
sum+=a[i];
if(sum<0)
{
sum=0;
pos=i;
}
else
{
if(sum>ansum)
{
ansum=sum;
ans=make_pair(pos+1,i);
}
else if(sum==ansum)
{
ans=min(ans,make_pair(pos+1,i));
}
}
}
printf("%lld\n",ansum);
}
return 0;
}

View file

@ -0,0 +1,51 @@
/*
C.
Description
n个整数a1,a2,anai,ai+1,aji<=i<=j<=n的子段和的最大值0From为1To为na1,a2,a3,a4,a5,a6=-2,11,-4,13,-5,-2a2+a3+a4=20i=2,j=41
Input
nn<=1000n
Output
Sample Input
6
-2 11 -4 13 -5 -2
Sample Output
From=2,To=4
MaxSum=20
*/
#include <stdio.h>
int a[1001],n,max,from,to,s;
int main()
{
int i,j,n;
while(1==scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
from=1;
to=n;
max=0;
for(i=1;i<=n;i++)
{
s=0;
for(j=i;j<=n;j++)
{
s+=a[j];
if(max<s)
{
max=s;
from=i;
to=j;
}
}
}
printf("From=%d,To=%d\n",from,to);
printf("MaxSum=%d\n",max);
}
return 0;
}

View file

@ -0,0 +1,92 @@
/*
D.
Description
n个整数a1a2a3an1<=i<=j<=n1<=n<=100000ai+ai+1+ai+2+aj的最大值n个整数都为负数
Input
nn个整数
Output
i和jSample Output
Sample Input
6
2 -5 11 -4 13 -2
Sample Output
From=3,To=5
MaxSum=20
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
int n;
long long a[MAXN],sum,ansum;
pair<int,int>ans;
const long long inf=(1LL)<<61;
void div_algorithm(int l,int r)
{
if(l>r)return;
int mid=(l+r)>>1;
long long lsum=0,rsum=0,lmax=-inf,rmax=-inf;
int lpos,rpos;
for(int i=mid;i>=l;--i)
{
lsum+=a[i];
if(lmax<lsum)
{
lmax=lsum;
lpos=i;
}
else if(lmax==lsum)
{
lpos=i;
}
}
for(int i=mid;i<=r;++i)
{
rsum+=a[i];
if(rmax<rsum)
{
rmax=rsum;
rpos=i;
}
}
if(ansum<lmax+rmax-a[mid])
{
ansum=lmax+rmax-a[mid];
ans=make_pair(lpos,rpos);
}
else if(ansum==lmax+rmax-a[mid])
{
ans=min(ans,make_pair(lpos,rpos));
}
div_algorithm(l,mid-1);
div_algorithm(mid+1,r);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;++i)
{
scanf("%lld",&a[i]);
}
bool flag=false;
for(int i=1;i<=n;++i)
{
if(a[i]>=0)flag=true;
}
if(!flag)
{
printf("From=0,To=0\nMaxSum=0\n");
continue;
}
ansum=-1;
sum=0;
div_algorithm(1,n);
printf("From=%d,To=%d\nMaxSum=%lld\n",ans.first,ans.second,ansum);
}
return 0;
}

View file

@ -0,0 +1,54 @@
/*
A.
Description
n个整数a1,a2,anai,ai+1,aj 1<=i<=j<=n的子段和的最大值0a1,a2,a3,a4,a5,a6=-2,11,-4,13,-5,-2a2+a3+a4=20i=2,j=41
n<=100n的范围规定修改为n<=200
Input
nn个整数
Output
From=xxx,To=xxxMaxSum=xxxx
Sample Input
6
-2 11 -4 13 -5 -2
Sample Output
From=2,To=4
MaxSum=20
*/
#include <stdio.h>
int a[1001],n,max,from,to,s;
int main()
{
int i,j,n;
while(1==scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
from=1;
to=n;
max=0;
for(i=1;i<=n;i++)
{
s=0;
for(j=i;j<=n;j++)
{
s+=a[j];
if(max<s)
{
max=s;
from=i;
to=j;
}
}
}
printf("From=%d,To=%d\n",from,to);
printf("MaxSum=%d\n",max);
}
return 0;
}

View file

@ -0,0 +1,54 @@
/*
B.
Description
n个整数a1,a2,anai,ai+1,aj i<=i<=j<=n的子段和的最大值00a1,a2,a3,a4,a5,a6=-2,11,-4,13,-5,-2a2+a3+a4=20i=2,j=41
n<=200n的范围规定修改为n<=2000
Input
nn个整数
Output
From=xxx,To=xxxMaxSum=xxxx
Sample Input
6
-2 11 -4 13 -5 -2
Sample Output
From=2,To=4
MaxSum=20
*/
#include <stdio.h>
int a[1001],n,max,from,to,s;
int main()
{
int i,j,n;
while(1==scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
from=1;
to=n;
max=0;
for(i=1;i<=n;i++)
{
s=0;
for(j=i;j<=n;j++)
{
s+=a[j];
if(max<s)
{
max=s;
from=i;
to=j;
}
}
}
printf("From=%d,To=%d\n",from,to);
printf("MaxSum=%d\n",max);
}
return 0;
}

View file

@ -0,0 +1,28 @@
#
'''
E. 穿错衣服问题
Description
SmallBeer曾经当过兵他常常回忆起刚入伍时的情景记得那时周末只有一天休息而且部队规定休息天外出营区只能有三分之一人数所以大家都很珍惜周末外出的机会一旦外出肯定要尽情享受一天的乐趣但部队规定下午五点必须归队因此大家外出后回来都很匆忙其中最有意思的是早上外出前大家都把军装洗完晾好下午归队时赶紧穿上但由于时间紧常常发生穿错衣服的事件现在请你这个编程高手想办法算算所有人都穿错的情况有多少种
Input
有多组测试数据对于每一组测试数据输入只有一行即总人数n(1<=n<=1000)
Output
对于每一组的输入输出只有一行即所有人都穿错的情况数
Sample Input
1
2
Sample Output
0
1
'''
a=[0,0,1]
for i in range(3,1010):
a.append((i-1)*(a[i-2]+a[i-1]))
while True:
try:
n=int(input())
print(a[n])
except:
break

View file

@ -0,0 +1,59 @@
/*
B.
Description
NIT篮球协会要在学校内选一批篮球宝贝
Input
n和mn表示学生总数m表示需要选拔的篮球宝贝的人数1<=n<=200001<=m<=50F和T表示F<=<=T150<=F<=T<=180n行是学生的数据S表示性别S=0S=1H表示身高G表示年级1<=G<=4
Output
m个满足要求的学生的输入顺序号No such student!
Sample Input
2
3 1
160 165
1 181 2
0 162 3
0 163 2
3 2
160 165
1 180 1
0 165 1
0 163 3
Sample Output
3
No such student!
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
scanf("%d",&t);
int a,b,c;
while(t--)
{
int n,m,l,r;
cin>>n>>m>>l>>r;
int ans=0;
for(int i=0;i<n;i++)
{
cin>>a>>b>>c;
if(m==0)continue;
if(a==0&&b<=r&&b>=l&&c<=2)
{
m--;
}
if(!m)
{
ans=i+1;
}
}
if(!m)cout<<ans<<endl;
else cout<<"No such student!"<<endl;
}
return 0;
}

View file

@ -0,0 +1,31 @@
#
'''
D. 自增自减运算
Description
小胖子正在学习汇编语言汇编语言里面有自增和自减指令叫inc和dec指令其实C语言里面也有自增和自减运算的++运算但这些指令或者运算都是有范围的现在小胖子碰到了很长很长的整数小胖子脑子一转很快就想出来现在请你也实现这个自增和自减运算
Input
输入有多组第一行是测试数据的组数对于每一组测试数据共有两行其中第一行是一个整数M0表示自减运算1表示自增运算第二行是一个很长的整数它的位数不超过10000位输入一定符合数学规定的要求并且不会有多余的字符
Output
输出只有一行
Sample Input
2
0
100
1
200
Sample Output
99
201
'''
t=int(input())
for i in range(t):
m=int(input())
a=int(input())
if m==0:
a-=1
else:
a+=1
print(a)

View file

@ -0,0 +1,48 @@
/*
A.
Description
NIT学校前不久刚结束了一次大一学生的英语测试
Input
n21<=n<=2000n个整数n个学生的英语成绩0<=<=100
Output
2
Sample Input
2
30
65 71 94 83 62 77 81 96 80 70 66 72 95 84 63 78 82 97 81 71 64 70 93 82 61 76 80 95 79 69
22
77 81 96 80 70 66 72 95 84 63 78 82 97 81 71 64 70 93 82 61 76 80
Sample Output
26
19.10
33
18.86
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
double a[2010];
int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
double sum=0;
for(int i=0;i<n;i++)
{
scanf("%lf",&a[i]);
sum+=a[i];
}
sort(a,a+n);
reverse(a,a+n);
printf("%.0f\n",a[0]-a[19]);
printf("%.2f\n",a[0]-sum/n);
}
return 0;
}

View file

@ -0,0 +1 @@
老蔡你来真的啊0.o

View file

@ -0,0 +1,36 @@
/*7-1 找出一批学生的最高分
10
:
:
:
67 88 73 54 0 95 60 -1
:
95*/
#include<stdio.h>
int main()
{
int num,maxnum;
scanf("%d",&num);
maxnum=num;
for(;num>=0;){
scanf("%d",&num);
if(num>maxnum){
maxnum=num;
}
}
printf("%d",maxnum);
return 0;
}

View file

@ -0,0 +1,40 @@
/*
7-10 ???????
?? 10
?? ???
?? ????
??????????M?N???????????????
????:
?????????????M?N(1=M=N=500)?
????:
????????M?N??????????????,?????????
????:
10 31
????:
7 143
*/
#include<stdio.h>
int main(){
int m,n,i,j,cnt=0,sum=0,flag=1;
scanf("%d %d",&m,&n);
if(m==1) m=2;
for(i=m;i<=n;i++){
flag=1;
for(j=2;j<i-1;j++){
if(i%j==0){
flag=0;
break;
}
}
if(flag){
cnt++;
sum+=i;
}
}
printf("%d %d\n",cnt,sum);
return 0;
}

View file

@ -0,0 +1,37 @@
/*
7-3 ???????????????
?? 10
?? C???
?? ????
????????,??????? 1 - 1/4 + 1/7 - 1/10 + ... ?????????????????eps?
????:
?????????????eps?
????:
??????sum = S??????????S,???????????????????????????
????1:
4E-2
????1:
sum = 0.854457
????2:
0.02
????2:
sum = 0.826310
*/
#include<stdio.h>
int main(){
int flag=1;
double eps,a=1,sum=0;
scanf("%lf",&eps);
for(int i=1;;){
sum+=1.0/a*flag;
if(1.0/a<=eps)
break;
a+=3;
flag=-flag;
}
printf("sum = %.6lf",sum);
return 0;
}

View file

@ -0,0 +1,30 @@
/*
7-4
10
C课程组
N
10^9N
N的位数及其各位数字之和
321
3 6
*/
#include<stdio.h>
int main(){
int N,cnt=0,sum=0;
scanf("%d",&N);
for(;N!=0;){
sum+=N%10;
N=N/10;
cnt++;
}
printf("%d %d",cnt,sum);
return 0;
}

View file

@ -0,0 +1,41 @@
/*
7-5 ??????
?? 10
?? ??
?? ????
?????????/????????????,??????????,????????????????????????????????????6/12??????1/2?????????,??????????????,?11/8??11/8;?????????,?????1/1??????
????:
????????????,??????????/??,?:12/34??34??12???????????(???0,?????????????)?
??:
??C??,?scanf?????????/,?scanf????????
??Python??,?a,b=map(int, input().split('/'))?????????????
????:
?????????????????,????????,?????/???????????
5/6??6??5?
????:
66/120
????:
11/20
*/
#include<stdio.h>
int main(){
int zi,mu,r,zi_save,mu_save;
scanf("%d/%d",&zi,&mu);
zi_save=zi;
mu_save=mu;
if(zi==mu) printf("1/1");
else{
r=zi%mu;
for(;r!=0;r=zi%mu){
zi=mu;
mu=r;
}
printf("%d/%d",zi_save/mu,mu_save/mu);
}
return 0;
}

View file

@ -0,0 +1,31 @@
/*
7-7 ?e????
?? 10
?? C???
?? ????
???? e ????? 1+1/1!+1/2!+?+1/n!+? ?????????????????? n,?????? n+1 ???
????:
???????????? n(=1000)?
????:
???????????,?????????
????:
10
????:
2.71828180
*/
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
double sum=1.0,tag=1.0;
for(int i=1;i<=n;i++){
tag=tag*1.0/i;
sum+=tag;
}
printf("%.8lf",sum);
return 0;
}

View file

@ -0,0 +1,36 @@
/*
7-8 ?????????
?? 10
?? ??
?? ??????
????????,??n??????A?????????????
????:
?????????????n(1=n<7)?
????:
??n??????A????????????????????,???????????????
????:
4
????:
A B C D
E F G
H I
J
*/
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
char c='A';
for(int i=n;i>0;i--){
for(int j=i;j>0;j--){
printf("%c ",c);
c++;
}
printf("\n");
}
return 0;
}

View file

@ -0,0 +1,59 @@
/*
7-9
10
6=1+2+31236m和n之间的所有完数
2m和n1<mn10000
= 1 + 2 + ... + kNone
2 30
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
*/
#include<stdio.h>
int main()
{
int n, m, i, j, k, sum = 0;
int N = 1; //用于判断m-n之间是否有完数
int flag = 0; //判断是否是完数
scanf("%d %d", &m, &n);
for (i = m;i <= n;i++) //m到n之间找完数
{
flag = 0;
sum = 0;
for (j = 1;j <= i / 2;j++) //循环次数不会超过一半
{
if (i % j == 0)
sum += j;
}
if (sum == i) //是完数
flag = 1;
if (flag) //进行输出
{
N = 0;
printf("%d = 1 ", i); //完数和第一项因子
for (k = 2;k <= i / 2;k++) //第一项因子已经拿出来k这里取2开始
{
if (i % k == 0)
{
printf("+ %d ", k);
}
}
printf("\n");
}
}
if (N) //m-n之间没有完数
{
printf("None\n");
}
return 0;
}

View file

@ -0,0 +1,8 @@
#include<stdio.h>
int main(){
int i=10, j=0;
if (j=0)i++; else i--;
printf("%d",i);
return 0;
}

View file

@ -0,0 +1,33 @@
/*
7-2 N项和
10
C课程组
, 1 - 1/4 + 1/7 - 1/10 + ... N项之和
:
N
:
sum = SS
:
10
:
sum = 0.819
*/
#include<stdio.h>
int main(){
int N;
scanf("%d",&N);
int flag=1;
int a=1;
double sum=0;
for(int i=1;i<=N;i++,a+=3){
sum+=1.0/a*flag;
flag=-flag;
}
printf("sum = %.3lf",sum);
return 0;
}

View file

@ -0,0 +1,69 @@
/*
6-1 ?????????????
?? 10
?? ??
?? ??????
??????????????????????
??????:
int FindArrayMax( int a[], int n );
??a????????,n???a?????????????a??????
????????:
#include <stdio.h>
#define MAXN 10
int FindArrayMax( int a[], int n );
int main()
{
int i, n;
int a[MAXN];
scanf("%d", &n);
for( i=0; i<n; i++ ){
scanf("%d", &a[i]);
}
printf("%d\n", FindArrayMax(a, n));
return 0;
}
????????
????:
4
20 78 99 -14
????:
99
*/
#include <stdio.h>
#define MAXN 10
int FindArrayMax( int a[], int n );
int main()
{
int i, n;
int a[MAXN];
scanf("%d", &n);
for( i=0; i<n; i++ ){
scanf("%d", &a[i]);
}
printf("%d\n", FindArrayMax(a, n));
return 0;
}
int FindArrayMax( int a[], int n ){
int maxnum=a[0];
for(int j=0;j<n;j++){
if(a[j]>maxnum) maxnum=a[j];
}
return maxnum;
}

View file

@ -0,0 +1,60 @@
/*
7-1 ???????k?
?? 10
?? ??
?? ??????
???????????,????n???????????,???k?(k?0??)???????
???????????:
?0?:?????n??(a[0]? a[n-1])??????,??? a[0]??;
?1?:???????n-1??(a[1] ? a[n-1])??????,??? a[1] ??;
?k?:???????n-k??(a[k]?a[n-1])??????,??? a[k] ??;
?n-2?:???????2??(a[n-2] ?a[n-1])??????,??? a[n-2]???
????:
????????????10????n??????n-1????k??????n???,????????
????:
????????????k?(k?0??)?????,??k??a[0]? a[n-1]??,??????????,??????????
????:
4 1
5 1 7 2
????:
1 2 7 5
*/
#include<stdio.h>
int main()
{
int i,j,n,k;
scanf("%d %d",&n,&k);
int a[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
if(n==1) printf("%d",a[0]);
for(i=0;i<n-1;i++){
int c=i;
for(j=i+1;j<n;j++){
if(a[c]>a[j]) c=j;
}
int t=a[i];
a[i]=a[c];
a[c]=t;
if(i==k){
printf("%d",a[0]);
for(j=1;j<n;j++)
printf(" %d",a[j]);
}
}
return 0;
}

View file

@ -0,0 +1,64 @@
/*
7-2 ?????????
?? 10
?? ???
?? ????
????? N ???????????? N ?? A[ ] ?????? Avg,?????????:
[(A
1
?
-Avg)
2
+(A
2
?
-Avg)
2
+?+(A
N
?
-Avg)
2
]/N
?
?
????:
??????????????? N(=10
4
),?????? N ????????????? 1000,??????????
????:
???N??????,????????????5??
???? 1:
10
6 3 7 1 4 8 2 9 11 5
???? 1:
3.03974
???? 2:
1
2
???? 2:
0.00000
*/
#include<stdio.h>
#include<math.h>
int main(){
int N,i,num,sum=0;
double final,avg,sum2=0;
scanf("%d",&N);
int a[N];
for(i=0;i<N;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
avg=1.0*sum/N;
for(i=0;i<N;i++){
sum2+=pow(a[i]-avg,2);
}
final=sqrt(sum2/N);
printf("%.5lf",final);
return 0;
}

View file

@ -0,0 +1,43 @@
/*
7-3 ??????????
?? 10
?? ???
?? ???????
????????????????
????:
?????n( n<=1000,???????),??????? n???,?????????
????:
????????????
????:
????????????:
4
4 2 2 4
????:
?????????????:
4 2
*/
#include<stdio.h>
int main(){
int n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
int max=a[0];
for(i=0;i<n;i++){
if(a[i]>max) max=a[i];
}
int cnt=0;
for(i=0;i<n;i++){
if(max==a[i]) cnt++;
}
printf("%d %d\n",max,cnt);
return 0;
}

View file

@ -0,0 +1,57 @@
/*
7-4 ?????????
?? 10
?? C???
?? ????
????????,??????????????????????,??????????????,???????????
??:????????????????
????:
??????????????N(=10),?????N???,?????????
????:
??????????????,???????????
????:
5
8 2 5 1 4
????:
1 2 5 4 8
*/
#include<stdio.h>
int main(){
int N,i;
scanf("%d",&N);
int a[N];
for(i=0;i<N;i++){
scanf("%d",&a[i]);
}
int max=a[0],max_i=0;
for(i=0;i<N;i++){
if(max<a[i]){
max=a[i];
max_i=i;
};
}
int t=a[N-1];
a[N-1]=max;
a[max_i]=t;
int min=a[0],min_i=0;
for(i=0;i<N;i++){
if(min>a[i]){
min=a[i];
min_i=i;
};
}
t=a[0];
a[0]=min;
a[min_i]=t;
for(i=0;i<N;i++){
printf("%d ",a[i]);
}
return 0;
}

View file

@ -0,0 +1,46 @@
/*
7-5 ???????????????
?? 10
?? ???
?? ????
??????,????????????,?????????????????3???1234?2345?3456,????????????3?4,????3??
????:
????1???????N(=1000),???????N?????????????,?????????
????:
???????M: n1 n2 ...??,??M?????,n1?n2?????????????,???????????????????,???????????
????:
3
1234 2345 3456
????:
3: 3 4
*/
#include<stdio.h>
int main(){
int N,i;
scanf("%d",&N);
int a[N],b[10],c;
for(i=0;i<10;i++)
b[i]=0;
for(i=0;i<N;i++){
scanf("%d",&a[i]);
if(a[i]==0) b[0]++;
for(;a[i]!=0;a[i]/=10){
c=a[i]%10;
b[c]++;
}
}
int max=b[0];
for(i=1;i<10;i++){
if(max<b[i]) max=b[i];
}
printf("%d:",max);
for(i=0;i<10;i++){
if(max==b[i]) printf(" %d",i);
}
return 0;
}

View file

@ -0,0 +1,44 @@
/*
7-6 ???????????
?? 10
?? ??
?? ??????
????????????8???(????????1~8)??????,????n???(1=n=1000),???????,????????????(??????????????????),?????????????
????:
????1?????????n(1=n=1000),?2???n???,????????
????:
???????8??????????,????4?,????????????????
????:
10
3 4 7 6 3 9 2 3 1 8
????:
1 1
2 1
3 3
4 1
5 0
6 1
7 1
8 1
*/
#include<stdio.h>
int main(){
int n,i,num;
scanf("%d",&n);
int b[9];
for(i=0;i<n;i++){
b[i]=0;
}
for(i=0;i<n;i++){
scanf("%d",&num);
b[num]++;
}
for(i=1;i<=8;i++){
printf("%4d%4d\n",i,b[i]);
}
return 0;
}

View file

@ -0,0 +1,43 @@
/*
7-7 ???????
?? 10
?? ??
?? ???????
????
??????????????????????(???????,?????????????,??????)
????:
???????,??????????,?????2?30??1073741824,?????????????
????:
??????????????,????????????
????:
25 36 0 1
1024 1073741824
????:
11001
100100
0
1
10000000000
1000000000000000000000000000000
*/
#include<stdio.h>
int main(){
int n;
int b[10000];
while(scanf("%d",&n)==1){
int i,cnt=0;
if(n==0) printf("0");
for(i=0;n!=0;n/=2){
b[i]=n%2;
cnt++;
i++;
}
for(i=cnt-1;i>=0;i--){
printf("%d",b[i]);
}
printf("\n");
}
return 0;
}

View file

@ -0,0 +1,43 @@
/*
7-8 ?????????????
?? 10
?? ??
?? ??????
????????,????n???????a?,?????a??????x?????a?????x????,????????????????(???0??);??????,??Not Found?
????:
????1?????????n(1=n=100)?????x,?2???n???,??????????????????????????
????:
????,???x??????????????;??????,??????Not Found?
????1:
5 9
2 9 8 1 9
????1:
4
????2:
10 101
2 8 10 1 9 8 -101 0 98762 1
????2:
Not Found
*/
#include<stdio.h>
int main(){
int n,x,i,cnt=0,max;
scanf("%d %d",&n,&x);
int a[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
if(a[i]==x){
max=i;
cnt++;
}
}
if(cnt==0) printf("Not Found\n");
else printf("%d",max);
return 0;
}

View file

@ -0,0 +1,60 @@
/*
7-9 ????????
?? 10
?? ??
?? ??????
???????????,????n????????????,?????????????????
?????????????:
?1?:?????n??(a[0]? a[n-1])?,?a[0]?,??????????,????????????,??????????????,???????????a[n-1];
?2?:???????n-1??(a[0] ? a[n-2])?,?a[0]?,??????????,????????????,??????????????,a[0] ? a[n-2]?????????a[n-2];
?i?:???????n-k??(a[0]?a[n-i])?,?a[0]?,??????????,????????????,??????????????,a[0] ? a[n-i]?????????a[n-i];
?n-1?:???????2??(a[0] ?a[1])?,??????,????????,??????????????,a[0] ? a[1]?????????a[1]?
????:
????????????10????n??????n???,????????
????:
?????????????????????,?????a[0]? a[n-1]??,??????????,??????????
????:
5
8 7 6 0 1
????:
7 6 0 1 8
6 0 1 7 8
0 1 6 7 8
0 1 6 7 8
*/
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
printf("%d",a[0]);
for(int k=1;k<n;k++){
printf(" %d",a[k]);
}
printf("\n");
}
return 0;
}

View file

@ -0,0 +1,59 @@
/*
7-1 ?????????
?? 10
?? ???
?? ????
??M?N??????A,??A??????A[i][j]?????????4???,??????A[i][j]??????????????????????????????????
????:
???????????A???M???N(3=M,N=20);??M?,????A????N???????????????
????:
??????? ?? ??????????????,????????1?????????????;??????1??????,???????????????????,???None ??? ????
????1:
4 5
1 1 1 1 1
1 3 9 3 1
1 5 3 5 1
1 1 1 1 1
????1:
9 2 3
5 3 2
5 3 4
????2:
3 5
1 1 1 1 1
9 3 9 9 1
1 5 3 5 1
????2:
None 3 5
*/
#include<stdio.h>
int main(){
int m,n;
scanf("%d %d",&m,&n);
int a[100][100];
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&A[i][j]);
}
}
for(i=2;i<m;i++)
{
for(j=2;j<n;j++)
{
if(A[i][j]>A[i+1][j]&&A[i][j]>A[i][j+1]&&A[i][j]>A[i-1][j]&&A[i][j]>A[i][j-1])
{
cnt++;
printf("%d %d %d\n",A[i][j],i,j);
}
}
}
if(cnt==0)
printf("None %d %d",m,n);
return 0;
}

View file

@ -0,0 +1,43 @@
/*
7-2 ?????????
?? 10
?? C???
?? ????
????????,??????m×n?????????
????:
????????????m?n(1=m,n=6)???m?,????n???,??
??????
????:
??????????????
????:
3 2
6 3
1 -8
3 12
????:
9
-7
15
*/
#include<stdio.h>
int main(){
int m,n,i,j,sum=0;
scanf("%d %d",&m,&n);
int A[100][100];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
sum+=A[i][j];
}
printf("%d\n",sum);
sum=0;
}
return 0;
}

View file

@ -0,0 +1,58 @@
/*
7-3 ??????
?? 10
?? ???
?? ????
?????????????N??????
????:
????????N(1=N=10)?
????:
???????????N?????????????4??
????:
6
????:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
*/
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
int a[100][100],cnt=0;
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(j==0||j==i)
{
a[i][j]=1;
}
else
{
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
printf("W");
}
for(j=0;j<=i;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
return 0;
}

View file

@ -0,0 +1,49 @@
/*
7-4 ????
?? 10
?? C???
?? ????
????n×n???,??????????????????????????????????????????????????????
????:
??????????n(1<n=10);??n?,????n???,????????
????:
??????????????????????????????????
????:
4
2 3 4 1
5 6 1 1
7 1 8 1
1 1 1 1
????:
35
*/
#include<stdio.h>
int main()
{
int n,i,j,sum=0;
scanf("%d",&n);
int A[100][100];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(!((i==i&j==n-i-1)||i==n-1||j==n-1))
{
sum+=A[i][j];
}
}
}
printf("%d",sum);
return 0;
}

View file

@ -0,0 +1 @@
我永远喜欢朱森森030

View file

@ -0,0 +1,73 @@
//删除重复字符
//本题要求编写程序将给定字符串去掉重复的字符后按照字符ASCII码顺序从小到大排序后输出。
//输入格式:
//输入是一个以回车结束的非空字符串少于80个字符
//输出格式:
//输出去重排序后的结果字符串。
//输入样例:
//ad2f3adjfeainzzzv
//输出样例:
//23adefijnvz
#include <stdio.h>
#include <string.h>
#define SIZE 81
void delete_repeat(char *str);
void bubble_sort(char *str);
int main()
{
char str[SIZE];
gets(str);
delete_repeat(str);
bubble_sort(str);
puts(str);
return 0;
}
//删除重复字符
void delete_repeat(char *str)
{
/**判断数组内第i个字符是否与前i-1个字符存在重复 若重复删除该字符i之后的字符下标均减1后重新判断新的第i个字符 若不重复,不做处理,继续遍历下一个 */
for(int i=1;str[i]!='\0';i++){
for(int j=0;j<i;j++)
{
if(str[i]==str[j])
{
for(int k=i;k<strlen(str)-1;k++)
{
str[k]=str[k+1];
}
str[strlen(str)-1]='\0';
i--;
}
}
}
}
//冒泡排序
void bubble_sort(char *str)
{
int swap;
char temp;
int k=strlen(str);
do{
swap =0;
for(int i=0;i<k-1;i++){
if(str[i]>str[i+1])
{
swap=1;
temp=str[i];
str[i]=str[i+1];
str[i+1]=temp;
}
}
k--;
}while(k>0&&swap);
}

View file

@ -0,0 +1,38 @@
//判断回文
//输入一个以回车符为结束标志的字符串少于80个字符判断该字符串是否为回文。
//回文就是字符串中心对称如“abcba”、“abccba”是回文“abcdba”不是回文。
//输入格式:
//输入一个以回车符为结束标志的字符串少于80个字符
//输出格式:
//为回文输出yes; 非回文输出no注意输出的结果后面有回车符
//输入样例:
//abccba
//输出样例:
//yes
#include<stdio.h>
#include<string.h>
int main()
{
int n,i;
char line[80];
gets(line);
n=strlen(line);
for(i=0;i<n/2;i++)
{
if(line[i]!=line[n-1-i])
break;
}
if(i>=n/2)
printf("yes\n");
else
printf("no\n");
return 0;
}

View file

@ -0,0 +1,96 @@
//判断回文字符串
//回文就是字符串中心对称,从左向右读和从右向左读的内容是一样的。
//输入一个字符串,判断该字符串是否为回文,只考虑数字和字母字符,字母的大小写没有区别。
//输入格式:
//输入一个字符串。
//输出格式:
//是回文一行输出yes,否则输出no。
//输入样例:
//在这里给出一组输入。例如:
//A man,a plan; cnalPanama
//输出样例:
//在这里给出相应的输出。例如:
//yes
#include<stdio.h>
#include<string.h>
void tran(char a[]);//大小写转换
void del(char *str);//删除符号
void Copy_string(char *str1, char *str2);
void reverse(char *a);
//复制字符串
void Copy_string(char* str1, char* str2)
{
int i = 0;
while (str1[i] != 0)
{
str2[i] = str1[i];
i++;
}
str2[i] = '\0';//添加字符串结束符
}
//将大写字母转换为小写字母
void tran(char* a)
{
int i;
for (i = 0; a[i] != '\0'; i++)
if (a[i] >= 'A' && a[i] <= 'Z'){
a[i] += 32;
}
a[i]='\0';
}
//移除空格
void del(char str[]){
int i=0,j=0;
char ch;
ch=str[i];
while(ch!='\0'){
if((ch>='a'&&ch<='z')||ch>='A'&&ch<='Z'){
str[j]=ch;
j++;
}
i++;
ch=str[i];
}
str[j]='\0';
}
//倒序排列字符串
void reverse(char *s){
char *end = s;
char temp;
while(*end)
end++;
end--;
for(; s<end; s++, end--){
temp = *s;
*s = *end;
*end = temp;
}
}
int main(){
char str[100],str1[100],str2[100];
gets(str);
tran(str);
del(str);
Copy_string(str,str1);
reverse(str);
if(strcmp(str1,str)==0)
printf("yes");
else
printf("no");
return 0;
}

View file

@ -0,0 +1,73 @@
//十六进制字符串转换成十进制非负整数
//输入一个以#结束的字符串,滤去所有的非十六进制字符(不分大小写),组成一个新的表示十六进制数字的字符串,输出该字符串并将其转换为十进制数后输出。
//输入格式:
//输入在一行中给出一个不超过80个字符长度的、以#结束的非空字符串。
//输出格式:
//第1行中输出已滤去所有非十六进制字符的字符串。
//在第2行中输出转换后的十进制数。题目保证输出结果在长整型范围内。
//输入样例:
//-zy1+Ak0-bq?#
//输出样例:
//1A0b
//6667
#include <stdio.h>
#include <string.h>
#include<math.h>
int num(char s);
int main()
{
char a[81],b[81]; //数组b存放过滤后的字符串
int d,i,j,k;
i=0;
while((d=getchar())!='#'){
a[i]=(char)d;
i++;
}
a[i]='\0';
j=i,k=0;
for(i=0;i<j;i++){
if((a[i]>='0'&&a[i]<='9')||(a[i]>='a'&&a[i]<='f')||(a[i]>='A'&&a[i]<='F')){
b[k]=a[i];
k++;
}
}
for(i=0;i<k;i++){
printf("%c",b[i]);
}
int sum=0;
for(i=k-1,j=0;i>=0;i--,j++){ //i 控制幂次 j控制位数 因为 f41 第一位的要乘的数的幂次为f*16的2次方
if(b[j]>='0'&&b[j]<='9'){
sum=sum+(b[j]-48)*pow(16,i); //(b[j]-48) 将字符转化为相对应的数字;
}else{
sum=sum+num(b[j])*pow(16,i);
}
}
printf("\n%d",sum);
return 0;
}
int num(char s)
{
if(s == 'a'||s == 'A')
return 10;
if(s == 'b'||s == 'B')
return 11;
if(s == 'c'||s == 'C')
return 12;
if(s == 'd'||s == 'D')
return 13;
if(s == 'e'||s == 'E')
return 14;
if(s == 'f'||s == 'F')
return 15;
}

View file

@ -0,0 +1,34 @@
//字符串字母大小写转换
//本题要求编写程序,对一个以“#”结束的字符串,将其小写字母全部转换成大写字母,把大写字母全部转换成小写字母,其他字符不变输出。
//输入格式:
//输入为一个以“#”结束的字符串不超过30个字符
//输出格式:
//在一行中输出大小写转换后的结果字符串。
///输入样例:
//Hello World! 123#
//输出样例:
//hELLO wORLD! 123
#include<stdio.h>
int main()
{
char c;
while((c=getchar())!='#')//当输入的不是‘# 则进入循环
{
if(c>='A'&&c<='Z')//如果输入是大写
{
c+=32;
printf("%c",c);
}
else if(c>='a'&&c<='z')//如果输入是小写
{
c-=32;
printf("%c",c);
}
else//输入是其他
printf("%c",c);
}
return 0;
}

View file

@ -0,0 +1,40 @@
//字符串替换
//本题要求编写程序,将给定字符串中的大写英文字母按以下对应规则替换:
//原字母 对应字母
//A Z
//B Y
//C X
//D W
//… …
//X C
//Y B
//Z A
//输入格式:
//输入在一行中给出一个不超过80个字符、并以回车结束的字符串。
//输出格式:
//输出在一行中给出替换完成后的字符串。
//输入样例:
//Only the 11 CAPItaL LeTtERS are replaced.
//输出样例:
//Lnly the 11 XZKRtaO OeGtVIH are replaced.
#include <stdio.h>
int main()
{
char str[81];
gets(str);
for( int i=0; i<81; i++ )
{
if( str[i]>='A' && str[i]<='Z' )
str[i] += 25-2*(str[i]-65);
printf("%c", str[i]);
}
return 0;
}

View file

@ -0,0 +1,83 @@
//字符串转换成十进制整数
//输入一个以#结束的字符串,本题要求滤去所有的非十六进制字符(不分大小写),组成一个新的表示十六进制数字的字符串,然后将其转换为十进制数后输出。如果在第一个十六进制字符之前存在字符“-”,则代表该数是负数。
//输入格式:
//输入在一行中给出一个以#结束的非空字符串。
//输出格式:
//在一行中输出转换后的十进制数。题目保证输出在长整型范围内。
//输入样例:
//+-P-xf4+-1!#
//输出样例:
//-3905
#include <stdio.h>
#include <string.h>
#include<math.h>
int num(char s);
int main()
{
char a[800],b[800];
int d,i,j,k;
i=0;
while((d=getchar())!='#'){
a[i]=(char)d;
i++;
}
a[i]='\0';
int p;
p=i;
k=0;
for(i=0;i<p;i++){
if((a[i]>='0'&&a[i]<='9')||(a[i]>='a'&&a[i]<='f')||(a[i]>='A'&&a[i]<='F')||a[i]=='-'){
b[k]=a[i];
k++;
}
}
int flag=1;
if(b[0]=='-') flag=-1;
k=0;
for(i=0;i<p;i++){
if((a[i]>='0'&&a[i]<='9')||(a[i]>='a'&&a[i]<='f')||(a[i]>='A'&&a[i]<='F')){
b[k]=a[i];
k++;
}
}
int sum=0;
for(i=k-1,j=0;i>=0;i--,j++){
if(b[j]>='0'&&b[j]<='9'){
sum=sum+(b[j]-48)*pow(16,i);
}else{
sum=sum+num(b[j])*pow(16,i);
}
}
printf("%d",sum*flag);
return 0;
}
int num(char s)
{
if(s == 'a'||s == 'A')
return 10;
if(s == 'b'||s == 'B')
return 11;
if(s == 'c'||s == 'C')
return 12;
if(s == 'd'||s == 'D')
return 13;
if(s == 'e'||s == 'E')
return 14;
if(s == 'f'||s == 'F')
return 15;
}

View file

@ -0,0 +1,39 @@
//字符串输入练习 (III)
//输出字符串的子串。
//输入格式:
//每行的开始是一个正整数k,然后是一个字符串sk和s之间用空格分开。(k大于0且小于等于s的长度请在这里写输入格式。例如输入在一行中给出2个绝对值不超过1000的整数A和B。
//输出格式:
//输出从头开始的长度为k的子串。
//输入样例:
//2 hello world!
//输出样例:
//he
#include<stdio.h>
int main()
{
int k;
scanf("%d",&k);
char s[1000];
getchar();
gets(s);
int i;
for(i=0;i<k;i++)
{
printf("%c",s[i]);
}
return 0;
}

View file

@ -0,0 +1,34 @@
//字符串逆序
//输入一个字符串,对该字符串进行逆序,输出逆序后的字符串。
//输入格式:
//输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。
//输出格式:
//在一行中输出逆序后的字符串。
//输入样例:
//Hello World!
//输出样例:
//!dlroW olleH
#include<stdio.h>
#include<string.h>
int main()
{
char str[81];
gets(str);
int n=strlen(str);
int i;
char temp;
for(i=0;i<(n/2);i++){
temp=str[i];
str[i]=str[n-i-1];
str[n-i-1]=temp;
}
printf("%s\n",str);
return 0;
}

View file

@ -0,0 +1,59 @@
//字符转换
//本题要求提取一个字符串中的所有数字字符('0'……'9'),将其转换为一个整数输出。
//输入格式:
//输入在一行中给出一个不超过80个字符且以回车结束的字符串。
//输出格式:
//在一行中输出转换后的整数。题目保证输出不超过长整型范围。
//输入样例:
//free82jeep5
//输出样例:
//825
#include<stdio.h>
int main()
{
char arr[80];
char b[80];
int n = 0;
int len = 0;
while (1)
{
scanf("%c", &arr[len]);
if (arr[len] == '\n')
{
break;
}
len++;
}
//将数字备份到字符串b中
for (int i = 0; i < len; ++i)
{
if (arr[i] >= '0' && arr[i] <= '9')
{
b[n] = arr[i];
n++;//记录b的个数
}
}
int sum = 0;
//最大的坑
//本题要求提取一个字符串中的所有数字字符('0'……'9'),将其转换为一个整数输出。
for (int i = 0; i < n; ++i)
{
//输出最好按题目要求
if (i == 0)
{
sum = b[i] - '0';
}
//字符串转数字要-'0'
else
{
sum = sum * 10 + b[i] - '0';
}
}
printf("%d",sum);
return 0;
}

View file

@ -0,0 +1,25 @@
//宇宙无敌大招呼
//据说所有程序员学习的第一个程序都是在屏幕上输出一句“Hello World”跟这个世界打个招呼。作为天梯赛中的程序员你写的程序得高级一点要能跟任意指定的星球打招呼。
//输入格式:
//输入在第一行给出一个星球的名字S是一个由不超过7个英文字母组成的单词以回车结束。
//输出格式:
//在一行中输出Hello S跟输入的S星球打个招呼。
//输入样例:
//Mars
//输出样例:
//Hello Mars
#include<stdio.h>
int main()
{
char s[8]="\0";
scanf("%s",s);
printf("Hello %s",s);
return 0;
}

View file

@ -0,0 +1,48 @@
//查找指定字符
//本题要求编写程序,从给定字符串中查找某指定的字符。
//输入格式:
//输入的第一行是一个待查找的字符。第二行是一个以回车结束的非空字符串不超过80个字符
//输出格式:
//如果找到在一行内按照格式“index = 下标”输出该字符在字符串中所对应的最大下标下标从0开始否则输出"Not Found"。
//输入样例1
//m
//programming
//输出样例1
//index = 7
//输入样例2
//a
//1234
//输出样例2
//Not Found
#include<stdio.h>
#include<string.h>
int main(){
int i,count=0,index;
char c;
scanf("%c\n",&c);
char str[81];
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==c)
{
index=i;
count++;
}
}
if(count!=0)
printf("index = %d\n",index);
else if(count==0)
printf("Not Found");
return 0;
}

View file

@ -0,0 +1,29 @@
//统计大写辅音字母
//英文辅音字母是除A、E、I、O、U以外的字母。本题要求编写程序统计给定字符串中大写辅音字母的个数。
//输入格式:
//输入在一行中给出一个不超过80个字符、并以回车结束的字符串。
//输出格式:
//输出在一行中给出字符串中大写辅音字母的个数。
//输入样例:
//HELLO World!
//输出样例:
//4
#include<stdio.h>
#include<string.h>
int main(){
int i,count=0;
char str[81];
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]!='A'&&str[i]!='E'&&str[i]!='I'&&str[i]!='O'&&str[i]!='U'&&str[i]>'A'&&str[i]<='Z')
count++;
}
printf("%d\n",count);
return 0;
}

View file

@ -0,0 +1,31 @@
//统计字符串中数字字符的个数
//输入一个字符串,统计其中数字字符('0'……'9')的个数。
//输入格式:
//输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。
//输出格式:
//输出所统计的数字字符的个数。
//输入样例:
//It's 512?
//输出样例:
//3
#include<stdio.h>
#include<string.h>
int main(){
int i,count=0;
char str[81];
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='0'&&str[i]<='9')
count++;
}
printf("%d\n",count);
return 0;
}

View file

@ -0,0 +1,34 @@
//统计字符出现次数
//本题要求编写程序,统计并输出某给定字符在给定字符串中出现的次数。
//输入格式:
//输入第一行给出一个以回车结束的字符串少于80个字符第二行输入一个字符。
//输出格式:
//在一行中输出给定字符在给定字符串中出现的次数。
//输入样例:
//programming is More fun!
//m
//输出样例:
//2
#include<stdio.h>
#include<string.h>
int main(){
int i,count=0;
char str[81];
gets(str);
char c;
scanf("%c",&c);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==c)
count++;
}
printf("%d\n",count);
return 0;
}

View file

@ -0,0 +1,45 @@
//英文字母替换加密(大小写转换+后移1位
//本题要求编写程序将英文字母替换加密。为了防止信息被别人轻易窃取需要把电码明文通过加密方式变换成为密文。变换规则是将明文中的所有英文字母替换为字母表中的后一个字母同时将小写字母转换为大写字母大写字母转换为小写字母。例如字母a->B、b->C、…、z->A、A->b、B->c、…、Z->a。输入一行字符将其中的英文字母按照以上规则转换后输出其他字符按原样输出。
//输入格式:
//输入一行字符,以回车符 '\n'作为 结束符。
//输出格式:
//将输入的一行字符中的所有英文字母替换为字母表中的后一个字母,同时将小写字母转换为大写字母,大写字母转换为小写字母后输出,其他字符按原样输出。
//输入样例:
//在这里给出一组输入。例如:
//Reold Z123?
//输出样例:
//在这里给出相应的输出。例如:
//sFPME a123?
#include <stdio.h>
int main()
{
char c;
while((c=getchar())!='\n')
{
if(c>='a'&&c<='y')
{
c=c-31;
printf("%c",c);
}
else if(c=='z')
printf("A");
else if(c>='A'&&c<='Y')
{
c=c+33;
printf("%c",c);
}
else if(c=='Z')
printf("a");
else
{
printf("%c",c);
}
}
}

View file

@ -0,0 +1,65 @@
//输出大写英文字母
//本题要求编写程序顺序输出给定字符串中所出现过的大写英文字母每个字母只输出一遍若无大写英文字母则输出“Not Found”。
//输入格式:
//输入为一个以回车结束的字符串少于80个字符
//输出格式:
//按照输入的顺序在一行中输出所出现过的大写英文字母每个字母只输出一遍。若无大写英文字母则输出“Not Found”。
//输入样例1
//FONTNAME and FILENAME
//输出样例1
//FONTAMEIL
//输入样例2
//fontname and filrname
//输出样例2
//Not Found
#include <stdio.h>
#include <string.h>
int main()
{
char a[80];
char b[80];
int i,j,count;
int flag=0;
int n;
int d;
gets(a);
d=strlen(a);
for(i=0;i<d;i++)
{
b[i]=a[i];
}
a[i]=0;
count=0;
for(i=0;a[i]!='\0';i++)
{
flag=0;
if(a[i]>='A'&&a[i]<='Z')
{
for(j=0;j<i;j++)
{
if(b[j]==a[i])
{
flag=1;
}
}
if(flag==0)
{
printf("%c",a[i]);
count++;
}
}
}
if(count==0)
{
printf("Not Found");
}
return 0;
}

View file

@ -0,0 +1,12 @@
//ðÅÝÅÅÐò
#include<stdio.h>
void bubble£¨int a[3],int n);//a[]==*a
int main()
{
int n,a[8];
int i;
printf("Enter n(n<=8):");
scanf("%d",&n);
return 0;
}

View file

@ -0,0 +1,43 @@
//判断上三角矩阵
#include<stdio.h>
int main()
{
int T;
scanf("%d", &T);
for(int i=0;i<T;i++)
{
int m;
scanf(" %d", &m);
int mat[m][m];
//输入矩阵
for(int j=0;j<m;j++)
{
for(int n=0;n<m;n++)
{
scanf(" %d", &mat[j][n]);
}
}
int flag=1;//定义标志
for(int x=1;x<m;x++)//判断左下是否有非0
{
for(int y=0;y<x;y++)
{
if(mat[x][y]!=0)
{
flag=0;
break;//有非0跳出
}
}
if(flag==0)
break;
}
if(flag==1)//打印
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

View file

@ -0,0 +1,30 @@
//判断对称矩阵
#include<stdio.h>
int main()
{
int n, a[10][10];
int i, j, sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=a[j][i])
{
printf("No\n");
return 0;
}
}
}
printf("Yes\n");
return 0;
}

View file

@ -0,0 +1,42 @@
//打印杨辉三角
#include<stdio.h>
int main()
{
int arr[11][11];
int n=0;
arr[0][0]=0;
scanf("%d",&n);//输入n
for(int i=0;i<n;i++)//打印,控制外层行数循环
{
for(int k=0;k<n-i-1;k++)
{
printf(" ");//打印空格
}
//打印数据
for(int j=0;j<=i;j++)
{
if(i==j)//画图找规律在二维数组第一行为0行每行第一个最后一个为1i==j换行j=0然后根据杨辉三角规律写中间元素
{
arr[i][j]=1;
}
else if(j==0)
{
arr[i][j]=1;
}
else
{
arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
}
printf("%4d",arr[i][j]);
if(i==j)
{
printf("\n");
}
}
}
return 0;
}

View file

@ -0,0 +1,51 @@
//找鞍点
#include<stdio.h>
#define MAXN 6 //阶数最大声明
int main()
{
int n,i,j,k,i_max,j_min,t=0;
int matrix[MAXN][MAXN];
scanf("%d",&n);//输入阶数
for(i=0;i<n;i++)//输入数据
{
for(j=0;j<n;j++)
{
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<n;i++)
{
i_max=matrix[i][0];
for(j=0;j<n;j++)
{
if(matrix[i][j]>i_max)//找到该行最大数
{
i_max=matrix[i][j];
}
}
for(j=0;j<n;j++)
{
j_min=matrix[0][j];
for(k=0;k<n;k++)
{
if(matrix[k][j]<j_min)//找到该行最小数
{
j_min=matrix[k][j];
}
}
if(matrix[i][j]==i_max&&matrix[i][j]==j_min)//判断鞍点
{
printf("%d %d\n",i,j);
t++;//标识
}
}
}
if(t==0)
{
printf("NONE\n");
}
return 0;
}

View file

@ -0,0 +1,30 @@
//方阵循环右移
#include<stdio.h>
int main()
{
int a[10][10];
int m,n;
int i,j;
scanf("%d %d",&m,&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
m=m%n;//可能m比n大取余数
for(i=0;i<n;i++)
{
for(j=n-m;j<n;j++)//先输出右移的部分
{
printf("%d ",a[i][j]);
}
for(j=0;j<n-m;j++)//再输出余下的部分
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}

View file

@ -0,0 +1,35 @@
//求矩阵各行元素之和
#include<stdio.h>
#include<string.h>
int main()
{
int m, n, i, j, k;
//输入两个正整数: m*n
scanf("%d %d", &m, &n);
int a[m][n];
//记录每行的n个数的和
int b[100]= {0};
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
//每行输出对应矩阵行元素之和
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[k]+=a[i][j];
}
k++;
}
for(i=0;i<m;i++)
{
printf("%d\n",b[i]);
}
return 0;
}

View file

@ -0,0 +1,30 @@
//求矩阵的局部极大值
#include<stdio.h>
int main()
{
int i, j, m, n, a[20][20];
int t=-1;
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=1;i<m-1;i++)
{
for(j=1;j<n-1;j++)
{
if(a[i][j]>a[i-1][j]&&a[i][j]>a[i][j-1]&&a[i][j]>a[i+1][j]&&a[i][j]>a[i][j+1])
{
printf("%d %d %d",a[i][j],i+1,j+1);
printf("\n");
t=1;
}
}
}
if(t==-1)
printf("None %d %d\n",m,n);
return 0;
}

View file

@ -0,0 +1,15 @@
//求矩阵的最大值(设惟一)
#include<stdio.h>
int main(){
int m,n,i,j,im=0,jm=0;
scanf("%d %d",&m,&n);
int a[m][n];
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
if(a[im][jm]<a[i][j]){im=i;jm=j;}
}
}
printf("%d\n%d %d",a[im][jm],im,jm);
return 0;
}

View file

@ -0,0 +1,25 @@
//矩阵转置
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,a[3][3],b[3][3];///定义两个数组,一个是转置前的,另一个是转置后的。
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);///遍历输入,遍历转置
b[j][i]=a[i][j];///转置时行变列,列变行
}
}
for(j=0;j<3;j++)///输出转置后的数列。
{
for(i=0;i<3;i++)
{
printf("%4d",b[j][i]);
}
printf("\n");
}
return 0;
}

View file

@ -0,0 +1,19 @@
//¾ØÕóÔËËã
#include<stdio.h>
int main()
{
int n, a[10][10];
int i, j, sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &a[i][j]);
if(!(i+j==n-1||i==n-1||j==n-1))
sum+=a[i][j];
}
}
printf("%d",sum);
return 0;
}

View file

@ -0,0 +1,27 @@
#include <stdio.h>
int main()
{
int c=1,x=0, y=0,n, a[10][10]={0};
scanf("%d", &n);
a[x][y] = 1;
while(n*n!=c)
{
while(y+1<n && !a[x][y+1] )
a[x][++y] = ++c;
while(x+1<n && !a[x+1][y])
a[++x][y] = ++c;
while(y-1>=0 && !a[x][y-1])
a[x][--y] = ++c;
while(x-1>=0 && !a[x-1][y])
a[--x][y] = ++c;
}
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
{
printf("%3d", a[x][y]);
}
printf("\n");
}
return 0;
}

View file

@ -0,0 +1,18 @@
//½ÇÉ«½»»»
#include<stdio.h>
void swap(int *px,int *py);
int main()
{
int a=1,b=2;
int *pa=&a,*pb=&b;
swap(pa,pb);
printf("After calling swap:a=%d b=%d\n",a,b);
return 0;
}
void swap(int *px,int *py)
{
int t;
t=*px;
*px=*py;
*py=t;
}

View file

@ -0,0 +1,18 @@
//计算天数
#include<stdio.h>
int main()
{
int i, year, month, day, n=0;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
scanf("%d/%d/%d",&year,&month,&day);
if((year%4==0&&year%100)||year%400==0)
{
a[2]=29;
}
for(i=0;i<=month-1;i++)
{
n+=a[i];
}
printf("%d",n+day);
return 0;
}

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,16 @@
//: gui/HelloLabel.java
import javax.swing.*;
import java.util.concurrent.*;
public class HelloLabel {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Hello Swing");;
JLabel label = new JLabel("A Label");
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);
TimeUnit.SECONDS.sleep(1);
label.setText("Hey! This is Different!");
}
}///:~

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,11 @@
//: gui/HelloSwing.java
import javax.swing.*;
public class HelloSwing {
public static void main(String[] args){
JFrame frame = new JFrame("Hello Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);
}
}

View file

@ -0,0 +1,649 @@
//2023.06.28 Java期末复习课程
1.写出程序运行结果
class A{
public A(){
System.out.println("Hello!");
}
public A(String s){
this();
System.out.println("My name is"+s);
}
}
class B extends A{
public static void main(String[] args){
B b=new B("Amy");
}
public B(){
System.out.println("I am Amy.");
}
public B(String s){
super(s);
System.out.println("How are you!");
}
}
程序运行结果如下:
Hello!
My name is Amy
How are you!
首先程序从B类的主方法开始执行。在主方法中创建了一个B类的对象b并调用了带有一个String类型参数的构造方法参数为"Amy"。由于B类继承自A类因此构造B类对象时会先调用父类A的构造方法。
在A类的构造方法中首先打印输出"Hello!"。接着B类的构造方法调用了父类A的带有一个String类型参数的构造方法这里传入了"Amy"作为参数。在A类的带有一个String类型参数的构造方法中先调用了A类的无参构造方法this()),所以又会打印输出"Hello!"。然后,打印输出"My name is Amy"。
接着回到B类的构造方法继续执行后续的代码。在B类的构造方法中打印输出"I am Amy."。最后,打印输出"How are you!"。
因此,程序运行结果为:
Hello!
My name is Amy
How are you!
*******************************************************************************************************
2.下列try catch语句块中的第二个语句S1将引起一个异常试回答下列问题。
try{
S1;
S2;
}catch(ExceptionType1 e){
}catch(ExceptionType2 e){
}finally{
S3;
}
S4
(1)S2会执行吗?
(2)如果异常未被捕获S3会被执行吗?S4会被执行吗?
(3)如果catch子句捕获了异常S3会被执行吗?S4会被执行吗?
(1) S2不会执行。由于S1引发了异常程序将会跳转到catch子句而不会继续执行后面的语句。
(2) 如果异常未被捕获S3会被执行。无论是否发生异常finally块中的代码都会被执行。但是S4不会被执行因为异常发生后程序会跳出try-catch-finally语句块。
(3) 如果catch子句捕获了异常S3会被执行。无论是否发生异常finally块中的代码都会被执行。S4也会被执行因为异常已经被处理并且程序会继续执行后面的语句。
如果异常未被捕获程序会跳转到catch子句之前的位置不会执行后面的语句但finally块中的代码仍会被执行。
如果catch子句捕获了异常程序会继续执行catch子句之后的语句包括finally块中的代码。
*******************************************************************************************************
3.下列代码简单模拟了多个窗口购买火车票会出现下图问题请问代码如何修改提示synchronized
class BuyTicket implements Runnable{
int ticketnum=10;//共有十张宁波到北京的火车票
public void run{
for(int i=1;i<=20;i++){//每个窗口有20人在排队买票
if(ticketnum>0)//票数大于0买票
System.out.println(Thread.currentThread().getName()+"买到从宁波到北京的第"+ticketnum--+"张车票")
}
}
}
public class Test{
public static void main(String[] args){
BuyTicket t=new BuyTicket();
Thread t1=new Thread(t,"1号窗口");
t1.start();
Thread t2=new Thread(t,"2号窗口");
t2.start();
Thread t3=new Thread(t,"3号窗口");
t3.start();
}
}
问题出在多个线程同时访问ticketnum变量时可能会出现竞争条件导致数据不一致。为了解决这个问题可以使用synchronized关键字来保证多个线程访问ticketnum变量时的同步性。
修改后的代码如下:
class BuyTicket implements Runnable{
private int ticketnum=10;//共有十张宁波到北京的火车票
public synchronized void run(){
for(int i=1;i<=20;i++){//每个窗口有20人在排队买票
if(ticketnum>0){//票数大于0买票
System.out.println(Thread.currentThread().getName()+"买到从宁波到北京的第"+ticketnum--+"张车票");
}
}
}
}
public class Test{
public static void main(String[] args){
BuyTicket t=new BuyTicket();
Thread t1=new Thread(t,"1号窗口");
t1.start();
Thread t2=new Thread(t,"2号窗口");
t2.start();
Thread t3=new Thread(t,"3号窗口");
t3.start();
}
}
在BuyTicket类的run方法上加上synchronized关键字这样就可以保证在多线程环境下只有一个线程能够访问run方法从而避免了多个线程同时修改ticketnum的问题。这样可以保证每个窗口买票时的操作是互斥的不会出现问题。
在BuyTicket类的run方法上添加了synchronized关键字这样每个窗口在执行run方法时都会获得对象锁保证了同一时间只有一个窗口在买票。这样就避免了多个窗口同时买同一张票的问题。
*******************************************************************************************************
4.阅读以下代码片段,回答问题
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class EventTest extends JFrame implements ActionListener/*a*/{
JButton btn1,btn2;Container ctpn;
EventTest(){
ctpn=this.getContentPane();//b
btn1=new JButton("Blue");
btn2=new JButton("Red");
btn1.addActionListener(this);//c
btn2.addActionListener(this);
this.setTitle("Action Event");setSize(200,150);
this.setLayout(new FlowLayout(FlowLayout.CENTER)); //d
this.ctpn.add(btn1);ctpn.add(btn2);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
JButton btn=(JButton)e.getSource();//e
if(btn==btn1) ctpn.setBackground(Color.blue);
if(btn==btn2)
ctpn.setBackground(Color.red);
}
public static void main(String args[]){
EventTest frm=new EventTest();
}
}
(1)在"//"后添加注释
(2)画出程序的运行效果,并说程序完成的功能
(1) 在"//"后添加注释:
import java.awt.event.*; // 导入事件相关的包
import javax.swing.*; // 导入Swing相关的包
import java.awt.*; // 导入AWT相关的包
class EventTest extends JFrame implements ActionListener{ //a 继承JFrame类并实现ActionListener接口,ActionListener接口是一个事件监听器接口用于处理按钮点击事件
JButton btn1,btn2; // 创建两个按钮对象
Container ctpn; // 创建一个容器对象
EventTest(){
ctpn=this.getContentPane();//b getContentPane()方法用于获取容器的内容面板。
btn1=new JButton("Blue"); // 创建名为"Blue"的按钮
btn2=new JButton("Red"); // 创建名为"Red"的按钮
btn1.addActionListener(this); //c 为按钮添加事件监听器,addActionListener(this)方法将当前类实例作为按钮的事件监听器
btn2.addActionListener(this); //c 为按钮添加事件监听器
this.setTitle("Action Event"); // 设置窗口标题为"Action Event"
setSize(200,150); // 设置窗口大小为200x150像素
this.setLayout(new FlowLayout(FlowLayout.CENTER)); //d 设置布局为居中对齐的流式布局
this.ctpn.add(btn1); // 将"Blue"按钮添加到容器中
ctpn.add(btn2); // 将"Red"按钮添加到容器中
this.setVisible(true); // 设置窗口可见
}
public void actionPerformed(ActionEvent e){
JButton btn=(JButton)e.getSource(); //e 获取触发事件的按钮对象
if(btn==btn1) ctpn.setBackground(Color.blue); // 如果是"Blue"按钮触发的事件,将容器背景设置为蓝色
if(btn==btn2) ctpn.setBackground(Color.red); // 如果是"Red"按钮触发的事件,将容器背景设置为红色
}
public static void main(String args[]){
EventTest frm=new EventTest(); // 创建EventTest对象
}
}
(2) 程序的运行效果:
程序创建了一个窗口,窗口标题为"Action Event"大小为200x150像素。窗口中有两个按钮一个是"Blue"按钮,另一个是"Red"按钮。当点击"Blue"按钮时,窗口的背景颜色会变为蓝色;当点击"Red"按钮时,窗口的背景颜色会变为红色。
程序完成的功能是该程序演示了如何使用Java的事件处理机制通过按钮的事件监听器实现了当点击不同的按钮时改变窗口的背景颜色。
5.下列代码完成了邮箱系统的登陆功能,请在注释位置编写相应代码写出运行结果。相关信息和Java帮助如下:
User表id username password
1 zhangsan 123
2 lisi 234
数据库操作类已实现,大纲视图如下:
JDBCUtils
url:String
user:String
password:String
driver:String
{...}
getConnection():Connection
close(Statement,Connection):void
close(ResultSet,Statement,Connection):void
接口Connection下的方法
Statement createStatement() 创建一个Statement对象来将SQL语句发送到数据库
接口Statement下的方法
ResultSet executeQuery(String sql) 执行给定的SQL语句该语句返回单个ResultSet对象。
接口ResultSet下的方法
boolean next() 将光标从当前位置先前移一行。
import java.sql.*;
public class LoginTest{
public static void main(String[] args){
String username="lisi";
String pasasword="123";
boolean flag=new LoginTest().login(username,password);
if(flag) System.out.println("登陆成功");
else System.out.println("用户名或密码错误");
}
public boolean login(String username,String password){
if(username==null||password==null){
return false;
}
Connection conn=null;PreparedStatement pstmt=null;ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
//添加数据库操作代码
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBCUtils.close(rs,pstmt,conn);
}
return false;
}
}
answer:
pstmt = conn.prepareStatement("SELECT * FROM User WHERE username = ? AND password = ?");
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()){
return true; // 用户名和密码匹配,登录成功
}
import java.sql.*;
public class LoginTest{
public static void main(String[] args){
String username="lisi";
String password="123";
boolean flag=new LoginTest().login(username,password);
if(flag) System.out.println("登陆成功");
else System.out.println("用户名或密码错误");
}
public boolean login(String username,String password){
if(username==null||password==null){
return false;
}
Connection conn=null;PreparedStatement pstmt=null;ResultSet rs=null;
try{
conn=JDBCUtils.getConnection();
pstmt = conn.prepareStatement("SELECT * FROM User WHERE username = ? AND password = ?");
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()){
return true;
}
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBCUtils.close(rs,pstmt,conn);
}
return false;
}
}
运行结果:
用户名或密码错误
程序通过调用login方法进行登录验证传入用户名和密码参数。在login方法中首先判断用户名和密码是否为空如果为空则直接返回false。然后获取数据库连接通过PreparedStatement对象执行SQL语句查询用户表使用占位符设置用户名和密码参数。如果查询结果存在则返回true表示登录成功否则返回false表示用户名或密码错误。
在main方法中调用login方法并根据返回结果输出相应的提示信息。在本例中用户名为"lisi",密码为"123",所以输出结果为"用户名或密码错误"。
6.下面有两个Java源文件(Lute.java和Music.java),阅读程序,回答问题。
a:Lute.java
package mypack;
interface Instrument{//乐器
int NUM=5;
void play();
String what();
void adjust();
}
class Wind implements Instrument{//管乐器
public void play(){
System.out.println("Wind.play()");
}
public String what() {return "Wind";}
public void adjust() {}
}
class Stringed{//有弦乐器
int StringNum;
Stringed(int num){
this.StringNum=num;
}
}
public class Lute extends Stringed implements Instruments{//古琵琶
public Lute(int num){
super(num);
}
public void play(){
System.out.println("lute.play()");
}
public String what() {return "lute"};
public void adjust() {}
}
b.Music.java
import mypack.*
public class Music{
public static void main(String[] args){
Lute test=new Lute(6);
test.play();
}
}
(1)以上代码分别有哪些接口、类,在哪些包中,有公共类吗?
(2)类Wind中的adjust()方法是抽象方法吗?能省略吗?为什么?
(3)类Lute和类Stringed是什么关系Lute类中有成员变量吗
(4)类Lute中的方法是public修饰符能省略吗为什么
(5)请写出程序的运行结果。
(1)以上代码接口有"Instrument",类有'Wind','Stringed','Lute','Music',都在'mypack'包中。
在'Lute.java'中有一个公共类'Lute'。在'Music.java'文件中有一个公共类'Music'。
(2)类Wind中的adjust()方法不是抽象方法。它不能省略。如果省略adjust()方法的实现则Wind类必须声明为抽象类。
(3)类Lute是类Stringed的子类。它们之间是继承关系。Lute类中有一个名为StringNum的成员变量。
(4) 类Lute中的方法的修饰符是public可以省略。这是因为Lute类实现了Instrument接口在接口中的所有方法都是公共的因此在实现接口方法时默认的访问修饰符是public。
(5) 程序的运行结果是lute.play()
二.程序设计题
1.“具有报警功能的移门”针对上述描述请用类、接口等面向对象知识进行设计实现方法中不必写出具体实现代码用System.out.println()。
根据描述,我们可以设计以下类和接口来实现具有报警功能的移门:
1. 接口Alarm
- 方法void alarm(),用于触发报警功能。
2. 类Door
- 属性isOpen表示门的状态true表示门开着false表示门关着
- 方法void open(),用于打开门
- 方法void close(),用于关闭门
- 方法void alarm()实现Alarm接口中的方法用于触发报警功能
- 方法void move(),用于移动门的位置
- 方法void displayStatus(),用于显示门的当前状态
设计实现如下:
interface Alarm {
void alarm();
}
class Door implements Alarm {
private boolean isOpen;
public void open() {
isOpen = true;
System.out.println("门已打开");
}
public void close() {
isOpen = false;
System.out.println("门已关闭");
}
public void alarm() {
System.out.println("门正在报警");
}
public void move() {
System.out.println("门正在移动");
}
public void displayStatus() {
System.out.println("门的状态:" + (isOpen ? "开着" : "关着"));
}
}
这样设计的类和接口可以实现具有报警功能的移门。Door类实现了Alarm接口并实现了alarm()方法用于触发报警功能。同时Door类还具有打开门、关闭门、移动门、显示门状态的功能。
2.数据可视化在统计报表有重要应用,柱状图以及折线图等图形是以不同视角展示数据。假设对于同一份二维数据,系统要求根据不同情景,展示不同图形(柱形图或折线图),且数据变化,图形自动变化(刷新),而且,需要预留接口,以方便需求升级,如同一份数据还支持饼图。为了满足以上需求,请设计类系,并写一个测试类,进行模拟。具体要求:
(a) 设计一个数据类,且有一份订阅者列表,可具备对订阅者(图形)列表的管理功能(删除与增加),当数据变化时,自动触发订阅者更新图形。
(b) 设计一个图形的抽象类,具备根据数据绘制图形的功能,并具备订阅数据和取消订阅的功能。
(c) 写一个测试类,模拟上述过程。
备注方法中不必写出具体实现代码用System.out.println()说明即可。
根据需求,我们可以设计以下类来满足要求:
1. 数据类Data
- 属性subscribers订阅者列表
- 方法addSubscriber(Subscriber subscriber),用于添加订阅者
- 方法removeSubscriber(Subscriber subscriber),用于移除订阅者
- 方法updateData(),用于更新数据并通知订阅者
2. 抽象类Graph
- 属性data数据对象
- 方法subscribe(),用于订阅数据
- 方法unsubscribe(),用于取消订阅数据
- 抽象方法draw(),用于根据数据绘制图形
3. 具体图形类BarGraph柱状图和LineGraph折线图
- 方法draw()实现Graph抽象类中的draw()方法,根据数据绘制柱状图或折线图
设计实现如下:
import java.util.ArrayList;
import java.util.List;
// 数据类
class Data {
private List subscribers;
public Data() {
subscribers = new ArrayList<>();
}
public void addSubscriber(Graph subscriber) {
subscribers.add(subscriber);
}
public void removeSubscriber(Graph subscriber) {
subscribers.remove(subscriber);
}
public void updateData() {
// 更新数据
System.out.println("数据更新");
// 通知订阅者更新图形
for (Graph subscriber : subscribers) {
subscriber.draw();
}
}
}
// 抽象图形类
abstract class Graph {
protected Data data;
public Graph(Data data) {
this.data = data;
}
public void subscribe() {
data.addSubscriber(this);
}
public void unsubscribe() {
data.removeSubscriber(this);
}
public abstract void draw();
}
// 具体图形类:柱状图
class BarGraph extends Graph {
public BarGraph(Data data) {
super(data);
}
public void draw() {
System.out.println("绘制柱状图");
}
}
// 具体图形类:折线图
class LineGraph extends Graph {
public LineGraph(Data data) {
super(data);
}
public void draw() {
System.out.println("绘制折线图");
}
}
// 测试类
public class Test {
public static void main(String[] args) {
// 创建数据对象
Data data = new Data();
// 创建柱状图和折线图
Graph barGraph = new BarGraph(data);
Graph lineGraph = new LineGraph(data);
// 订阅数据
barGraph.subscribe();
lineGraph.subscribe();
// 更新数据,图形自动变化
data.updateData();
// 取消订阅折线图
lineGraph.unsubscribe();
// 更新数据,只有柱状图会变化
data.updateData();
}
}
根据需求设计了数据类Data、抽象图形类Graph以及具体图形类BarGraph和LineGraph。测试类Test中模拟了订阅数据、更新数据以及取消订阅的过程。
测试类中先创建了数据对象Data然后创建了柱状图BarGraph和折线图LineGraph。接着订阅了数据然后更新数据可以看到柱状图和折线图都会自动绘制图
形。最后取消了对折线图的订阅,再次更新数据时,只有柱状图会变化。
3.当前,在我国,不同的企业类型有不同的税收计算方法,假设有高新企业、外资企业、普通企业。现有税收系统,可计算每一类企业的收税,也可自动计算某个地区(如鄞州区)的税收,一般地,某个地区的企业都有不同类型的企业存在。为了满足以上需求,请设计类系,并写一个测试类,进行模拟。具体要求:
(a) 设计一抽象类:企业,具备计算税收的功能。
(b) 高新企业、外资企业、普通企业应该是企业的子类,都具有计算税收的功能。
(c) 设计一个地区类,它是由不同类型的企业构成,具备计算地区税收的功能。
(d) 写一个测试类,进行模拟。
备注方法中不必写出具体实现代码用System.out.println(),或者直接返回一个值进行说明即可。提示:抽象类、方法重写、泛型等的应用。
根据需求,我们可以设计以下类来满足要求:
1. 抽象类Enterprise企业
- 方法calculateTax(),用于计算税收
2. 子类HighTechEnterprise高新企业、ForeignEnterprise外资企业、NormalEnterprise普通企业
- 方法calculateTax(),重写父类的计算税收方法
3. 类Region地区
- 属性enterprises不同类型的企业列表
- 方法addEnterprise(Enterprise enterprise),用于添加企业
- 方法removeEnterprise(Enterprise enterprise),用于移除企业
- 方法calculateRegionTax(),用于计算地区税收
设计实现如下:
abstract class Enterprise {
public abstract double calculateTax();
}
class HighTechEnterprise extends Enterprise {
public double calculateTax() {
System.out.println("计算高新企业的税收");
return 0.0;
}
}
class ForeignEnterprise extends Enterprise {
public double calculateTax() {
System.out.println("计算外资企业的税收");
return 0.0;
}
}
class NormalEnterprise extends Enterprise {
public double calculateTax() {
System.out.println("计算普通企业的税收");
return 0.0;
}
}
class Region {
private List enterprises;
public Region() {
enterprises = new ArrayList<>();
}
public void addEnterprise(Enterprise enterprise) {
enterprises.add(enterprise);
}
public void removeEnterprise(Enterprise enterprise) {
enterprises.remove(enterprise);
}
public double calculateRegionTax() {
double regionTax = 0.0;
for (Enterprise enterprise : enterprises) {
regionTax += enterprise.calculateTax();
}
return regionTax;
}
}
public class Test {
public static void main(String[] args) {
// 创建地区对象
Region region = new Region();
// 添加不同类型的企业
region.addEnterprise(new HighTechEnterprise());
region.addEnterprise(new ForeignEnterprise());
region.addEnterprise(new NormalEnterprise());
// 计算地区税收
double regionTax = region.calculateRegionTax();
System.out.println("地区税收:" + regionTax);
}
}
根据需求设计了抽象类Enterprise、子类HighTechEnterprise、ForeignEnterprise和NormalEnterprise以及类Region。测试类Test中模拟了创建地区对象、添加不同类型的企业、计算地区税收的过程。
测试类中先创建了地区对象Region然后添加了高新企业HighTechEnterprise、外资企业ForeignEnterprise和普通企业NormalEnterprise。接着计算地区税收可以看到会调用各个企业子类的计算税收方法并计算出地区的总税收。最后将地区税收输出。

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Some files were not shown because too many files have changed in this diff Show more