mirror of
https://github.com/m1ngsama/code4sk.git
synced 2026-02-08 06:54:05 +00:00
upload :)
This commit is contained in:
parent
2061a0732b
commit
e150ec5613
1076 changed files with 12341 additions and 0 deletions
1
c/README.md
Normal file
1
c/README.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
All the C source files gathered here.
|
||||||
201
c/sourcecode/AlgorithmO/20230911/Toocold之区间最大子段和.cpp
Normal file
201
c/sourcecode/AlgorithmO/20230911/Toocold之区间最大子段和.cpp
Normal 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;
|
||||||
|
}
|
||||||
62
c/sourcecode/AlgorithmO/20230911/公共交通问题.cpp
Normal file
62
c/sourcecode/AlgorithmO/20230911/公共交通问题.cpp
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
C. 公共交通问题
|
||||||
|
Description
|
||||||
|
现在的大学生刚毕业不久如果没有家庭的支持一般是买不起心仪的车的,所以刚毕业的人一般都要乘公交车上下班,在早晚的上下班高峰时间段,道路交通很拥挤,每站都有人上下,公交车在每站都停。刚刚毕业的小明常常会被每站都停的公交车弄得很不耐烦,于是他提出了这样一个办法:
|
||||||
|
|
||||||
|
由于公交车的站点并不是非常多,那么在繁忙的上下班高峰时间,每次公交车从始发站点往终点站点开时,我们只允许公交车停在其中的某一个站点。所有乘客都从始发站点上公交车,到达某站点后,公交车停下来,所有乘客再从这里步行到自己的目的站点。在始发站的时候,每个乘客选择自己的目的站点,公交车系统则自动计算出应停的站点。
|
||||||
|
|
||||||
|
现在请问:公交车停在哪一站点能够保证这次乘坐公交车的所有乘客步行的站点数之和最少?
|
||||||
|
|
||||||
|
在这里为了更好体现算法的魅力所在,我们假定站点数可以很多,每次乘客人数也可以很多(实际生活中这两点是不可能出现的)。另外为了方便起见,在这里我们规定,刚开始公交车在始发站,并且认为乘客至少是在第一个站点及以后才有可能下车。
|
||||||
|
|
||||||
|
Input
|
||||||
|
有两行,第一行是公交站点数N(1<=N<=1000),第二行是N个正整数,分别表示对应站点(以该站点作为目的站点)的乘客人数M(1<=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;
|
||||||
|
}
|
||||||
70
c/sourcecode/AlgorithmO/20230911/数列游戏.cpp
Normal file
70
c/sourcecode/AlgorithmO/20230911/数列游戏.cpp
Normal 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);
|
||||||
|
}
|
||||||
75
c/sourcecode/AlgorithmO/20230911/最大子段和-DP方法.cpp
Normal file
75
c/sourcecode/AlgorithmO/20230911/最大子段和-DP方法.cpp
Normal 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;
|
||||||
|
}
|
||||||
51
c/sourcecode/AlgorithmO/20230911/最大子段和问题3.c
Normal file
51
c/sourcecode/AlgorithmO/20230911/最大子段和问题3.c
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
C. 最大子段和问题
|
||||||
|
Description
|
||||||
|
给定n个整数(可能为负数)a1,a2,……an。求ai,ai+1,……aj,其中i<=i<=j<=n的子段和的最大值。当所有整数均为负数时我们定义其最大子段和为0(此时From为1,To为n)。例如:当(a1,a2,a3,a4,a5,a6)=(-2,11,-4,13,-5,-2)时,最大子段和为a2+a3+a4=20,i=2,j=4(下标从1开始),这个问题我们称之为“最大子段和问题”。
|
||||||
|
|
||||||
|
Input
|
||||||
|
本问题有多组测试数据,每组数据有两行,第一行为n(n<=1000),第二行有n’个整数,数与数之间用空格隔开。
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
92
c/sourcecode/AlgorithmO/20230911/最大子段和问题(分治法).cpp
Normal file
92
c/sourcecode/AlgorithmO/20230911/最大子段和问题(分治法).cpp
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
D. 最大子段和问题三(分治法)
|
||||||
|
Description
|
||||||
|
给你n个整数a1,a2,a3……,an,对于1<=i<=j<=n(1<=n<=100000);求ai+ai+1+ai+2+……aj的最大值。如果给定的n个整数都为负数,那么规定最大值为零。
|
||||||
|
|
||||||
|
要求用分治法求解
|
||||||
|
|
||||||
|
Input
|
||||||
|
输入为两行,第一行为整数n,第二行为n个整数。
|
||||||
|
|
||||||
|
Output
|
||||||
|
输出也有两行,第一行为i和j;第二行为最大的子段和的值,格式参见Sample 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;
|
||||||
|
}
|
||||||
54
c/sourcecode/AlgorithmO/20230911/最简单的“最大子段和”问题.c
Normal file
54
c/sourcecode/AlgorithmO/20230911/最简单的“最大子段和”问题.c
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
A. 最简单的“最大子段和”问题
|
||||||
|
Description
|
||||||
|
给定n个整数(可能为负数)a1,a2,……an。求ai,ai+1,……aj 其中1<=i<=j<=n的子段和的最大值。当所有整数均为负数时我们定义其最大子段和为0。例如:当(a1,a2,a3,a4,a5,a6)=(-2,11,-4,13,-5,-2)时,最大子段和为a2+a3+a4=20,i=2,j=4(下标从1开始)这个问题我们称之为“最大子段和问题”。
|
||||||
|
|
||||||
|
在课堂上,我们假定n<=100,今天我们把n的范围规定修改为n<=200,你的任务是设计一个程序解决它。
|
||||||
|
|
||||||
|
Input
|
||||||
|
输入由两行,第一行为n;第二行为n个整数。
|
||||||
|
|
||||||
|
Output
|
||||||
|
输出也有两行,第一行为:“From=xxx,To=xxx”;第二行为:“MaxSum=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;
|
||||||
|
}
|
||||||
|
|
||||||
54
c/sourcecode/AlgorithmO/20230911/最简单的“最大子段和”问题2.c
Normal file
54
c/sourcecode/AlgorithmO/20230911/最简单的“最大子段和”问题2.c
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
B. 最简单的“最大子段和”问题
|
||||||
|
Description
|
||||||
|
给定n个整数(可能为负数)a1,a2,……an。求ai,ai+1,……aj 其中i<=i<=j<=n的子段和的最大值。当所有整数均为负数时我们定义其最大子段和为0,且初始和终止位置也为0。例如:当(a1,a2,a3,a4,a5,a6)=(-2,11,-4,13,-5,-2)时,最大子段和为a2+a3+a4=20,i=2,j=4(下标从1开始)这个问题我们称之为“最大子段和问题”。
|
||||||
|
|
||||||
|
在考试一上,我们假定n<=200,今天我们把n的范围规定修改为n<=2000,你的任务是设计一个程序解决它。
|
||||||
|
|
||||||
|
Input
|
||||||
|
输入由两行,第一行为n;第二行为n个整数。
|
||||||
|
|
||||||
|
Output
|
||||||
|
输出也有两行,第一行为:“From=xxx,To=xxx”;第二行为:“MaxSum=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;
|
||||||
|
}
|
||||||
|
|
||||||
28
c/sourcecode/AlgorithmO/20230911/穿错衣服问题.py
Normal file
28
c/sourcecode/AlgorithmO/20230911/穿错衣服问题.py
Normal 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
|
||||||
59
c/sourcecode/AlgorithmO/20230911/篮球宝贝.cpp
Normal file
59
c/sourcecode/AlgorithmO/20230911/篮球宝贝.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
B. 篮球宝贝
|
||||||
|
Description
|
||||||
|
篮球宝贝既是激烈的篮球比赛之中,给赛场的紧张气氛带来些许放松的篮球拉拉队,同时为球迷和球员加油鼓劲的拉拉队员,而且篮球拉拉队的表演也非常精彩。篮球宝贝就是被球迷爱称为“篮球宝贝”的拉拉队员。今年NIT篮球协会要在学校内选一批篮球宝贝,学校很支持这项活动,把愿意参加活动的学生名单全部提供出来了,现在需要聪明的你来帮助活动的主办方完成这项任务。学校提供的名单信息含有性别、身高、年级三项内容,而篮球宝贝只能是大一或者大二的符合身高要求的女生,现在请你编程实现。
|
||||||
|
|
||||||
|
Input
|
||||||
|
本问题有多组测试数据,而且输入的第一行就是测试数据的组数。对于每一组测试数据,共有三部分;第一部分只有一行是两个整数n和m,n表示学生总数,m表示需要选拔的篮球宝贝的人数,其中1<=n<=20000,1<=m<=50,两数之间有空格分隔;第二部分也是一行是篮球宝贝所要求的身高,用两个整数F和T表示,也就是说F<=选中的学生的身高<=T,其中150<=F<=T<=180,两数之间有空格分隔;最后一部分有n行是学生的数据,每行有三个整数组成,第一个整数S表示性别,其中S=0表示女生,S=1表示男生,第二个整数H表示身高,第三个整数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;
|
||||||
|
}
|
||||||
31
c/sourcecode/AlgorithmO/20230911/自增自减运算.py
Normal file
31
c/sourcecode/AlgorithmO/20230911/自增自减运算.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#
|
||||||
|
'''
|
||||||
|
D. 自增自减运算
|
||||||
|
Description
|
||||||
|
小胖子正在学习汇编语言,汇编语言里面有自增和自减指令,叫inc和dec指令,其实C语言里面也有自增和自减运算的,即++和—运算,但这些指令或者运算都是有范围的,现在小胖子碰到了很长很长的整数,小胖子脑子一转,很快就想出来。现在请你也实现这个自增和自减运算。
|
||||||
|
|
||||||
|
Input
|
||||||
|
输入有多组,第一行是测试数据的组数。对于每一组测试数据,共有两行,其中第一行是一个整数M(0表示自减运算,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)
|
||||||
48
c/sourcecode/AlgorithmO/20230911/英语成绩分析.cpp
Normal file
48
c/sourcecode/AlgorithmO/20230911/英语成绩分析.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
A. 英语成绩分析
|
||||||
|
Description
|
||||||
|
NIT学校前不久刚结束了一次大一学生的英语测试,分管教学的副校长想知道全校最高分与排名第二十名学生的分差是多少,还想知道最高分与平均分的分差是多少,请你帮忙算一下。
|
||||||
|
|
||||||
|
Input
|
||||||
|
本问题有多组测试数据,第一行输入的就是测试数据组数,对于每组数据,有两行。其中第一行就是学生的个数n(21<=n<=2000);第二行是n个整数,表示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;
|
||||||
|
}
|
||||||
1
c/sourcecode/AlgorithmO/README.md
Normal file
1
c/sourcecode/AlgorithmO/README.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
老蔡你来真的啊0.o
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-10 统计素数并求和.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-10 统计素数并求和.exe
Normal file
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
7-4 求整数的位数及各位数字之和
|
||||||
|
分数 10
|
||||||
|
作者 C课程组
|
||||||
|
单位 浙江大学
|
||||||
|
对于给定的正整数N,求它的位数及其各位数字之和。
|
||||||
|
|
||||||
|
输入格式:
|
||||||
|
输入在一行中给出一个不超过10^9的正整数N。
|
||||||
|
|
||||||
|
输出格式:
|
||||||
|
在一行中输出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;
|
||||||
|
}
|
||||||
41
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-5 约分最简分式.cpp
Normal file
41
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-5 约分最简分式.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
/**/
|
||||||
31
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-7 求e的近似值.cpp
Normal file
31
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-7 求e的近似值.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
59
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-9 找完数.cpp
Normal file
59
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-9 找完数.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
7-9 找完数
|
||||||
|
分数 10
|
||||||
|
作者 陈建海
|
||||||
|
单位 浙江大学
|
||||||
|
所谓完数就是该数恰好等于除自身外的因子之和。例如:6=1+2+3,其中1、2、3为6的因子。本题要求编写程序,找出任意两正整数m和n之间的所有完数。
|
||||||
|
|
||||||
|
输入格式:
|
||||||
|
输入在一行中给出2个正整数m和n(1<m≤n≤10000),中间以空格分隔。
|
||||||
|
|
||||||
|
输出格式:
|
||||||
|
逐行输出给定范围内每个完数的因子累加形式的分解式,每个完数占一行,格式为“完数 = 因子1 + 因子2 + ... + 因子k”,其中完数和因子均按递增顺序给出。若区间内没有完数,则输出“None”。
|
||||||
|
|
||||||
|
输入样例:
|
||||||
|
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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-9 找完数.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-9 找完数.exe
Normal file
Binary file not shown.
8
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/test.cpp
Normal file
8
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/test.cpp
Normal 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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/test.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/test.exe
Normal file
Binary file not shown.
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
7-2 求简单交错序列前N项和
|
||||||
|
分数 10
|
||||||
|
作者 C课程组
|
||||||
|
单位 浙江大学
|
||||||
|
本题要求编写程序,计算序列 1 - 1/4 + 1/7 - 1/10 + ... 的前N项之和。
|
||||||
|
|
||||||
|
输入格式:
|
||||||
|
输入在一行中给出一个正整数N。
|
||||||
|
|
||||||
|
输出格式:
|
||||||
|
在一行中按照“sum = S”的格式输出部分和的值S,精确到小数点后三位。题目保证计算结果不超过双精度范围。
|
||||||
|
|
||||||
|
输入样例:
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-1 选择法排序之第k趟.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-1 选择法排序之第k趟.exe
Normal file
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-2 求集合数据的均方差.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-2 求集合数据的均方差.exe
Normal file
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-3 计算最大值出现的次数.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-3 计算最大值出现的次数.exe
Normal file
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-4 交换最小值和最大值.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-4 交换最小值和最大值.exe
Normal file
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-7 十进制转二进制.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-7 十进制转二进制.exe
Normal file
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-9 冒泡法排序之过程.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/7-9 冒泡法排序之过程.exe
Normal file
Binary file not shown.
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-2 求矩阵各行元素之和.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-2 求矩阵各行元素之和.exe
Normal file
Binary file not shown.
58
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-3 打印杨辉三角.cpp
Normal file
58
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-3 打印杨辉三角.cpp
Normal 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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-3 打印杨辉三角.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-3 打印杨辉三角.exe
Normal file
Binary file not shown.
49
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-4 矩阵运算.cpp
Normal file
49
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-4 矩阵运算.cpp
Normal 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;
|
||||||
|
}
|
||||||
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-4 矩阵运算.exe
Normal file
BIN
c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/7-4 矩阵运算.exe
Normal file
Binary file not shown.
1
c/sourcecode/朱森森带领的奇妙冒险/readme.md
Normal file
1
c/sourcecode/朱森森带领的奇妙冒险/readme.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
我永远喜欢朱森森030
|
||||||
73
c/sourcecode/朱森森带领的奇妙冒险/字符串1/删除重复字符.cpp
Normal file
73
c/sourcecode/朱森森带领的奇妙冒险/字符串1/删除重复字符.cpp
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
38
c/sourcecode/朱森森带领的奇妙冒险/字符串1/判断回文.cpp
Normal file
38
c/sourcecode/朱森森带领的奇妙冒险/字符串1/判断回文.cpp
Normal 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;
|
||||||
|
}
|
||||||
96
c/sourcecode/朱森森带领的奇妙冒险/字符串1/判断回文字符串.cpp
Normal file
96
c/sourcecode/朱森森带领的奇妙冒险/字符串1/判断回文字符串.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
73
c/sourcecode/朱森森带领的奇妙冒险/字符串1/十六进制字符串转换成十进制非负整数.cpp
Normal file
73
c/sourcecode/朱森森带领的奇妙冒险/字符串1/十六进制字符串转换成十进制非负整数.cpp
Normal 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;
|
||||||
|
}
|
||||||
34
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串字母大小写转换.cpp
Normal file
34
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串字母大小写转换.cpp
Normal 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;
|
||||||
|
}
|
||||||
40
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串替换.cpp
Normal file
40
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串替换.cpp
Normal 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;
|
||||||
|
}
|
||||||
83
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串转换成十进制整数.cpp
Normal file
83
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串转换成十进制整数.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
39
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串输入练习 (III).cpp
Normal file
39
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串输入练习 (III).cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
//字符串输入练习 (III)
|
||||||
|
|
||||||
|
//输出字符串的子串。
|
||||||
|
|
||||||
|
//输入格式:
|
||||||
|
//每行的开始是一个正整数k,然后是一个字符串s,k和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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
34
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串逆序.cpp
Normal file
34
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符串逆序.cpp
Normal 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;
|
||||||
|
}
|
||||||
59
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符转换.cpp
Normal file
59
c/sourcecode/朱森森带领的奇妙冒险/字符串1/字符转换.cpp
Normal 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;
|
||||||
|
}
|
||||||
25
c/sourcecode/朱森森带领的奇妙冒险/字符串1/宇宙无敌大招呼.cpp
Normal file
25
c/sourcecode/朱森森带领的奇妙冒险/字符串1/宇宙无敌大招呼.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
48
c/sourcecode/朱森森带领的奇妙冒险/字符串1/查找指定字符.cpp
Normal file
48
c/sourcecode/朱森森带领的奇妙冒险/字符串1/查找指定字符.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
29
c/sourcecode/朱森森带领的奇妙冒险/字符串1/统计大写辅音字母.cpp
Normal file
29
c/sourcecode/朱森森带领的奇妙冒险/字符串1/统计大写辅音字母.cpp
Normal 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;
|
||||||
|
}
|
||||||
31
c/sourcecode/朱森森带领的奇妙冒险/字符串1/统计字符串中数字字符的个数.cpp
Normal file
31
c/sourcecode/朱森森带领的奇妙冒险/字符串1/统计字符串中数字字符的个数.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
34
c/sourcecode/朱森森带领的奇妙冒险/字符串1/统计字符出现次数.cpp
Normal file
34
c/sourcecode/朱森森带领的奇妙冒险/字符串1/统计字符出现次数.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
45
c/sourcecode/朱森森带领的奇妙冒险/字符串1/英文字母替换加密(大小写转换+后移1位).cpp
Normal file
45
c/sourcecode/朱森森带领的奇妙冒险/字符串1/英文字母替换加密(大小写转换+后移1位).cpp
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
65
c/sourcecode/朱森森带领的奇妙冒险/字符串1/输出大写英文字母.cpp
Normal file
65
c/sourcecode/朱森森带领的奇妙冒险/字符串1/输出大写英文字母.cpp
Normal 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;
|
||||||
|
}
|
||||||
12
c/sourcecode/朱森森带领的奇妙冒险/数组2/冒泡排序.cpp
Normal file
12
c/sourcecode/朱森森带领的奇妙冒险/数组2/冒泡排序.cpp
Normal 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;
|
||||||
|
}
|
||||||
43
c/sourcecode/朱森森带领的奇妙冒险/数组2/判断上三角矩阵.cpp
Normal file
43
c/sourcecode/朱森森带领的奇妙冒险/数组2/判断上三角矩阵.cpp
Normal 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;
|
||||||
|
}
|
||||||
30
c/sourcecode/朱森森带领的奇妙冒险/数组2/判断对称矩阵.cpp
Normal file
30
c/sourcecode/朱森森带领的奇妙冒险/数组2/判断对称矩阵.cpp
Normal 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;
|
||||||
|
}
|
||||||
42
c/sourcecode/朱森森带领的奇妙冒险/数组2/打印杨辉三角.cpp
Normal file
42
c/sourcecode/朱森森带领的奇妙冒险/数组2/打印杨辉三角.cpp
Normal 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行,每行第一个,最后一个为1,i==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;
|
||||||
|
}
|
||||||
51
c/sourcecode/朱森森带领的奇妙冒险/数组2/找鞍点.cpp
Normal file
51
c/sourcecode/朱森森带领的奇妙冒险/数组2/找鞍点.cpp
Normal 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;
|
||||||
|
}
|
||||||
30
c/sourcecode/朱森森带领的奇妙冒险/数组2/方阵循环右移.cpp
Normal file
30
c/sourcecode/朱森森带领的奇妙冒险/数组2/方阵循环右移.cpp
Normal 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;
|
||||||
|
}
|
||||||
35
c/sourcecode/朱森森带领的奇妙冒险/数组2/求矩阵各行元素之和.cpp
Normal file
35
c/sourcecode/朱森森带领的奇妙冒险/数组2/求矩阵各行元素之和.cpp
Normal 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;
|
||||||
|
}
|
||||||
30
c/sourcecode/朱森森带领的奇妙冒险/数组2/求矩阵的局部极大值.cpp
Normal file
30
c/sourcecode/朱森森带领的奇妙冒险/数组2/求矩阵的局部极大值.cpp
Normal 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;
|
||||||
|
}
|
||||||
15
c/sourcecode/朱森森带领的奇妙冒险/数组2/求矩阵的最大值.cpp
Normal file
15
c/sourcecode/朱森森带领的奇妙冒险/数组2/求矩阵的最大值.cpp
Normal 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;
|
||||||
|
}
|
||||||
25
c/sourcecode/朱森森带领的奇妙冒险/数组2/矩阵转置.cpp
Normal file
25
c/sourcecode/朱森森带领的奇妙冒险/数组2/矩阵转置.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
19
c/sourcecode/朱森森带领的奇妙冒险/数组2/矩阵运算.cpp
Normal file
19
c/sourcecode/朱森森带领的奇妙冒险/数组2/矩阵运算.cpp
Normal 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;
|
||||||
|
}
|
||||||
27
c/sourcecode/朱森森带领的奇妙冒险/数组2/螺旋方阵.cpp
Normal file
27
c/sourcecode/朱森森带领的奇妙冒险/数组2/螺旋方阵.cpp
Normal 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;
|
||||||
|
}
|
||||||
18
c/sourcecode/朱森森带领的奇妙冒险/数组2/角色交换.cpp
Normal file
18
c/sourcecode/朱森森带领的奇妙冒险/数组2/角色交换.cpp
Normal 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;
|
||||||
|
}
|
||||||
18
c/sourcecode/朱森森带领的奇妙冒险/数组2/计算天数.cpp
Normal file
18
c/sourcecode/朱森森带领的奇妙冒险/数组2/计算天数.cpp
Normal 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;
|
||||||
|
}
|
||||||
7
java/JavaDemo/Swing/HelloLabel/.vscode/settings.json
vendored
Normal file
7
java/JavaDemo/Swing/HelloLabel/.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"java.project.sourcePaths": ["src"],
|
||||||
|
"java.project.outputPath": "bin",
|
||||||
|
"java.project.referencedLibraries": [
|
||||||
|
"lib/**/*.jar"
|
||||||
|
]
|
||||||
|
}
|
||||||
18
java/JavaDemo/Swing/HelloLabel/README.md
Normal file
18
java/JavaDemo/Swing/HelloLabel/README.md
Normal 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).
|
||||||
BIN
java/JavaDemo/Swing/HelloLabel/bin/HelloLabel.class
Normal file
BIN
java/JavaDemo/Swing/HelloLabel/bin/HelloLabel.class
Normal file
Binary file not shown.
BIN
java/JavaDemo/Swing/HelloLabel/src/HelloLabel.class
Normal file
BIN
java/JavaDemo/Swing/HelloLabel/src/HelloLabel.class
Normal file
Binary file not shown.
16
java/JavaDemo/Swing/HelloLabel/src/HelloLabel.java
Normal file
16
java/JavaDemo/Swing/HelloLabel/src/HelloLabel.java
Normal 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!");
|
||||||
|
}
|
||||||
|
}///:~
|
||||||
7
java/JavaDemo/Swing/HelloSwing/.vscode/settings.json
vendored
Normal file
7
java/JavaDemo/Swing/HelloSwing/.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"java.project.sourcePaths": ["src"],
|
||||||
|
"java.project.outputPath": "bin",
|
||||||
|
"java.project.referencedLibraries": [
|
||||||
|
"lib/**/*.jar"
|
||||||
|
]
|
||||||
|
}
|
||||||
18
java/JavaDemo/Swing/HelloSwing/README.md
Normal file
18
java/JavaDemo/Swing/HelloSwing/README.md
Normal 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).
|
||||||
BIN
java/JavaDemo/Swing/HelloSwing/bin/HelloSwing.class
Normal file
BIN
java/JavaDemo/Swing/HelloSwing/bin/HelloSwing.class
Normal file
Binary file not shown.
BIN
java/JavaDemo/Swing/HelloSwing/src/HelloSwing.class
Normal file
BIN
java/JavaDemo/Swing/HelloSwing/src/HelloSwing.class
Normal file
Binary file not shown.
11
java/JavaDemo/Swing/HelloSwing/src/HelloSwing.java
Normal file
11
java/JavaDemo/Swing/HelloSwing/src/HelloSwing.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
649
java/JavaExam/java期末试卷练习
Normal file
649
java/JavaExam/java期末试卷练习
Normal 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)。接着计算地区税收,可以看到会调用各个企业子类的计算税收方法,并计算出地区的总税收。最后将地区税收输出。
|
||||||
|
|
||||||
7
java/JavaLab/Lab9/PrimeNumber/.vscode/settings.json
vendored
Normal file
7
java/JavaLab/Lab9/PrimeNumber/.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"java.project.sourcePaths": ["src"],
|
||||||
|
"java.project.outputPath": "bin",
|
||||||
|
"java.project.referencedLibraries": [
|
||||||
|
"lib/**/*.jar"
|
||||||
|
]
|
||||||
|
}
|
||||||
18
java/JavaLab/Lab9/PrimeNumber/README.md
Normal file
18
java/JavaLab/Lab9/PrimeNumber/README.md
Normal 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
Loading…
Reference in a new issue