码题集新手村600道[不含vip]
- 刷题链接
- MT1301
- MT1302
- MT1303
- MT1304
- MT1305
- MT1307
- MT1308
- MT1310
- MT1311
- MT1312
- MT1314
- MT1318
- MT1320
- MT1322
- MT1323
- MT1325
- MT1326
- MT1327
- MT1328
- MT1329
- MT1332
- MT1333
- MT1334
- MT1335
- MT1336
- MT1337
- MT1338
- MT1339
- MT1340
- MT1342
- MT1343
- MT1344
- MT1345
- MT1347
- MT1348
- MT1349
- MT1350
- MT1352
- MT1353
- MT1354
- MT1355
- MT1356
- MT1357
- MT1358
- MT1359
- MT1360
- MT1363
- MT1366
- MT1367
- MT1368
- MT1370
- MT1372
- MT1374
- MT1375
- MT1377
- MT1379
- MT1380
- MT1381
- MT1382
- MT1385
- MT1386
- MT1387
- MT1388
- MT1389
- MT1390
- MT1393
- MT1394
- MT1395
- MT1396
- MT1397
- MT1398
- MT1399
- MT1400
- MT1404
- MT1405
- MT1408
- MT1410 逆时针旋转数组
- MT1411 顺时针旋转数组
- MT1412 合并
- MT1414
- MT1415
- MT1416
- MT1417
- MT1418
- MT1420
- MT1421
- MT1422
- MT1425
- MT1427
- MT1428
- MT1429
- MT1432
- MT1433
- MT1434
- MT1435
- MT1437
- MT1438
- MT1440
- MT1441
- MT1442
- MT1445
- MT1446
- MT1447
- MT1448
- MT1450
- MT1451
- MT1453
- MT1454
- MT1455
- MT1456
- MT1458
- MT1459
- MT1462
- MT1463
- MT1464
- MT1465
- MT1466
- MT1470
- MT1472
- MT1473
- MT1475
- MT1476
- MT1477
- MT1478
- MT1479
- MT1480
- MT1481
- MT1482
- MT1484
- MT1485
- MT1487
- MT1488
- MT1489
- MT1490
- MT1491
- MT1492
- MT1493
- MT1496
- MT1497
- MT1498
- MT1499
- MT1500
- MT1501
- MT1502
- MT1505
- MT1508
- MT1509
- MT1513
- MT1514
- MT1515
- MT1517
- MT1518
- MT1519
- MT1520
- MT1521
- MT1522
- MT1523
- MT1524
- MT1526
- MT1527
- MT1530
- MT1532
- MT1534
- MT1535
- MT1536
- MT1537
- MT1540
- MT1541
- MT1542
- MT1543
- MT1544
- MT1545
- MT1547
- MT1548
- MT1549
- MT1550
- MT1552
- MT1553
- MT1555
- MT1556
- MT1557
- MT1558
- MT1559
- MT1562
- MT1563
- MT1564
- MT1565
- MT1566
- MT1567
- MT1568
- MT1572
- MT1573
- MT1574
- MT1575
- MT1577
- MT1579
- MT1580
- MT1581
- MT1582
- MT1584
- MT1585
- MT1586
- MT1587
- MT1588
- MT1589
- MT1590
- MT1591
- MT1592
- MT1593
- MT1594
- MT1595
- MT1596
- MT1597
- MT1598
- MT1599
- MT1600
刷题链接
https://matiji.net/exam/oj-questionbank/B3FCFEC101BD05189BB74D522E019504
MT1301
#include<bits/stdc++.h>
using namespace std;
/*
1010
0101 =>原码和补码相加为pow(2,4)-1
0001 0001
1110 1110 =>原码和补码相加为pow(2,8)-1
*/
int main( )
{
int a;
cin>>a;
long now=1;
while(1){
if(a>=now && a<now*16){//每4位是:2的4次方=>16
cout<<now*16-1-a;
return 0;
}
now = now * 16;
}
return 0;
}
MT1302
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//从右往左看
char s[10];
cin>>s;
for(int a=3;a>=1;a--){//题目说了只有4位
//xor:同为,不同为1
if(s[a]!=s[a-1]) s[a]='1';
else s[a]='0';
}
cout <<s;
return 0;
}
MT1303
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//从左往右看
char s[10];
cin>>s;
for(int a=1;a<=3;a++){//题目说了只有4位
if(s[a]!=s[a-1])s[a]='1';
else s[a]='0';
}
cout<<s;
return 0;
}
MT1304
#include<bits/stdc++.h>
using namespace std;
int main( )
{
/*
此题目设定格雷码为3位
十进制数0~7,转格雷码,再将其转为十进制数
4=>100=>格雷码是110(二进制转格雷码是从右往左)=>6
*/
int n,cnt=3,A[4];//我们使用下标1,2,3
int ans;
cin>>n;
while(n){//变二进制
A[cnt--]=n%2;
n /= 2;
}
for(int a=3;a>=2;a--){//二进制转格雷码,从后往前
if(A[a]!=A[a-1])A[a]=1;
else A[a]=0;
}
for(int b=3,k=1;b>=1;b--,k=k*2){//格雷码转十进制
//二进制从低到高的权重分别是1 2 4
ans += A[b]*k;
}
cout<<ans;
return 0;
}
MT1305
#include<bits/stdc++.h>
using namespace std;
/*
七进制的三位数的范围是:
100~666 => 49~342
九进制的三位数的范围是:
100~888 => 81~728
=>所以题目要求的这个三位数的范围是[81,342]
*/
int main( )
{
int qi[3],jiu[3];
for(int a=81;a<=342;a++){
int t1=a,t2=a;//t1和t2都是消耗品
for(int i=2;i>=0;i--){
qi[i]=t1%7;
t1=t1/7;
}
for(int i=2;i>=0;i--){
jiu[i]=t2%9;
t2=t2/9;
}
if(qi[0]==jiu[2] &&
qi[1]==jiu[1] &&
qi[2]==jiu[0])cout<<a;
}
return 0;
}
MT1307
#include<bits/stdc++.h>
using namespace std;
double f(double x){
return x*x-6*x-1;
}
int main( )
{
double a=-10,b=0;//该实根一定是负数
double mid = (a+b)/2;
//为什么要和1的-6次方进行对比,因为样例1给的是6位小数
while(fabs(f(mid))>1e-6){//fabs是浮点数的绝对值
mid=(a+b)/2;
//a代表小于0的一边,
//b代表大于0的一边
//如果f(mid)与f(a)同号,则a在该实根的左边,应该
//令a=mid,反之,令b=mid
if(f(mid)*f(a)>0) a=mid;
else b=mid;
}
printf("%lf",mid);
return 0;
}
MT1308
#include<bits/stdc++.h>
using namespace std;
/*
需要在本上计算一个pqr的范围:
p=>2~4
q=>p~6
r=>q~12
s = pqr/(pqr-qr-pr-pq)
*/
int main( )
{
for(int p=2;p<=4;p++){//1/4
for(int q=p;q<=6;q++){// (1-1/2) * 1/3
for(int r=q;r<=12;r++){// (1-1/2-1/3)*1/2
int fenzi = p*q*r;
int fenmu = (p*q*r-q*r-p*r-p*q);
if(fenmu){//不为0
//是整数 s>=r
if((fenzi%fenmu==0)&&(fenzi/fenmu)>=r){
printf("%d %d %d %d\n",p,q,r,fenzi/fenmu);
}
}
}
}
}
return 0;
}
MT1310
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
cin>>n;
long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
/*for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}*/
long tmp=0;//最终答案
/*
第一轮循环,负责拿到第一个数【比如说s1】不动;
第二轮循环,负责将(从第一轮循环拿到的那个数开始,进行依次累乘【比如说s2】),然后对比s1和说s2
*/
for(int i=0;i<n;i++){
long product=a[i];//中间变量,最终答案要和他对比
tmp=max(tmp,product);
for(int j=i+1;j<n;j++){
product *= a[j];//先乘再进行对比
tmp=max(tmp,product);
}
}
cout<<tmp;
return 0;
}
MT1311
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//每位数都不能重复
int ans=0;
for(int a=1;a<=9;a++)
for(int b=0;b<=9;b++)
for(int c=0;c<=9;c++){
if(a!=b && a!=c &&b!=c){
ans++;
}
}
cout<<ans;
return 0;
}
MT1312
#include<bits/stdc++.h>
using namespace std;
int judge(int a,int b){//判断这个4位数和5位数 是否 把1~9全用到了
//a是四位数 , b是五位数
int g[10]={0};//存放每个数出现的次数
int f= a*100000+b;
for(int i=1;i<=9;i++){
if(f%10==0)return 0;//不能有0
if(g[f%10]==0)g[f%10]=1;//f%10只能用1~9
else return 0;
f= f/10;
}
return 1;
}
int main( )
{
//被除数5位,除数4位,商<80
//注意:只能用1~9
/*
eg:
79546 / 1283 = 62
94736 / 1528 = 62
so,这个4位数和5位数一定是把1~9全用到了
*/
int a;
cin>>a;
for(int i=1000;i<=9999;i++){
if(i*a>99999 || i*a<10000)continue;
if(judge(i,i*a)==1)cout<<i*a<<" "<<i<<endl;
}
return 0;
}
MT1314
#include<bits/stdc++.h>
using namespace std;
/*
因为默认数据类型保留的小数位有限,
此题需要用代码模拟除法和四舍五入过程
1<n<=100
*/
int main( )
{
int a,b,n,A[101];//A[0]是正数位,A[1]~A[100]是小数位
cin>>a>>b>>n;
A[0]=a/b;
a=a%b;
//模拟除法,但是没有四舍五入
for(int i=1;i<=n+1;i++){
a *= 10;
A[i] =a/b;
a=a%b;
}
//进行四舍五入
if(A[n+1]>=5){
A[n]++;//前一位+1
/*for(int j=n-1;j>=1;j--){//遍历前前一位到第一位
if(A[j+1]==10){
A[j]++;
A[j+1]=0;
}
}
if(A[1]==0)A[0]++,A[1]=0;
*/
for(int j=n;j>=1;j--){//遍历n位小数
if(A[j]==10){
A[j-1]++;
A[j]=0;
}
}
}
cout<<A[0]<<".";
for(int z=1;z<=n;z++)cout<<A[z];
return 0;
}
MT1318
#include<bits/stdc++.h>
using namespace std;
bool judge(int c){
if((int)sqrt(c)*(int)sqrt(c)==c)return 1;
else return 0;
}
int main( )
{
/*
1681=>首先这个数时平方数,其实从中间砍一刀,分成的两个数,也是平方
注:不一定是22分,也可以是13分(以四位数为例)
*/
//用字符串处理
string n;
cin>>n;
int a = 0;
for(int i=0;i<n.length();i++){
a=a*10 +(n[i]-'0');
}
if(judge(a)==1){
for(int i=1;i<n.length();i++){//第一次:一个数是1位,另一个数(n.length()-1) -1位
int temp = 0;
for(int j=0;j<i;j++){
temp=temp*10 +(n[j]-'0');
}
if(judge(temp)==0)continue;
temp=0;
for(int j=i;j<n.length();j++){
temp=temp*10 +(n[j]-'0');
}
if(judge(temp)==0)continue;
cout<<"YES";
return 0;
}
}
cout<<"NO";
return 0;
}
MT1320
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//注:N<100
int n,a[100],count=0;//统计奇数的个数
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]%2==1)count++;
}
sort(a,a+n);//sort(首元素地址(必填), 尾元素地址的下一个地址(必填), 比较函数(非必填));
for(int j=0;j<n;j++){
if((a[j]+count)%2==0){//若count是奇数,则输出第一个奇数,若count是偶数,则输出第一个偶数
cout<<a[j]<<endl;
break;
}
}
return 0;
}
MT1322
#include<bits/stdc++.h>
using namespace std;
//3 6 12
/*
6 5 1
6 4 2
6 3 3
6 2 4
6 1 5
5 6 1
5 5 2
5 4 3
5 3 4
5 2 5
5 1 6
4 6 2
4 5 3
4 4 4
4 3 5
4 2 6
3 6 3
3 5 4
3 4 5
3 3 6
2 6 4
2 5 5
2 4 6
1 6 5
1 5 6=>一共25种,说明可以重复
*/
int method (int n , int m, int sum){//n个筛子,每个m面,第三个参数用来计数
if(sum<=0||sum>n*m)return 0;//计数可能出现的情况
if(n==1)return 1;//只剩下一个筛子的时候
int tmp=0;
for(int i=1;i<=m;i++){
tmp += method(n-1,m,sum-i);
}
return tmp;
}
int main( )
{
int n,m,sum;
cin>>n>>m>>sum;
cout<<method(n,m,sum);
return 0;
}
MT1323
//方二:自己写的
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
int a[100],b[100];
a[0]=x,b[0]=y;//根节点
int a1=0,b1=0;
while(x!=1){
a[++a1]=x/2;
x /= 2;
}
while(y!=1){
b[++b1]=y/2;
y /= 2;
}
int ans = a1+b1;
for (int i=a1,j=b1; i>0 && j>0; i--,j--)//从头节点开始遍历
{
if (a[i]==b[j]&&a[i-1]==b[j-1])
{
ans = ans-2;
}
}
cout<<ans;
return 0;
}
//方一:官方写的
/*#include <bits/stdc++.h>
using namespace std;
int main()
{
//找x到1,y到1的路径,然后把重复的路径删了,就是最短路径
//eg1: 4 3
//4->2->1
//3->1
//没有重合路径,所以一共四步,即:4->2->1->3
//eg2:8 10
//8->4->2->1
//10->5->2->1
//2->1重合了,删去一个2->1,一共4步,即:8->4->2->5->10
//注:每个数除以2(取整),都可以得到上一个节点
int a, b;
cin >> a >> b;
int a1[100], b1[100]; //分别存放两个数的路径
int a2=0, b2=0; //记录步数
a1[0] = a, b1[0] = b;//第一个节点是自己
while (a != 1)
{
a1[++a2] = a / 2;
a /= 2;
}
while (b != 1)
{
b1[++b2] = b / 2;
b /= 2;
}
//a1[a2] = 1, b1[b2] = 1;//最后一个节点是1,这一步是多余的,写不写都一样
for (int i = 0; i <= min(a2, b2); i++)
{
if (a1[a2 - i] != b1[b2 - i])
{
cout << a2 + b2 - i * 2 + 2;
return 0;
}
}
cout << abs(a2 - b2);
return 0;
}*/
MT1325
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//先固定两个数,再看剩下的两个数
int n,x;
int num[200];
cin>>n>>x;
for(int i=0;i<n;i++)
cin>>num[i];
sort(num,num+n);
for(int i = 0;i<n-3;i++){
for(int j=i+1;j<n-2;j++){
if(num[i]+num[j] > x){
cout<<"0";
return 0;
}else{
int high = n-1,low = j+1;//两面夹击
int y = x-(num[i]+num[j]);
while(high>low){
if(num[high]+num[low]<y)low++;
else if(num[high]+num[low]>y)high--;
else{
cout<<"1";
return 0;
}
}
}
}
}
cout<<"0";
return 0;
}
MT1326
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//就是将0和1分开
//四种情况:
//0 0不用换 low++
//1 1不用换 high--
//0 1不用换 low++,high--
//1 0交换
char s[100];
int n;
cin>>n;
cin>>s;
int high =n-1 , low=0;
while(high>low){
if(s[low]=='0' && s[high]=='0')low++;
else if(s[low]=='1' && s[high]=='1')high--;
else if(s[low]=='0' && s[high]=='1'){
low++;
high--;
}else{
char tmp=s[low];
s[low]=s[high];
s[high]=tmp;
low++;
high--;
}
}
cout<<s;
return 0;
}
MT1327
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//上一道题类似,这道题我们,先分离2,再分离1,最后剩下的就是0
char s[100];
int n;
cin>>n;
cin>>s;
int high=n-1,low=0;
char all[4]="210";
//cout<<strlen(all);//3
for(int i=0;i<strlen(all)-1;i++){//i取0和1
char max_ch = all[i];
while(high>low){
if(s[low] !=max_ch && s[high]!= max_ch)low++;//都不相等
else if(s[low]==max_ch && s[high]==max_ch)high--;//都相等
else if(s[low]==max_ch && s[high]!=max_ch){//low相等,high不相等
//大小交换
char tmp = s[low];
s[low] = s[high];
s[high] =tmp;
low++;
high--;
}else{//low不相等,high相等
low++;
high--;
}
}
low=0;//low重新归位
}
cout<<s;
return 0;
}
MT1328
#include<bits/stdc++.h>
using namespace std;
int add(int x,int y){
return x+y;
}
int main( )
{
int a,b;
scanf("%d,%d",&a,&b);
cout<<add(a,b);
return 0;
}
MT1329
#include<bits/stdc++.h>
using namespace std;
int fun(int n){
int sum=0;
for(int i=1;i<=n;i+=3){
sum += i;
}
return sum;
}
int main( )
{
int n;
cin>>n;
cout<<fun(n);
return 0;
}
MT1332
#include<bits/stdc++.h>
using namespace std;
int max(int a,int b){
return a>=b ? a : b;
}
int main( )
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<max(max(a,b),max(c,d));
return 0;
}
MT1333
#include<bits/stdc++.h>
using namespace std;
int min(int a,int b){
return a<=b ? a : b;
}
int main( )
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<min(min(a,b),min(c,d));
return 0;
}
MT1334
#include<bits/stdc++.h>
using namespace std;
int getceil(double n){
//直接(int)强制类型转换,就是直接抹掉小数
if(n<0){
return (int)n;
}else{
if(n - (int)n != 0){
//有小数
return ((int)n)+1;
}else{
//没小数
return (int)n;
}
}
}
int main( )
{
double num;
cin>>num;
//printf("%d\n",int(ceil(num)));//>=x
cout<<getceil(num);
return 0;
}
MT1335
#include<bits/stdc++.h>
using namespace std;
int getfloor(double n){
//直接(int)强制类型转换,就是直接抹掉小数
if(n>0){
return (int)n;
}else{//小于0
if(n - (int)n != 0){
//有小数
return ((int)n)-1;
}else{
//没小数
return (int)n;
}
}
}
int main( )
{
double num;
cin>>num;
//printf("%d\n",int(floor(num)));//<=x
cout<<getfloor(num);
return 0;
}
MT1336
#include<bits/stdc++.h>
using namespace std;
int fun(int n){
int ans = 1;
for(int i=n;i>=1;i--){
ans = ans * i;
}
//cout<<ans<<endl;
return ans;
}
int main( )
{
int n;
cin>>n;
cout<<fun(n);
return 0;
}
MT1337
#include<bits/stdc++.h>
using namespace std;
int fun(int x,int n){
int ans=1;
for(int i=1;i<=n;i++)ans = ans *x;
return ans;
}
int main( )
{
int x,n;
cin>>x>>n;
cout<<fun(x,n);
return 0;
}
MT1338
#include<bits/stdc++.h>
using namespace std;
double root(double x,int n){
//x开n次方
double xmin=0,xmax=x,xmid;
while(xmax-xmin>0.000001){
xmid = (xmin+xmax)/2;
double tmp=pow(xmid,n);//中点的n次方
if(tmp>x)xmax=xmid;
else xmin=xmid;
}
return xmid;
}
int main( )
{
//备注:x>1
double x;
int n;
//scanf("%lf %d",&x,&n);
cin>>x>>n;
printf("%lf",root(x,n));
return 0;
}
MT1339
#include<bits/stdc++.h>
using namespace std;
//根据公式写式子
//n不大于100
float fun(float x[],float n){
float s=0,avg,sum=0;//avg代表平均数
for(int i=0;i<n;i++){
sum += x[i];
}
avg = sum/n;
for(int i=0;i<n;i++){
s += (x[i]-avg)*(x[i]-avg);
}
return s;
}
int main( )
{
float n,x[100]={0};
cin>>n;
for(int i=0;i<n;i++)cin>>x[i];
printf("%lf",fun(x,n));
return 0;
}
MT1340
#include<bits/stdc++.h>
using namespace std;
double avg(int arr[],int begin,int end){
double ans,sum=0;
for(int i=begin;i<=end;i++){
sum += arr[i];
}
ans=sum/(end-begin+1);//本题就考了个这
return ans;
}
int main( )
{
int n,arr[100],begin,end;
cin>>n>>begin>>end;
for(int i=0;i<n;i++)cin>>arr[i];
printf("%lf",avg(arr,begin,end));
return 0;
}
MT1342
#include<bits/stdc++.h>
using namespace std;
double f(double x){
return 1/(1+x);
}
int main( )
{
//精度为0.001===>我们这里的高
//求积分
//比如说:分成一千份,然后求每一份的面积
int a,b;//区间
double ans=0 , h=0.001;
cin>>a>>b;
double x = a;
while(x+h <= b){
ans += (f(x) + f(x+h))*h/2;//梯形面积法:上底与下底的和,乘高,除以2
x += h;
}
printf("%lf",ans);
return 0;
}
MT1343
#include<bits/stdc++.h>
using namespace std;
double f(double x){
return 1/(1+x*x);
}
int main( )
{
//精度为0.001===>我们这里的高
//求积分
//比如说:分成一千份,然后求每一份的面积
int a,b;//区间
double ans=0 , h=0.001;
cin>>a>>b;
double x = a;
while(x+h <= b){
ans += (f(x) + f(x+h))*h/2;//梯形面积法:上底与下底的和,乘高,除以2
x += h;
}
printf("%lf",ans);
return 0;
}
MT1344
#include<bits/stdc++.h>
using namespace std;
//C(6,3)=(6*5*4)/(3*2*1)
//C(6,2)=(6*5)/(2*1)
//C(n,m)={(n)*(n-1)*...*(n-m+1)}/{m!}
int C(int n, int m){
int ans=1;
if(n-m<0)return 0;
for(int i=n;i>=n-m+1;i--){
ans *= i;
}
for(int j=m;j>=1;j--){
ans /= j;
}
return ans;
}
int main( )
{
int n,m;
cin>>n>>m;
cout<<C(n,m);
return 0;
}
MT1345
#include<bits/stdc++.h>
using namespace std;
double f(double x){
if(x>=0) return sqrt(x);
else return x+33;
}
int main( )
{
double num;
cin>>num;
printf("%lf",f(num));
return 0;
}
MT1347
#include<bits/stdc++.h>
using namespace std;
//这题就是麻烦,思路还是超级简单的
void fun(int a[],int b[],int c[],int n, char mark){
if(mark=='+')
for(int i=0;i<n;i++)c[i]=a[i]+b[i];
else if(mark=='-')
for(int i=0;i<n;i++)c[i]=a[i]-b[i];
else if(mark=='*')
for(int i=0;i<n;i++)c[i]=a[i]*b[i];
else if(mark=='/')
for(int i=0;i<n;i++)c[i]=a[i]/b[i];
}
int main( )
{
//n<100
int a[100],b[100],c[100];
int n;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)cin>>b[i];
char mark;
cin>>mark;
fun (a,b,c,n,mark);
for(int i=0;i<n;i++) cout<<c[i]<<" ";
return 0;
}
MT1348
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){//求最大公约数,背住了!!!!!
return b ? gcd(b,a%b) : a;
}
int main( )
{
//ax-by=0
//也就是ax=by
//即有一解为:
//x=b;y=a;
//要求x和y的最小值,即求x和y的最大公约数
int a,b;
cin>>a>>b;
int f=gcd(a,b);
cout<<b/f<<" "<<a/f;
return 0;
}
MT1349
#include<bits/stdc++.h>
using namespace std;
/*int gcd(int a,int b){//求最大公约数,背住了!!!!!
return b ? gcd(b,a%b) : a;
}*/
int gcd(int a,int b){
int r;
while(b!=0){
r=a%b;
a=b;
b=r;
}
return a;
}
int main( )
{
int a,b;
cin>>a>>b;
cout<<gcd(a,b);
return 0;
}
MT1350
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
return b ? gcd(b,a%b) : a;
}
int main( )
{
int a,b,c,d,e,f;// a/b c/d e/f
int mark=0;//标志位,表示是正数还是负数
char s;
scanf("%d/%d%c%d/%d",&a,&b,&s,&c,&d);
if(s=='+'){
e=a*d+b*c;//分子
f=b*d;//分母
int yue=gcd(e,f);//约分
printf("%d/%d",e/yue,f/yue);
}
if(s=='-'){
e=a*d-b*c;
f=b*d;
if(e==0){
printf("0");
return 0;
}else if(e<0){
e=-e;
mark=1;//表示负数
}
int yue = gcd(e,f);
if(mark==0)printf("%d/%d",e/yue,f/yue);
else printf("-%d/%d",e/yue,f/yue);
}
return 0;
}
MT1352
#include<bits/stdc++.h>
using namespace std;
int is_Prime(int n){
bool flag=true;
if(n==1)flag=false;
else if(n==2)flag=true;
else if(n%2==0)flag=false;//判断是否为偶数
for(int i=3;i<=sqrt(n);i=i+2){//处理其余的奇数
if(n%i==0)flag=false;
}
return flag;
}
int main( )
{
//n<10000
int n;
cin>>n;
for(int i=2;i<=n;i++){
if(is_Prime(i))cout<<i<<" ";
}
return 0;
}
MT1353
#include<bits/stdc++.h>
using namespace std;
int change(int m,int c){
int money = c-m;
int res = 0;
int value[6]={1,2,5,10,20,50};
for(int i=5;i>=0;i--){
int cnt = money/value[i];//要找几张50的
res += cnt;//总张数
money -= cnt*value[i];//还差多少钱
}
return res;
}
//求,要找几张
int main( )
{
//要找50,那就直接找一张50的,不可能找2张20+1张10块的
int m,c;
cin>>m>>c;
cout<<change(m,c);
return 0;
}
MT1354
#include<bits/stdc++.h>
using namespace std;
//全部换成分
int change(int m,int c){
int remain = c - m;//要找的钱
int sum = 0;
int a[12]={1,2,5,10,20,50,100,200,500,1000,2000,5000};
for(int i=11;i>=0;i--){
int cnt =remain/a[i];
sum += cnt;
remain -= cnt * a[i];
}
return sum;
}
int main( )
{
double m,c;//m为商品价格,c为顾客付款
int ma,ca;
cin>>m>>c;
ma=m*100,ca=c*100;
cout<<change(ma,ca);
return 0;
}
MT1355
#include<bits/stdc++.h>
using namespace std;
int f(int n){
while(1){
if( n & (n<<1)) n++;//n & (n<<1)结果为1,那就不是稀疏二进制 ,那就n++找下一位
else return n;
}
}
//稀疏二进制:二进制表示二进制表示下,不包含任何连续的1
int main( )
{
//思路:相邻的两位与运算,如果全是0,就是稀疏二进制
//方法:原数左移一位,然后与原数进行与运算
//eg: 10110
//左移一位:101100
//连数与运算:为1=>不是稀疏二进制
//eg: 10101
//左移一位:101010
//连数与运算:为0=>是稀疏二进制
int n;
cin>>n;
cout<<f(n);
return 0;
}
MT1356
#include<bits/stdc++.h>
using namespace std;
//求一个十进制数n的二进制形式的位数=>log2(n)+1
//eg:8的二进制是:1000=>log2(8)+1==4位
//补充:log2就写log2,log10就写log10
int fun(int n){
int i=0;//右指针
int j=log2(n);//左指针
while(i<=j){//为什么要加 = ? 因为可能位数是奇数位
//1&0==0 1&1==1
if((1&(n>>i)) != (1&(n>>j)))return 0;
i++,j--;
}
return 1;
}
int main( )
{
int n;
cin>>n;
if(fun(n))cout<<"YES";
else cout<<"NO";
return 0;
}
MT1357
#include<bits/stdc++.h>
using namespace std;
//unsigned char只有8位
//方法: "分别" 将该数右移4位,左移四位 =>然后或运算
unsigned char change(unsigned char n){
return (n>>4)|(n<<4);
}
int main( )
{
unsigned char n;
cin>>n;
printf("%c",change(n));
return 0;
}
MT1358
#include<bits/stdc++.h>
using namespace std;
//右移 0位 求的就是第一位,
//右移 k-1位 求的就是第k位
int getbit(int n,int k){
return (n>>(k-1)) & 1;//&1得到本身
}
int main( )
{
int n,k;
cin>>n>>k;
cout<<getbit(n,k);
return 0;
}
MT1359
#include<bits/stdc++.h>
using namespace std;
//循环移位指的是:
//eg:1101 向右移1位 => 1 101
//eg:1010 向左移1位 => 010 1
//就是把移出的那个数再移回来
//题目说了:是4位2进制 ,而实际上unsigned有32位,所以我们可以使用:先左移28位,再右移28位,即可将用不到的位置进行清零
//且0<value<15
unsigned move(unsigned value,int n){
unsigned res;
if(n==0) res=value;
else if(n>0) res = (value>>n) | (value<<(4-n));
else res = (value<<-n) | (value>>(4+n));
//sizeof是看他有多少个字节,x8就可以知道他有多少位
//即:1个字节=8位 1字节(byte) = 8位(bit)
res = res<<(sizeof(unsigned)*8-4);//如果unsigned是4,那就是4*8-4=32-4=28
res = res>>(sizeof(unsigned)*8-4);
return res;
}
int main( )
{
unsigned num;
int n = 0;
cin>>num>>n;
//第一步,n先余4=>因为移位7次,与移位3次效果一致
n=n%4;
cout<<move(num,n);
return 0;
}
//方二:用数组
/*#include<bits/stdc++.h>
using namespace std;
int f(int b[]){//将数组转换成十进制
int sum=0;
int tmp=1;
for(int i=3;i>=0;i--){
sum += b[i]*tmp;
//cout<<sum<<"-";
tmp *= 2;
}
//cout<<endl;
return sum;
}
int main( )
{
int value ,n;
cin>>value>>n;
n=n%4;
int a[4]={0},b[4]={0};
for(int i=0;i<=3;i++){
a[i]=value%2;
value=value/2;
}
for(int i=3;i>=0;i--){//反转一下
b[i]=a[3-i];
}
if(n==0){
cout<<f(b);
return 0;
}
if(n>0){
while(n){//右移,b[3]被挤出
int tmp=0;
tmp=b[3];
for(int i=3;i>=1;i--)b[i]=b[i-1];
b[0]=tmp;
n--;
}
cout<<f(b);
return 0;
}
if(n<0){
n = -n;
while(n){//左移,b[0]被挤出
int tmp=0;
tmp=b[0];
for(int i=0;i<=2;i++)b[i]=b[i+1];
b[3]=tmp;
n--;
}
cout<<f(b);
return 0;
}
return 0;
}*/
MT1360
#include<bits/stdc++.h>
using namespace std;
void left(char str[],int n,char dest[]){
int i;
for(i=0;i<n;i++){
dest[i]=str[i];
}
dest[n]='\0';
printf("%s\n",dest);
}
void right(char str[],int n,char dest[]){
int m,j=0;
m=strlen(str);//str的总长 13
//cout<<m<<" "<<m-n<<endl;
for(int i=m-n;i<m;i++){//示例Coding is fun中,有13个字符,最后三个字符的下标是10,11,12
//cout<<str[i]<<" ";
dest[j]=str[i];
j++;
}
//cout<<endl;
dest[n]='\0';
printf("%s\n",dest);
}
int main( )
{
char str[80],dest[80];
cin.getline(str,80);
left(str,3,dest);
right(str,3,dest);
return 0;
}
MT1363
#include<bits/stdc++.h>
using namespace std;
void del(char s[]){
int len = strlen(s);
for(int i=0;i<len;i++)
if(s[i]=='*'){
for(int j=i;j<len;j++){
s[j]=s[j+1];
}
len--;
i--;
}
}
int main( )
{
char s[]="****ABB*****DDDFFF******";
del(s);
printf("%s",s);
return 0;
}
MT1366
#include<bits/stdc++.h>
using namespace std;
int fun(int n){
int sum;
if(n==1)return 1;
else sum = n*n +fun(n-1);
return sum;
}
int main( )
{
int n;
cin>>n;
cout<<fun(n);
return 0;
}
MT1367
#include<bits/stdc++.h>
using namespace std;
int ack(int m,int n){
if(m==0)return n+1;
else if(n==0)return ack(m-1,1);
else return ack(m-1,ack(m,n-1));
}
int main( )
{
int x,y;
cin>>x>>y;
cout<<ack(x,y);
return 0;
}
MT1368
#include<bits/stdc++.h>
using namespace std;
void fun(int x){
if(x==0)return;
cout<<x%10;
fun(x/10);
}
int main( )
{
int x;
cin>>x;
fun(x);
return 0;
}
MT1370
//杨辉三角形
/*#include<bits/stdc++.h>
using namespace std;
//1
//1 1
//1 2 1
//1 3 3 1
//1 4 6 4 1
int yang(int i,int j){
if(j==1 || j==i)return 1;//每一行的第一个和每一行的最后一个
else return yang(i-1,j)+yang(i-1,j-1);//否则就是上一行同一列的数+上一行上一列的数
}
int main( )
{
int n;//一共几行
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("%d%c", yang(i,j) , j==i?'\n':' ');
}
}
return 0;
}*/
#include<bits/stdc++.h>
using namespace std;
//1234=>1+2+3+4=10=>1
int fun(int n){
if(n<10)return n;
int sum = 0;//n>=10
while(n>0){
sum += n%10;
n /= 10;
}
return fun(sum);
}
int main( )
{
int n;
cin>>n;
cout<<fun(n);
return 0;
}
MT1372
#include<bits/stdc++.h>
using namespace std;
//NXN
void fun(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
printf("0 ");
}
printf("\n");
}
}
int main( )
{
int n;
cin>>n;
fun(n);
return 0;
}
MT1374
#include<bits/stdc++.h>
using namespace std;
/*
请编写一个函数求m至n之间的所有Pronic数字,
如果某个数字是两个"连续数字"的乘积, 则该数字被称为Pronic数。
比如6 = 2 x 3,72 = 8 x 9。
*/
bool isPronic(int x){
int a=sqrt(x);//转换成int,直接抹去小数部分
if(a*(a+1)==x)return 1;
else return 0;
}
int main( )
{
int m,n;
cin>>m>>n;
for(int i=m;i<=n;i++){
if(isPronic(i)){
printf("%d ",i);
}
}
return 0;
}
MT1375
#include<bits/stdc++.h>
using namespace std;
//4, 7, 44,47, 74,……44744…
/*像二叉树=======>找规律
4, 7
44, 47, 74, 77
444,447,474,477 ,744,747,774,777
*/
int fun(int n){
if(n==0)return 0;//递归的终点
if(n%2==1)return 10*fun((n-1)/2)+4;//是第奇数个,末尾+4
else return 10*fun((n-2)/2)+7;//是第偶数个,末尾+7
}
//5 => (5-1)/2==2 70+4
//6 => (6-2)/2==2 70+7
//7 => (7-1)/2==3 440+4
int main( )
{
int n;
cin>>n;
cout<<fun(n);
return 0;
}
MT1377
#include<bits/stdc++.h>
using namespace std;
//逆元定义:满足 a * b≡1(mod m),称b为a模乘逆元(b<m)。
//注:x=n(mod m)意思是x除以m的余数是n。
//即x=n(mod m) x%m=n a * b≡1(mod m)==> (a*b)%m==1 ==>b是逆元,a和m已知
int fun(int a,int m){
for(int b=1;b<m;b++){
if((a*b)%m==1)return b;
}
return -1;
}
int main( )
{
int a,m;
cin>>a>>m;
cout<<fun(a,m);
return 0;
}
MT1379
#include<bits/stdc++.h>
using namespace std;
int operate(int a,int b,char op){
if(op=='+')return a+b;
else return a-b;
}
int main( )
{
int a,b;
char op;
scanf("%d%c%d",&a,&op,&b);
cout<<operate(a,b,op);
return 0;
}
MT1380
#include<bits/stdc++.h>
using namespace std;
void operate(int a,int b,char op){
if(op=='*')printf("%d",a*b);
else if(op=='%')printf("%d",a%b);
else printf("%lf",(double)a/b);
}
int main( )
{
int a,b;
char op;
scanf("%d%c%d",&a,&op,&b);
operate(a,b,op);
return 0;
}
MT1381
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[10];
for(int i=0;i<10;i++)cin>>a[i];
for(int i=9;i>=0;i--)cout<<a[i]<<" ";
return 0;
}
MT1382
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[10];
for(int i=0;i<10;i++)cin>>a[i];
for(int i=0;i<10; i++ ){
if(i%2==1)
cout<<a[i]<<" ";
}
return 0;
}
MT1385
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[10],n;
int flag=-1;
for(int i=0;i<10;i++)cin>>a[i];
cin>>n;
for(int i=0;i<10;i++){
if(a[i] == n){
flag=i;
break;
}
}
if(flag==-1)cout<<"No";
else cout<<flag;
return 0;
}
MT1386
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[200],n=0;
while(1){
scanf("%d",&a[n]);
if(a[n]==-9999)break;
n++;
}
for(int i=0;i<n-1;i++){
if(a[i]==a[n-1]){
printf("%d",i+1);//(序号从1开始)
return 0;
}
}
cout<<"no such number";
return 0;
}
MT1387
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100],n;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
int id;//要删除的位置(不是下标)
cin>>id;
for(int i=id-1;i<n-1;i++){
a[i]=a[i+1];
}
for(int i=0;i<n-1;i++)cout<<a[i]<<" ";
return 0;
}
MT1388
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100],n;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
int id;//要删除的元素
cin>>id;
for(int i=0;i<n;i++){
if(a[i]!=id)
cout<<a[i]<<" ";
}
return 0;
}
MT1389
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100],n=10;
for(int i=0;i<n;i++)cin>>a[i];
int id;//要删除的元素
cin>>id;
sort(a,a+n);
int flag=0;//代表新数组没有元素
for(int i=0;i<n;i++){
if(a[i]==id){//找到了
continue;
}else{
//不相等
cout<<a[i]<<" ";
flag=1;//有元素输出则说明新数组有元素
}
}
if(flag==0)cout<<-1;
return 0;
}
MT1390
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100],n=10;
for(int i=0;i<n;i++)cin>>a[i];
int id;//要删除的元素
cin>>id;
sort(a,a+n);
int flag=0;//代表新数组没有元素
for(int i=0;i<n;i++){
if(a[i]==id){//找到了
continue;
}else{
//不相等
cout<<a[i]<<" ";
flag=1;//有元素输出则说明新数组有元素
}
}
if(flag==0)cout<<-1;
return 0;
}
MT1393
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100];
for(int i=0;i<10;i++)cin>>a[i];
//有时候,某个数的出现次数会大于2次
bool iscount[10]={0};
for(int i=0;i<10;i++){
if(iscount[i]==true)continue;//如果这个数已经出现过了,那就不用再遍历了
int count=1;//默认当前这个数出现的次数为1
for(int j=i+1;j<10;j++){
if(a[j]==a[i]){
//重复出现了
count++;
iscount[j]=true;//标记为已重复出现
}
}
if(count>1)cout<<a[i]<<" ";//出现次数大于1
}
return 0;
}
MT1394
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100];
for(int i=0;i<10;i++)cin>>a[i];
//有时候,某个数的出现次数会大于2次
bool iscount[10]={0};
for(int i=0;i<10;i++){
if(iscount[i]==true)continue;//如果这个数已经出现过了,那就不用再遍历了
int count=1;//默认当前这个数出现的次数为1
for(int j=i+1;j<10;j++){
if(a[j]==a[i]){
//重复出现了
count++;
iscount[j]=true;//标记为已重复出现
}
}
cout<<a[i]<<" "<<count<<endl;
}
return 0;
}
MT1395
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[500];//n<500
int n;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
//有时候,某个数的出现次数会大于2次
bool iscount[n]={0};
for(int i=0;i<n;i++){
if(iscount[i]==true)continue;//如果这个数已经出现过了,那就不用再遍历了
int count=1;//默认当前这个数出现的次数为1
for(int j=i+1;j<n;j++){
if(a[j]==a[i]){
//重复出现了
count++;
iscount[j]=true;//标记为已重复出现
}
}
cout<<a[i]<<" "<<count<<endl;;
}
return 0;
}
MT1396
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n);
for(int i=0;i<n;i++)cout<<a[i]<<" ";
return 0;
}
MT1397
#include<bits/stdc++.h>
using namespace std;
void fun(int a[],int n){//O(n^2)的复杂度
for(int i=0;i<n-1;i++){
int min=i;//假设为最小的一个
bool flag=false;//标记位
for(int j=i;j<n;j++){
if(a[j]<a[min]){
min=j;
flag=true;//发生了变化
}
}
if(flag==true){//最小数的下标发送了改变,就交换一下
int tmp=a[i];
a[i]=a[min];
a[min]=tmp;
}
}
}
int main( )
{
int a[10],n=10;
for(int i=0;i<10;i++)cin>>a[i];
//sort(a,a+10);
fun(a,n);//选择排序
for(int i=0;i<10;i++)cout<<a[i]<<" ";
return 0;
}
MT1398
#include<bits/stdc++.h>
using namespace std;
void fun(int a[],int n){
//插入排序是不断将数拿进来,确保一定范围内的数的顺序是对的
//选择排序是找最小值
for(int i=1;i<n;i++){
int j=i;
while(j>=1 && a[j-1]>a[j]){//前面一个数小于当前这个数
swap(a[j-1],a[j]);
j--;//不断前移
}
}
}
int main( )
{
int a[10],n=10;
for(int i=0;i<10;i++)cin>>a[i];
//sort(a,a+10);
fun(a,n);//插入排序
for(int i=0;i<10;i++)cout<<a[i]<<" ";
return 0;
}
MT1399
#include<bits/stdc++.h>
using namespace std;
void fun(int a[],int n){//冒泡排序是不断找最大数
for(int i=0;i<n-1;i++){
for(int j=0 ; j<n-1-i ; j++){
if(a[j]>a[j+1])swap(a[j],a[j+1]);//保证最大的数在后边
}
}
}
int main( )
{
int a[10],n=10;
for(int i=0;i<n;i++)cin>>a[i];
//sort(a,a+10);
fun(a,n);//冒泡排序
for(int i=0;i<n;i++)cout<<a[i]<<" ";
return 0;
}
MT1400
#include<bits/stdc++.h>
using namespace std;
void fun(int a[],int low,int high){//快速排序是递归
//a1,a2,a3,...an
//a1作为参照物,然后<a1放左边, >a1放右边
//然后将左边的进刚刚的操作,右边的数也进行刚刚的操作
if(low>=high)return ;//结束递归的标志
int i=low;//代表左边的
int j=high;//代表右边的
//目标:左小右大
int key=a[low];//参照物
while(i<j){
while(i<j && key <= a[j])j--;//j左移 =>直到key>a[j]时跳出循环(右边不大跳出循环)
if(i<j) a[i++] = a[j];//右 "赋给" 左
while(i<j && key > a[i])i++;//i右移=>直到key<=a[i]时跳出循环(左边不小跳出循环)
if(i<j) a[j--] = a[i];//左 "赋给" 右
}
a[i]=key;//最后把key放到最中心的位置(也不是最中心的位置,就i==j的位置)
fun(a,low,i-1);//左边快排
fun(a,i+1,high);//右边快排
}
int main( )
{
int a[10],n=10;
for(int i=0;i<n;i++)cin>>a[i];
//sort(a,a+10);
fun(a,0,n-1);//快速排序
for(int i=0;i<n;i++)cout<<a[i]<<" ";
return 0;
}
MT1404
#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main( )
{
int a[10],len=10,n,m;
for(int i=0;i<len;i++)cin>>a[i];
cin>>n>>m;
sort(a,a+len);
sort(a+n,a+m+1,cmp);//注意细节:是a+m+1 =>要排序[0,n-1]则写成sort(a,a+n);
for(int i=0;i<len;i++)cout<<a[i]<<" ";
return 0;
}
MT1405
#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main( )
{
int a[10],len=10,n;
for(int i=0;i<len;i++)cin>>a[i];
cin>>n;
sort(a,a+len);
sort(a+n,a+len,cmp);//注意细节:是a+m+1 =>要排序[0,n-1]则写成sort(a,a+n);
for(int i=0;i<len;i++)cout<<a[i]<<" ";
return 0;
}
MT1408
#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main( )
{
int a[100],n,id;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
cin>>id;
a[n]=id;
sort(a,a+n+1);//全放进去再排序
for(int i=0;i<n+1;i++)cout<<a[i]<<" ";
return 0;
}
MT1410 逆时针旋转数组
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100];
int n,m;
cin>>n>>m;//n个数,m次
for(int i=0;i<n;i++)cin>>a[i];
//上一题是顺,这一题是逆
//旋转说白了,就是循环数组,数组前面走几个,后面再补回来
while(m){
int tmp = a[0];//记录第一个数
for(int i=0;i<n-1;i++)a[i]=a[i+1];
a[n-1]=tmp;
m--;
}
for(int i=0;i<n;i++)cout<<a[i]<<" ";
return 0;
}
MT1411 顺时针旋转数组
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100];
int n,m;
cin>>n>>m;//n个数,m次
for(int i=0;i<n;i++)cin>>a[i];
//旋转说白了,就是循环数组,数组后面走几个,前面再补回来
while(m){
int tmp = a[n-1];//记录最后一个数
for(int i=n-1;i>0;i--)a[i]=a[i-1];
a[0]=tmp;
m--;
}
for(int i=0;i<n;i++)cout<<a[i]<<" ";
return 0;
}
MT1412 合并
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,m;
cin>>n>>m;
int a[100],b[100];
for(int i=0;i<n;i++)cin>>a[i];//n
for(int i=0;i<m;i++)cin>>b[i];//m
for(int i=0;i<m;i++)a[n+i]=b[i];
sort(a,a+n+m);
for(int i=0;i<n+m;i++)cout<<a[i]<<" ";
return 0;
}
MT1414
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//交集中相同元素只会出现一次或者重复的元素只统计一次
int n,m;
cin>>n>>m;
unordered_set<int>a,b;//去重
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
a.insert(tmp);//n
}
for(int i=0;i<m;i++){
int tmp2;
cin>>tmp2;
b.insert(tmp2);//m
}
int cnt=0;
for (auto it = a.begin(); it != a.end(); it++) {
for (auto it1 = b.begin(); it1 != b.end(); it1++) {
if((*it)==(*it1)){
cnt++;//在b里面找a
}
}
}
cout<<cnt;
return 0;
}
MT1415
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//统计这些子数组的数量并输出
//举个例子,你就懂了:
/*
1 1 1 3 3 4=>答案是:
(1 1 1) (1 1)(1 1)(1 1) (1) (1) (1)=>C(3,3)+C(3,2)+C(3,1)
(3 3) (3) (3)=>C(2,2)+C(2,1)
(4)=>C(1,1)
公式回顾:
C(n,m)=>(n*(n-1)*...*(n-m+1))/(m*(m-1)*...*1)
C(n,0)+C(n,1)+C(n,2)+..C(n,n)=2^n
即:C(n,1)+C(n,2)+..C(n,n)=2^n-1
所以,上题的答案是2^3-1 + 2^2-1 + 2^1-1
*/
int a[10]={0};
bool isCount[10]={0};
int n,ans=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
if(isCount[i]==true)continue;
int count=1;
for(int j=i+1;j<n;j++){
if(a[i]==a[j]){
count++;
isCount[j]=true;
}
}
ans += pow(2,count)-1;
}
cout<<ans;
return 0;
}
MT1416
#include<bits/stdc++.h>
using namespace std;
int main( )
{
/*
n=7 k=2
1 2 3 4 5 6 7
k=2表示子数组最多包含2个偶数
(1 2 3 4 5)=>最长:答案是5 个数
(1 2 3 4)
(2 3 4)
(2 3 4 5)
(3 4 5 6)
*/
int n,k,a[100];//n<100
cin>>n>>k;//n个数,k个偶数
for(int i=0;i<n;i++)cin>>a[i];
int count=0;//计数器
//当count==k+1,说明已经多出一个,那就输出,除了这数之外,前面子数组的元素个数
//这里直接输出i,因为i是下标,下标比个数少1
for(int i=0;i<n;i++){
if(a[i]%2==0)count++;
if(count==k+1){
cout<<i;
return 0;
}
}
cout<<n;//如果全数组都凑不够k个偶数,或者没有遇到k+1个偶数,那么整个数组的长度就是答案
return 0;
}
MT1417
#include<bits/stdc++.h>
using namespace std;
int main( )
{
/*
n=4 m=10
1 2 3 4
找满足条件的子序列的个数
1 1
1*2<10 2
1*2*3=6<10 3
1*2*3*4>10
2 4
2*3=6<10 5
2*3*4>10
3 6
3*4>10
4 7
*/
int n,m;
int a[100];
cin>>n>>m;
for(int i=0;i<n;i++)cin>>a[i];
int ans=0;
for(int i=0;i<n;i++){
int temp=1;
for(int j=i;j<n;j++){
temp *= a[j];
if(temp<=m)ans++;
else break;
}
}
cout<<ans;
return 0;
}
MT1418
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
cin>>n;
int sum=0;
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
sum += tmp;
}
cout<<sum;
return 0;
}
MT1420
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,a[100]={0};
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n);//中值需要排序
if(n%2==1)cout<<a[n/2+1-1];//下标=位置-1
else cout<<(a[n/2-1]+a[n/2])/2;
/*
4=n
53 70 33 89
0 1 2 3
a[1]+a[2]==a[4/2-1]+a[4/2]
*/
return 0;
}
MT1421
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,a[1000]={0};
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
int ans=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if( (a[i]^a[j])%2 != 0)ans++;
}
}
cout<<ans;
return 0;
}
MT1422
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,a[100]={0};//所有元素均为非负数。
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
int ans=0;
for(int i=0;i<n;i++){
if(a[i]==0)ans++;//注意:非负数,指的是0和正数
while(a[i]){
ans++;
a[i] /= 10;
}
}
cout<<ans;
return 0;
}
MT1425
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//1,2,5,8,15,28,51
//1+2+5=8
//2+5+8=15
//从第4项开始,第n项=前三项的和
int n;
cin>>n;
int a[1000]={1,2,5};
if(n<=3){
cout<<a[n-1];
return 0;
}else{
for(int i=3;i<n;i++){
a[i]=a[i-3]+a[i-2]+a[i-1];
}
cout<<a[n-1];
}
return 0;
}
MT1427
#include<bits/stdc++.h>
using namespace std;
bool isAns(int n){
while(n){
int i=n%10;
if(i!=2 && i!=3 && i!=5 && i!=7){
return false;
}
n /= 10;
}
return true;
}
int main( )
{
//每位数字都是素数,即2、3、5、7
int n;
cin>>n;
//2 3 5 7
//22 23 25 27
//32 33 35 37
//52 53 55 57
//222 223...
int count=0;//计数器
//暴力枚举
for(int i=2;;i++){//从2开始
if(isAns(i)){
count++;
}
if(count==n){
cout<<i;
return 0;
}
}
return 0;
}
MT1428
#include<bits/stdc++.h>
using namespace std;
int fun(int n){
if(n==1)return 1;
for(int i=2;i<=n;i++){
if(n%i==0)return i;
}
return -1;
}
/*
6=>1 2 3 4 5 6
=>1的最小素数因子就是1
=>2的最小素数因子就是2
=>3的最小素数因子就是3
=>4的最小素数因子就是2 =>4%2==0
=>5的最小素数因子就是5
=>6的最小素数因子就是2 =>6%2==0
1 2 3 2 5 2
*/
int main( )
{
//整数N的最小素数因子是它的因子中最小的素数
//注:所有偶数的最小素数因子为2,
//素数是它自己的最小素数因子,1的为1。
int n;
cin>>n;
for(int i=1 ; i<=n ; i++){
cout<<fun(i)<<" ";
}
return 0;
}
MT1429
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;//给定一个N个数的数组。
cin>>n;
int a[100]={0};
int temp=1;
for(int i=0;i<n;i++){给定一个N个数的数组。
cin>>a[i];
temp *= a[i];//初始数组的所有元素的乘积。
}
for(int i=1;;i++){//找到一个特殊的最小正整数K,
//用K构成一个新数组,新数组中所有的元素都是K
int j=pow(i,n);
//要求:这个新数组的所有元素的乘积大于初始数组的所有元素的乘积。
if(j>temp){
cout<<i;// 输出K。
return 0;
}
}
return 0;
}
MT1432
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,a[100]={0},b[100]={0};
cin>>n;//有两个含N个整型元素的数组
//从键盘输入A数组所有元素,将其一一“赋值”给B数组对应的元素
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)b[i]=a[i];
if(n==1){
cout<<b[0]<<" ";
return 0;
}
//最后输出A数组下标为奇数
for(int i=1;i<n;i=i+2)cout<<a[i]<<" ";
cout<<endl;
//并输出B数组下标为偶数的元素
for(int i=0;i<n;i=i+2)cout<<b[i]<<" ";
return 0;
}
MT1433
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100],b[100],n;
cin>>n;//给定一个大小为N的数组arr[],其中每个元素都在0到N-1的范围内。
for(int i=0;i<n;i++)cin>>a[i];
//重新排列给定数组,使arr[i]变为arr[arr[i]]
for(int i=0;i<n;i++)b[i]=a[a[i]];
//输出新数组。
for(int i=0;i<n;i++)cout<<b[i]<<" ";
return 0;
}
MT1434
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[100];
cin.getline(a,100);//输入一个字符串(包含26个英文字母大小写及 . 空格,不含其他字符)
int len=strlen(a);
int sum=0,num=0;
for(int i=0;i<=len;i++){
int loc=a[i]-'0';
//其中连续的数字作为一个整数
if(loc>=0 && loc<=9){
num = num*10 + loc;
}else{
sum += num;
num = 0;//记得用完之后进行清零
}
}
cout<<sum;//输出这些整数的和
return 0;
}
MT1435
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,sum=0,a[50];
cin>>n;//有N个盒子
for(int i=0;i<n;i++)cin>>a[i];//里面装着数量不等的玩具,分给N个宝宝
//确保第i个宝宝分到i个玩具,即第1个宝宝分配1个玩具,第2个宝宝分配2个玩具,...第n个宝宝分配n个玩具
//加起来一共有(1+2+...+n)=(n+1)*n/2
for(int i=0;i<n;i++)sum += a[i];
//判断玩具是否足够且"刚好"分配,输出YES或者NO。
printf("%s",sum==(n+1)*n/2 ? "YES" : "NO");
return 0;
}
MT1437
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100][100],n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
//编写程序遍历n(< 100)阶方阵主对角线的元素
//即a[i][i]
for(int i=0;i<n;i++){
cout<<a[i][i]<<" ";
}
return 0;
}
MT1438
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100][100],n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
//上一题是对角线,就是左上->右下
//这一题是次对角线,是右上->左下
//编写程序遍历n(< 100)阶方阵次对角线的元素。
for(int i=0;i<n;i++){
cout<<a[i][n-1-i]<<" ";
}
//以3行3列为例
/*
n=3 =>[i][n-i-1]
第1数 1:[0][2] =>[i][3-0-1]
第2数 2:[1][1] =>[i][3-1-1]
第2数 3:[2][0] =>[i][3-2-1]
*/
return 0;
}
MT1440
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[3][3];
//输入3X3整型数组,所有元素在0到9之间
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>a[i][j];
}
}
//输出右上角所有元素
for(int i=0;i<3;i++){//遍历行
for(int j=0;j<3;j++){//遍历列
if(j>=i)cout<<a[i][j]<<" ";
else cout<<" ";
}
cout<<endl;
}
/*
[i][j]
[0][0] [0][1] [0][2]==>规律就是:j>=i 列>=行
[1][1] [1][2]
[1][0] [2][2]
[2][0] [2][1]
*/
return 0;
}
MT1441
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[3][3];
//输入3X3整型数组,所有元素在0到9之间
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>a[i][j];
}
}
//输出右上角所有元素
for(int i=0;i<3;i++){//遍历行
for(int j=0;j<3;j++){//遍历列
if(i+j>=2)cout<<a[i][j]<<" ";
else cout<<" ";
}
cout<<endl;
}
/*
[i][j]=> i+j>=2
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
*/
return 0;
}
MT1442
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[3][3];
//输入3X3整型数组,所有元素在0到9之间
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>a[i][j];
}
}
//输出右上角所有元素
for(int i=0;i<3;i++){//遍历行
for(int j=0;j<3;j++){//遍历列
if(i+j<=2)cout<<a[i][j]<<" ";
else cout<<" ";
}
cout<<endl;
}
/*
[i][j]=> i+j<=n-1==2
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
*/
return 0;
}
MT1445
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int m,n,a[100][100],odd=0;
cin>>m>>n;//输入一个M×N的正整数数组,统计奇数和偶数的个数
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
if(a[i][j]%2)odd++;//奇数个数
}
}
//输出奇数总数减去偶数总数的差
printf("%d-%d=%d", odd , m*n-odd, odd-(m*n-odd) );
return 0;
}
MT1446
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int m,n,a[100][100];
cin>>m>>n;//第一行输入正整数M和N(均小于100)
//其后M行每行输入N个元素。
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int ans=1;
for(int i=0;i<m;i++){
int sum=0;
for(int j=0;j<n;j++){
//计算每一行的元素累加和
sum+=a[i][j];
}
ans *= sum;
}
cout<<ans;
return 0;
}
MT1447
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int m,n,a[100][100];
cin>>m>>n;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
for(int i=0;i<m;i++){
int sum=0;
for(int j=0;j<n;j++){
//计算每一行的元素累加和
sum+=a[i][j];
}
cout<<sum<<" ";
}
cout<<endl;
for(int i=0;i<n;i++){
int sum=0;
for(int j=0;j<m;j++){
//计算每一行的元素累加和
sum+=a[j][i];//这里是a[j][i] , 不是a[i][j]
}
cout<<sum<<" ";
}
return 0;
}
MT1448
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int m,n,a[100][100];
cin>>m>>n;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){//运行超时一定是i和j写反了
cin>>a[i][j];
}
}
int sum=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i==0||i==m-1||j==0||j==n-1)
sum += a[i][j];
}
}
cout<<sum;
return 0;
}
MT1450
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int m,a[100][100];
cin>>m;
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
}
}
bool flag=true;
for(int i=0;i<m;i++){
for(int j=0;j<i;j++){
if(a[i][j]!=a[j][i])
flag = false;
}
}
printf("%s",flag?"YES":"NO");
return 0;
}
MT1451
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int row,col,n,a[100][100]={0};
cin>>n;
row=0;
col=(n-1)/2;
a[row][col] = 1;
for(int i=2;i<=n*n;i++){
if(a[(row-1+n)%n][(col+1)%n]==0){
row = (row-1+n)%n;
col = (col+1)%n;
}else{
row = (row+1)%n;
}
a[row][col] = i;
}
for(row=0;row<n;row++){
for(col=0;col<n;col++){
cout<<a[row][col]<<" ";
}
cout<<endl;
}
return 0;
}
MT1453
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,a[100][100];
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
for(int i=n-1;i>=0;i--){
for(int j =n-1;j>=0;j--){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
MT1454
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
scanf("%d\n",&n);
char arr[100];
for(int i=0;i<n;i++){
//cin>>arr[i];//=>char字符串不要使用cin
scanf("%c",&arr[i]);
}
for(int i=0;i<n;i++){
printf("%c",arr[i]);
}
return 0;
}
/*#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
cin>>n;
scanf("%d\n",&n);
char arr[100];
cin.getline(arr,100);
cout<<arr;
return 0;
}*/
MT1455
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
scanf("%d\n",&n);
char arr[100];
scanf("%s",arr);
printf("%s",arr);
return 0;
}
MT1456
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
scanf("%d\n",&n);
char arr[100];
fgets(arr,n+1,stdin);
fputs(arr,stdout);
return 0;
}
/*#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
scanf("%d\n",&n);
char arr[100];
fflush(stdin);//c语言清除缓存区
for(int i=0;i<n;i++){
scanf("%c",&arr[i]);
}
for(int i=0;i<n;i++){
printf("%c",arr[i]);
}
return 0;
}*/
MT1458
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[3][8];
for(int i=0;i<3;i++){//3组字符串
scanf("%s",a[i]);
}
for(int i=0;i<3;i++){
printf("%s\n",a[i]);
}
return 0;
}
MT1459
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80];
cin.getline(a,80);
printf("%d %d",strlen(a),sizeof(a));// 5 80
//strlen是遇到\0则停止计数
return 0;
}
MT1462
//字符串复制案例:cin.getline(cin,_);与getline(_,_);的用法及区别
#include<bits/stdc++.h>
using namespace std;
int main( )
{
/*
string s1,s2;
//注:cin.getline(name,sizeof(name));这种用法的前提是name是char数组
//string类型的输入一行是这样写的
getline(cin,s1);
s2=s1;
cout<<s2;
*/
char s1[80],s2[80];
cin.getline(s1,80);//cin.getline(s1,sizeof(s1));
int len =strlen(s1);
for(int i=0;i<len;i++){
s2[i]=s1[i];
}
s2[len]='\0';
cout<<s2;
return 0;
}
MT1463
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80],b[80];
scanf("%s",a);
scanf("%s",b);
strcat(a,b);//a+b的效果
printf("%s",a);
return 0;
}
MT1464
#include<bits/stdc++.h>
using namespace std;
int main( )
{
/*char a[80],b[80];
cin.getline(a,80);
cin.getline(b,80);
int lena = strlen(a);
int lenb = strlen(b);
for(int i=lena,j=0;i<lena+lenb;i++,j++){
a[i]=b[j];
}
a[lena+lenb]='\0';
printf("%s",a);*/
string s1,s2;
getline(cin,s1);
getline(cin,s2);
s1 = s1+ s2;
cout<<s1;
return 0;
}
MT1465
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80],b[80];
scanf("%s",a);
scanf("%s",b);
printf("%d",strcmp(a,b));
return 0;
}
MT1466
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string s1,s2;
getline(cin,s1);
getline(cin,s2);
if(s1>s2) cout<<1;
else if(s1==s2) cout<<0;
else cout<<-1;
return 0;
}
MT1470
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s1[80];
int n,len;
scanf("%s",s1);
scanf("%d",&n);
len=strlen(s1);
for(int i=0;i<len;i++){
printf("%c",s1[i]);
if( (i+1)%n==0 ){
printf("\n");
}
}
return 0;
}
MT1472
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string s1;
string s2="";
getline(cin,s1);
for(int i=s1.length()-1;i>=0;i--){
s2+=s1[i];
}
cout<<s2;
return 0;
}
MT1473
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80],b[80];
int n;
cin.getline(a,80);//第一行输入字符串
scanf("%d",&n);//第二行输入非负整数n,n小于字符串长度
strcpy(b,a+n);//输出字符串左移n位后的内容。移出去的内容去掉。
printf("%s",b);
return 0;
}
MT1475
#include<bits/stdc++.h>
using namespace std;
/*
什么叫是否互为回旋字符串呢?
比如:样例1:
waterbottle => 顺时针移动3个字符串 , 就得到了erbottlewat
erbottlewat
*/
int main( )
{
//输入字符串s1,s2(均不含空格),
//判断s1是否为s2的回旋字符串,输出YES或者NO。
string s1,s2,s11,s22;
cin>>s1>>s2;
s11=s1+s1;
s22=s2+s2;
if(s11.find(s2)!=-1 && //如果等于-1则说明没找到
s22.find(s1)!=-1){
cout<<"YES";
}else{
cout<<"NO";
}
return 0;
}
MT1476
#include<bits/stdc++.h>
using namespace std;
//看1409
/*
12345
3
答案:34512
=>解析:5走了,变成51234
4走了,变成45123
3走了,变成34512
*/
int main( )
{
char a[100];
int n,m;//n个字符,旋转m次
cin.getline(a,sizeof(a));//先输入字符串
cin>>m;//再输入m
n=strlen(a);//长度
m=m%n;
for(int i=0;i<n;i++)
//旋转说白了,就是循环数组,数组后面走几个,前面再补回来
while(m){
char tmp = a[n-1];//记录最后一个数
for(int i=n-1;i>0;i--)a[i]=a[i-1];//整体后移
a[0]=tmp;//放在前面
m--;
}
for(int i=0;i<n;i++)cout<<a[i];
return 0;
}
MT1477
#include<bits/stdc++.h>
using namespace std;
//看1409
/*
12345
3
答案:45123
=>解析:1走了,变成23451
2走了,变成34512
3走了,变成45123
*/
int main( )
{
char a[100];
int n,m;//n个字符,旋转m次
cin.getline(a,sizeof(a));//先输入字符串
cin>>m;//再输入m
n=strlen(a);//长度
m=m%n;
for(int i=0;i<n;i++)
//旋转说白了,就是循环数组,数组前面走几个,后面再补回来
while(m){
char tmp = a[0];//记录第一个一个数
for(int i=0;i<n-1;i++)a[i]=a[i+1];//整体前移
a[n-1]=tmp;//放在后面
m--;
}
for(int i=0;i<n;i++)cout<<a[i];
return 0;
}
MT1478
#include<bits/stdc++.h>
using namespace std;
/*
0 1 2 3
a b c d
0 1 2 3 4 5 6
a A b A c A d
*/
int main( )
{
char s1[10000],s2[20000];
//从键盘上输入一个字符串(长度小于10000,以回车作为结束,其余地方不出现回车)
cin.getline(s1,10000);
int len=strlen(s1);
//要求输出一个新字符串,
//新的字符串是在原来字符串中, 每两个字符之间插入一个A
for(int i=0;i<len-1;i++){
s2[i*2]=s1[i];
s2[i*2+1]='A';
}
s2[len*2-2]=s1[len-1];
s2[len*2-1]='\0';
cout<<s2;
return 0;
}
MT1479
#include<bits/stdc++.h>
using namespace std;
void insert(char *s1,char *s2,int pos){
//cout<<pos<<endl;
int len1 = strlen(s1);
//cout<<len1<<endl;
int len2 = strlen(s2);
//cout<<len2<<endl;
for(int i=len1;i>pos-1;i--){//从下标len1挪到pos-1
//cout<<i<<"=="<<endl;
//将s1从下标len1开始挪,往后挪 , 每次挪len2个位置
//一直挪到下标为pos-1位置为止
s1[i+len2]=s1[i];
//cout<<s1<<"=="<<endl;
}
//将s2放入s1
for(int i=0;i<len2;i++){
s1[i+pos]=s2[i];
}
}
int main( )
{
char s1[100],s2[100];
int pos;
cin.getline(s1,100);
cin.getline(s2,100);
cin>>pos;
insert(s1,s2,pos);
cout<<s1;
return 0;
}
MT1480
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//一个空格换成2个*
string s1,s2;
getline(cin,s1);
int len = s1.size();
for(int i=0;i<len;i++){
if(s1[i]==' '){
s2.push_back('*');
s2.push_back('*');
}else{
s2.push_back(s1[i]);
}
}
cout<<s2;
return 0;
}
MT1481
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80];
cin.getline(a,80);
for(int i=0;i<strlen(a);i++){
//十条法则
if(a[i]=='A'&&a[i+1]=='#')a[i]='B',a[i+1]='b';
else if(a[i]=='B'&&a[i+1]=='b')a[i]='A',a[i+1]='#';
else if(a[i]=='C'&&a[i+1]=='#')a[i]='D',a[i+1]='b';
else if(a[i]=='D'&&a[i+1]=='b')a[i]='C',a[i+1]='#';
else if(a[i]=='D'&&a[i+1]=='#')a[i]='E',a[i+1]='b';
else if(a[i]=='E'&&a[i+1]=='b')a[i]='D',a[i+1]='#';
else if(a[i]=='F'&&a[i+1]=='#')a[i]='G',a[i+1]='b';
else if(a[i]=='G'&&a[i+1]=='b')a[i]='F',a[i+1]='#';
else if(a[i]=='G'&&a[i+1]=='#')a[i]='A',a[i+1]='b';
else if(a[i]=='A'&&a[i+1]=='b')a[i]='G',a[i+1]='#';
}
printf("%s",a);
return 0;
}
MT1482
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s[1000];
char newS[1000];
cin.getline(s,1000);
int j=0;
for(int i=0;i<strlen(s);i++){
if(s[i]!=' ')newS[j++]=s[i];
}
newS[j]='\0';
cout<<newS<<endl;
return 0;
}
MT1484
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s[80];
char newS[80];
cin.getline(s,80);
int j=0;
for(int i=0;i<strlen(s);i++){
if(s[i]!=' ' && s[i]!='!' && s[i]!='.' && s[i]!=','){
newS[j++]=s[i];
}
}
newS[j]='\0';
cout<<newS<<endl;
return 0;
}
MT1485
#include<bits/stdc++.h>
using namespace std;
bool findChar(char c,char a[]){
for(int j=0;j<strlen(a);j++)
if(c==a[j])return true;
return false;
}
int main( )
{
char s1[100],s2[100],newS[100];
cin.getline(s1,100);
cin.getline(s2,100);
int k=0;
for(int i=0;i<strlen(s1);i++){
if(findChar(s1[i],s2)==false){
newS[k++]=s1[i];
}
}
newS[k]='\0';
cout<<newS;
return 0;
}
MT1487
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s[80];
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++){
if( (s[i]>='A'&&s[i]<='Z') || (s[i]>='a'&&s[i]<='z') ){
printf("YES\n");
return 0;
}
}
printf("NO\n");
return 0;
}
MT1488
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s1[80];
scanf("%s",s1);
int len = strlen(s1);
char max = s1[0],min=s1[0];
for(int i=0;i<len;i++){
if(s1[i]>max)max=s1[i];
else if(s1[i]<min)min=s1[i];
}
printf("%c %c",max,min);
return 0;
}
MT1489
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s[100000];
cin.getline(s,100000);
for(int i=0;i<strlen(s);i++){
if(s[i]>='a' && s[i]<='z')s[i] -= 32;
else if(s[i]>='A' && s[i]<='Z')s[i] += 32;
}
cout<<s<<endl;
return 0;
}
MT1490
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s[100000];
cin.getline(s,100000);
int num_a=0,num_A=0;
for(int i=0;i<strlen(s);i++){
if(s[i]>='a' && s[i]<='z')num_a++;
if(s[i]>='A' && s[i]<='Z')num_A++;
}
for(int i=0;i<strlen(s);i++){
//注:小写的ascll码值大 , 大写的ascll码值小
//大写字符多,则全部转换为大写字符。
if(num_A>num_a && s[i]>='a' && s[i]<='z')s[i] -= 32;
//如果其中小写字符多,则将其全部转换为小写字符
if(num_a>num_A && s[i]>='A' && s[i]<='Z')s[i] += 32;
}
cout<<s<<endl;
return 0;
}
MT1491
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80];
cin.getline(a,80);
int len = strlen(a);
int na=0,nb=0,nc=0,nd=0;
for(int i=0;i<len;i++){
if(a[i]=='!') na++;
if(a[i]==',') nb++;
if(a[i]=='.') nc++;
if(a[i]=='?') nd++;
}
printf("%d %d %d %d",na,nb,nc,nd);
return 0;
}
MT1492
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s[100000];
char check[11]="aeiouAEIOU";//aeiou
scanf("%s",s);
int len=strlen(s);
int cnt=0;
for(int i=0;i<len;i++){
for(int j=0;j<10;j++){
if(check[j]==s[i]){
cnt++;
}
}
}
printf("%d",cnt);
return 0;
}
MT1493
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string s;
getline(cin,s);//输入一行
//cout<<"输入结束"<<endl;
int len=s.size();//长度
//默认是第一个单词
//出现次数最多的首字母的下标,默认是(下标为第一个字符的ascll码值)的字符
//出现次数最多的首字母的次数,默认为1
int ans[100]={0};
int max=s[0]-'0';
//cout<<max<<endl;
ans[max]=1;
for(int i=1;i<len;i++){//我们默认第一个单词的开头就是出现次数最多的那个,所以,我们直接跳过首字母,从第二个字符开始遍历
if(s[i]==' ')continue;
//不是空格
if(s[i-1]==' '){//是第一个字符
ans[s[i]-'0']++;
}
if(ans[s[i]-'0']>ans[max])max=s[i]-'0';
}
printf("%c",max+'0');
return 0;
}
MT1496
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80],b[26]={0},c[26]={0};//b存大写字母(ascll小) c存小写字母(ascll大)
//所以先输出b里面的 , 再输出c里面的
cin.getline(a,80);
int len=strlen(a);
//查找字符串中的重复字符(指26个英语字母,区分大小写)
//从小到大(ASCII值)排序他们并输出
for(int i=0;i<len;i++){
if(a[i]>='A'&&a[i]<='Z'){
b[a[i]-'A']++;
}else{
c[a[i]-'a']++;
}
}
for(int i=0;i<26;i++){
if(b[i]>1)printf("%c ",'A'+i);
}
for(int i=0;i<26;i++){
if(c[i]>1)printf("%c ",'a'+i);
}
return 0;
}
MT1497
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80],b[26]={0};
cin.getline(a,80);
int len = strlen(a);
for(int i=0;i<len;i++){
if(a[i]>='a' && a[i]<='z'){
b[a[i]-'a']++;
}
}
for(int i=0;i<len;i++){
if(b[a[i]-'a']==1){
printf("%d",i);
return 0;
}
}
printf("%d",-1);
return 0;
}
MT1498
#include<bits/stdc++.h>
using namespace std;
int main( )
{
long n,i=0;
char a[10];
scanf("%ld",&n);
if(n>0)printf("%c",'+');
else if(n<0)n=-n,printf("%c",'-');
while(n){
a[i++]=n%10+'0';//转换为字符串
n/=10;
}
for(int j=i-1;j>=0;j--)printf("%c",a[j]);
return 0;
}
MT1499
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[32];
unsigned int num;
scanf("%u",&num);
int cnt=0;
while(num){
a[cnt++]=num%2;
num/=2;
}
for(int i=cnt-1;i>=0;i--){
printf("%d",a[i]);
}
if(cnt==0){
printf("0");
}
return 0;
}
MT1500
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[8];
int num;
scanf("%d",&num);
for(int i=0;i<8;i++){
a[i]=num&1;//位与,有0则0
//直接用位与就行了,就不用管正负了,负数系统会自动转化为其二进制下的补码,再和1位与的
num>>=1;
}
for(int i=7;i>=0;i--){
printf("%d",a[i]);
}
return 0;
}
MT1501
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[10];
cin>>a;
int result=0,len=strlen(a);
if(len==1){
cout<<"FAIL";
return 0;
}
for(int i=1;i<len;i++){
result = result*10+a[i]-'0';
}
if(a[0]=='-'){
result=-result;
}
cout<<result;
return 0;
}
MT1502
#include <bits/stdc++.h>
using namespace std;
int main()
{
//非法则输出0;
//什么情况非法? 小数点超过一个非法
//特别注意:不以正负号开头,是合法的
char a[80];
cin.getline(a, 80);
double ret = 0;
int flag = 1; //判断正负号,如果没有符号则默认为正数
if (a[0] == '+')
flag = 1;
if (a[0] == '-')
flag = -1;
bool flag2 = false; //判断是否有小数
int cnt = 1; //小数指数
int j = (a[0] == '+' || a[0] == '-' ? 1 : 0);
// cout<<j<<endl;
for (int i = j; i < strlen(a); i++)
{
/*if(i==0){//如果i从0开始,则没有正负号,非法
ret=0;
break;
}*/
if ((a[i] > '9' || a[i] < '0') && a[i] != '.')
{
// cout<<1<<endl;
ret = 0; //不是1~9的字符,也不是.号非法
break;
}
if (a[i] == '.' && flag2 == true)
{
// cout<<2<<endl;
ret = 0; //已经有小数了,结果又来一个小数点,非法
break;
}
if (a[i] == '.' && flag2 == false)
{
// cout<<3<<endl;
flag2 = true;
continue;
}
if (flag2 == false) //不是小数的部分
{
// cout<<4<<endl;
ret = ret * 10 + a[i] - '0';
}
else //是小数的部分
{
// cout<<5<<endl;
ret = ret * 10 + a[i] - '0';
cnt *= 10; //用它来除以,来还原小数
}
}
// cout<<flag<<endl;
if (ret == 0)
printf("0");
else
{
printf("%lf\n", (ret / cnt) * flag);
// cout<<ret<<" "<<cnt<<" "<<flag<<endl;
}
return 0;
}
MT1505
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//A=>1
//AB=>3
//ABC=>6
//ABCD=>10
/*
推导:
n个字符
子串有n个字符:数量1
子串有n-1个字符:数量2
子串有n-2个字符:数量3
子串有n-3个字符:数量4
...
子串有1个字符:数量n
(1+..+n)=(1+n)*n/2
*/
string s;
cin>>s;
int len=s.length();
cout<<(len+1)*len/2;
return 0;
}
MT1508
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[100000];
char zi[]="zichuan";
cin.getline(a,sizeof(a));
/*
定义:strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。
如果是,则该函数返回str2在str1中首次出现的地址;
否则,返回NULL。
*/
char *begin=strstr(a,zi);
if(begin == NULL)cout<<"NULL";
else{
//因为begin存放的是地址,我们要的是下标
//两个地址相减就可以得到下标
int i=begin-a;
cout<<i<<" "<<i+ (strlen(zi)-1);
}
return 0;
}
MT1509
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[100];
char b[100];
cin.getline(a,100);
cin.getline(b,100);
char *begin=strstr(a,b);
if(begin==NULL)cout<<-1;
else cout<<begin-a;
return 0;
}
MT1513
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string s[100000];
int n;
scanf("%d\n",&n);
for(int i=0;i<n;i++){
getline(cin,s[i]);
}
sort(s,s+n);
for(int i=0;i<n;i++)
cout<<s[i]<<endl;
return 0;
}
MT1514
#include<bits/stdc++.h>
using namespace std;
bool cmp(string a,string b){
if(a.length()==b.length())return a<b;//长度相同的编号按照字典序排列。
else if(a.length()<b.length())return true;//长度小的放在前面
else return false;//长度大的放在后面
}
int main( )
{
string s[100000];
int n;
scanf("%d\n",&n);
for(int i=0;i<n;i++){
getline(cin,s[i]);
}
sort(s,s+n,cmp);
for(int i=0;i<n;i++){
cout<<s[i]<<endl;
}
return 0;
}
MT1515
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a,*p;
scanf("%d",&a);
p=&a;
printf("%d",*p);
return 0;
}
MT1517
#include<bits/stdc++.h>
using namespace std;
void swap(int *pa,int *pb){
int temp;
temp=*pa;
*pa=*pb;
*pb=temp;
}
int main( )
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(a>b){
swap(&a,&b);
}
if(a>c){
swap(&a,&c);
}
if(b>c){
swap(&b,&c);
}
printf("%d %d %d",a,b,c);
return 0;
}
MT1518
#include<bits/stdc++.h>
using namespace std;
void swap(int *pa,int *pb){
int temp;
temp=*pa;
*pa=*pb;
*pb=temp;
}
int main( )
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(a>b){
swap(&a,&b);
}
if(a>c){
swap(&a,&c);
}
if(b>c){
swap(&b,&c);
}
printf("%d %d %d",a,b,c);
return 0;
}
MT1519
#include<bits/stdc++.h>
using namespace std;
int main( )
{
double a,*p;
scanf("%lf",&a);
p=&a;
printf("%lf",*p);
return 0;
}
MT1520
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a,*p;
scanf("%c",&a);
p=&a;
printf("%c",*p);
return 0;
}
MT1521
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80],*p;
int n;
p=a;
cin.getline(a,80);
scanf("%d",&n);
printf("%c",p[n-1]);
return 0;
}
MT1522
#include<bits/stdc++.h>
using namespace std;
void f(char *s){
while(*s){
if(*s>='a'&&*s<='z')*s-=3;
if(*s>='A'&&*s<='Z')*s+=3;
s++;
}
}
int main( )
{
char s[100];
cin.getline(s,100);
f(s);
printf("%s",s);
return 0;
}
MT1523
#include<bits/stdc++.h>
using namespace std;
void mycpy(char *s1,char *s2){
char *p=s1;
while(*s2 != '\0'){
*(p++)=*(s2++);
}
*p='\0';
//return s1;
}
int main( )
{
char str1[10000],str2[10000];
cin.getline(str1,10000);
cin.getline(str2,10000);
mycpy(str1,str2);
cout<<str1;
return 0;
}
MT1524
#include<bits/stdc++.h>
using namespace std;
void mystrcat(char *s1,char *s2){
char *p=s1;
while(*p != '\0')p++;//遍历到s1的最后面
while(*s2!='\0'){
*(p++) = *(s2++);
}
*p = '\0';
}
int main( )
{
char s1[10000]="",s2[5000]="";//两个字符串长度均小于5000
cin.getline(s1,10000);
cin.getline(s2,5000);
mystrcat(s1,s2);
cout<<s1;
return 0;
}
MT1526
#include <bits/stdc++.h>
using namespace std;
// 1525补充:
// isalpha函数用于判断字符是否为字母(a-z和A-Z)
//*(p++):对p指针自加之前解引 ,然后指针p再++
int main()
{
char s1[10000];
cin.getline(s1, 10000);
char *p = s1;
for (int i = 0; i < strlen(s1); i++)
{
if (*(p + i) == ' ')
continue;
int num = 1;
for (int j = i + 1; j < strlen(s1); j++)
{
if (*(p + i) == *(p + j))
{
num++;
*(p + j) = ' ';
}
}
if (num >= 2)
{
printf("%c %d\n", *(p + i), num);
}
}
return 0;
}
MT1527
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char str[10000]="";
char *p1,*p2;
bool state = true;
cin.getline(str,10000);
p1=str;
p2=str+strlen(str)-1;
while(p1<p2){
if(*p1 != *p2){
state = false;
break;
}
p1++;
p2--;
}
if(state){
cout<<"YES";
}else{
cout<<"NO";
}
return 0;
}
MT1530
#include<bits/stdc++.h>
using namespace std;
int maxLen(char *s){
int len = strlen(s);
int ret = 1;
int cnt = 1;
for(int i=1;i<len;i++){
if(*(s+i) == *(s+i-1))cnt++;
else cnt = 1;
ret = max(ret,cnt);
}
return ret;
}
int main( )
{
char s[100];
cin.getline(s,100);
printf("%d",maxLen(s));
return 0;
}
MT1532
#include<bits/stdc++.h>
using namespace std;
int match(char *s1,char *s2){
int len1=strlen(s1),len2=strlen(s2);
for(int i=0;i<len1;i++){//遍历s1全部
for(int j=0;j<len2;j++){//遍历s2全部
if( (s2[j]!='?') && (s1[i+j]!=s2[j]) )break;//当前遍历到s2不是问号,且有一个不匹配的时候,就退出循环
if(j==len2-1)return i;//s1与s2匹配完了
}
}
return -1;
}
int main( )
{
char s1[100],s2[100];
cin.getline(s1,100);
cin.getline(s2,100);
printf("%d",match(s1,s2));
return 0;
}
MT1534
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[3]={1,2,3};
int *ptr;
ptr=a;
for(int i=0;i<3;i++){
printf("a[%d]=%d\n",i,*ptr);
ptr++;
}
return 0;
}
MT1535
#include<bits/stdc++.h>
using namespace std;
int main( )
{
//0 1 2 3 4 5 6 7 8 9
int a[10];
for(int i=0;i<10;i++){
cin>>a[i];
}
for(int *i=a;i!=&a[10];i++){
cout<<*i<<" ";
if(i!=&a[9])cout<<1<<" ";
//其实就是输出一个原数组的数,后面就跟一个1,除了最后一个元素
}
return 0;
}
MT1536
#include<bits/stdc++.h>
using namespace std;
void reverse(int *a,int n){//n是元素个数
for(int i=0;i<n/2;i++){
/*
5
=>
0 1 2 3 4
n/2=2 i<2刚刚好
*/
int temp=a[i];
a[i]=a[n-i-1];
a[n-i-1]=temp;
}
}
int main( )
{
int a[10];
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",a+i);
reverse(a,n);
for(int i=0;i<n;i++)printf("%d ",a[i]);
return 0;
}
MT1537
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[10];
for(int i=0;i<10;i++)cin>>a[i];
for(int *i =&a[9];i!=a-1;i--)cout<<*i<<" ";
//a-1就是a[-1]的地址
//cout<<endl<<*(a-1)<<endl;
//cout<<a[-1]<<endl;
return 0;
}
MT1540
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[100],n;
int *p =a;
cin>>n;
int even =0,odd =0;
for(int i=0;i<n;i++){
cin>>*(p+i);
}
for(int i=0;i<n;i++){
if(i%2==1)odd += *(p+i);
else even += *(p+i);
}
if(odd==even)cout<<"YES";
else cout<<"NO";
return 0;
}
MT1541
#include<bits/stdc++.h>
using namespace std;
void sum(int *a,int n,int *odd,int *even){
for(int i=0;i<n;i++){
if(i%2==1){
*odd += a[i];
}else{
*even +=a[i];
}
}
}
int main( )
{
int a[10];
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",a+i);
int odd=0,even=0;
sum(a,n,&odd,&even);
printf("%d",odd-even);
return 0;
}
MT1542
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int chepai[10],fine[10];
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)cin>>chepai[i];
for(int i=0;i<n;i++)cin>>fine[i];
int finesum=0,*p1=chepai,*p2=fine;
if(m%2==1){
for(int i=0;i<n;i++){
if(*p1%2==0){
finesum+=*p2;
}
p1++;
p2++;
}
}else{
for(int i=0;i<n;i++){
if(*p1 %2 ==1){
finesum+=*p2;
}
p1++;
p2++;
}
}
cout<<finesum;
return 0;
}
MT1543
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[3][3];
int (*p)[3]=a;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
scanf("%d",&p[i][j]);
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%d ",p[i][j]);
}
printf("\n");
}
return 0;
}
MT1544
#include<bits/stdc++.h>
using namespace std;
int main( )
{
/*
注:
对于一维数组:a[4]=>数组元素的首地址是a
对于二维数组:a[4][4]=>数组元素的首地址是a[0]
*/
int a[4][4],*p;
//a[x][y]代表其存储的元素,a[n]代表地址,
//不论是多少维的数组,在内存中本质上都是线性存储的,即a[0]+16
for(p=a[0];p<a[0]+16;p++)scanf("%d",p);
for(p=a[0];p<a[0]+16;p++){
printf("%2d ",*p);
if((p-a[0])%4==3){
printf("\n");
}
}
return 0;
}
MT1545
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a[10][10]={0};
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
int ans=0;
for(int i=1;i<=n;i++){
int now=1;
for(int *j=&a[i][1];j!=&a[i][m+1];j++){
now=now * (*j);
}
ans+=now;
}
cout<<ans;
return 0;
}
MT1547
#include<bits/stdc++.h>
using namespace std;
void matrixPlus(int (*a)[3],int (*b)[3]){
int c[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
*(*(c+i)+j) = *(*(a+i)+j) + *(*(b+i)+j);
printf("%3d ",*(*(c+i)+j));
}
printf("\n");
}
}
int main( )
{
int a[3][3],b[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
scanf("%d",&b[i][j]);
matrixPlus(a,b);
return 0;
}
/*
1 9 8
12 10 11
8 5 7
*/
MT1548
#include<bits/stdc++.h>
using namespace std;
void matrixPlus(int (*a)[3],int (*b)[3]){
int c[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
*(*(c+i)+j) = *(*(a+i)+j) - *(*(b+i)+j);
printf("%3d ",*(*(c+i)+j));
}
printf("\n");
}
}
int main( )
{
int a[3][3],b[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
scanf("%d",&b[i][j]);
matrixPlus(a,b);
return 0;
}
/*
1 9 8
12 10 11
8 5 7
*/
MT1549
#include<bits/stdc++.h>
using namespace std;
void left_down(int (*n)[3]){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(j<=i){
cout<<*(*(n+i)+j)<<" ";
}
}
cout<<endl;
}
}
int main( )
{
int a[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>a[i][j];
}
}
left_down(a);
return 0;
}
MT1550
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string month[12]=
{"january","february","march",
"april","may","june",
"july","august","september",
"october","november","december"};
string *ptr = month;
int n;
cin>>n;
cout<<*(ptr+n-1);
return 0;
}
MT1552
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s[10][100];
for(int i=0;i<10;i++){
cin.getline(s[i],100);
}
char *sorted[10];
for(int i=0;i<10;i++){
sorted[i]=s[i];
}
for(int i=0;i<10;i++){
int pos=i;
for(int j=i;j<10;j++){
if(strcmp(sorted[pos],sorted[j])>0){
pos = j;
}
}
char *t = sorted[pos];
sorted[pos] = sorted[i];
sorted[i]=t;
}
for(int i=0;i<10;i++)
printf("%s\n",sorted[i]);
return 0;
}
MT1553
#include<bits/stdc++.h>
using namespace std;
void visitint(void *p){
printf("%d ",*( (int *)p ) );
}
void visitchar(void *p){
printf("%c ",*((char *)p));
}
void visitdouble(void *p){
printf("%lf ",*((double *)p));
}
void Traverse(void *p,int n,void(*visit)(void *ep)){
for(int i=0;i<n;i++){
visit((char *)p+i*(visit==visitchar ?sizeof(char):
(visit==visitint?sizeof(int):sizeof(double))));
}
}
int main( )
{
int a[]={1,2,3,4,5,6,7,8,9};
Traverse(a,9,visitint);
printf("\n");
double b[]={1.0,2.0,3.0,4.0,5.0,6.0,
7.0,8.0,9.0};
Traverse(b,9,visitdouble);
printf("\n");
char s[]="abcdefghi";
Traverse(s,9,visitchar);
return 0;
}
MT1555
#include<bits/stdc++.h>
using namespace std;
int getPointer(int a[],int n,int key){
for(int i=0;i<n;i++){
if(key == a[i]){
return i;
}
}
return -1;
}
int main( )
{
int a[100],n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
int key;
cin>>key;
printf("%d",getPointer(a,n,key));
return 0;
}
MT1556
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,m;
cin>>n>>m;//n个人,m被杀
int a[10000]={0};//座位
int cnt=0;//出局的人数
int count=0;//计数器
int i=0;//每人的编号
while(1){
i++;//编号从1开始
if(i>n)i=1;//循环
if(a[i]==0){
//等于0的代表没出局
count++;//计数+1
}
if(count==m){//计数记够了
a[i]=1;//出局
cnt++;//出局人数+1
count=0;//重新计数
if(cnt==n){
//如果是最后一个出局的
cout<<i<<" ";
return 0;
}
}
}
return 0;
}
MT1557
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,m;
cin>>n>>m;//n个人,m被杀
int a[10000]={0};//座位
int cnt=0;//出局的人数
int count=0;//计数器
int i=0;//每人的编号
while(1){
i++;//编号从1开始
if(i>n)i=1;//循环
if(a[i]==0){
//等于0的代表没出局
count++;//计数+1
}
if(count==m){//计数记够了
a[i]=1;//出局
cout<<i<<" ";
cnt++;//出局人数+1
count=0;//重新计数
if(cnt==n){
//如果是最后一个出局的
//cout<<i<<" ";
return 0;
}
}
}
return 0;
}
MT1558
#include<bits/stdc++.h>
using namespace std;
struct Baby{
char name[100];
int age;
char sex;
};
int main( )
{
Baby aiefa;
scanf("%s %d %c",
&aiefa.name,
&aiefa.age,
&aiefa.sex);
printf("%s %d %c",
aiefa.name,
aiefa.age,
aiefa.sex);
return 0;
}
MT1559
#include<bits/stdc++.h>
using namespace std;
struct Baby{
char name[100];
int age;
char sex;
};
int main( )
{
Baby a[5];
for(int i=0;i<5;i++)
scanf("%s %d %c",
&a[i].name,
&a[i].age,
&a[i].sex);
for(int i=0;i<5;i++)
printf("%s %d %c ",
a[i].name,
a[i].age,
a[i].sex);
return 0;
}
MT1562
#include<bits/stdc++.h>
using namespace std;
struct Soldier{
char name[100];
int power;
};
int main( )
{
Soldier a[4];
for(int i=0;i<4;i++){
scanf("%s %d",&a[i].name,&a[i].power);
}
int max =a[0].power;
int index = 0;
for(int i=1;i<4;i++){
if(max<a[i].power){
max = a[i].power;
index = i;
}
}
printf("%s %d",a[index].name,a[index].power);
return 0;
}
MT1563
#include<bits/stdc++.h>
using namespace std;
struct Soldier{
char name[100];
int power;
};
int main( )
{
Soldier a[4];
for(int i=0;i<4;i++){
scanf("%s %d",&a[i].name,&a[i].power);
}
int min =a[0].power;
int index = 0;
for(int i=1;i<4;i++){
if(min>a[i].power){
min = a[i].power;
index = i;
}
}
printf("%s %d",a[index].name,a[index].power);
return 0;
}
MT1564
#include<bits/stdc++.h>
using namespace std;
struct Book{
char subject[10];
char title[20];
int page;
};
int main( )
{
struct Book a[3];
for(int i=0;i<3;i++)
scanf("%s%s%d",&a[i].subject,&a[i].title,&a[i].page);
int min=a[0].page;
int index=0;
for(int i=1;i<3;i++){
if(min>a[i].page){
min = a[i].page;
index = i;
}
}
printf("%s %s %d",a[index].subject,a[index].title,a[index].page);
return 0;
}
MT1565
#include<bits/stdc++.h>
using namespace std;
struct Human{
char name[200];
int age;
};
int main( )
{
Human a[5];
for(int i=0;i<5;i++)
scanf("%s%d",&a[i].name,&a[i].age);
int max =a[0].age;
int index = 0;
for(int i=1;i<5;i++){
if(max<a[i].age){
max=a[i].age;
index=i;
}
}
printf("%s %d",a[index].name,a[index].age);
return 0;
}
MT1566
#include<bits/stdc++.h>
using namespace std;
struct Human{
char name[200];
int age;
};
int main( )
{
Human a[5];
for(int i=0;i<5;i++)
scanf("%s%d",&a[i].name,&a[i].age);
int min =a[0].age;
int index = 0;
for(int i=1;i<5;i++){
if(min>a[i].age){
min=a[i].age;
index=i;
}
}
printf("%s %d",a[index].name,a[index].age);
return 0;
}
MT1567
#include<bits/stdc++.h>
using namespace std;
struct Staff{
int number;
char name[20];
int wage;
};
int main( )
{
Staff a[3];
for(int i=0;i<3;i++){
scanf("%d%s%d",&a[i].number,&a[i].name,&a[i].wage);
}
int max=a[0].wage;
int index=0;
for(int i=1;i<3;i++){
if(max<a[i].wage){
max=a[i].wage;
index=i;
}
}
printf("%d %s %d",a[index].number,a[index].name,a[index].wage);
return 0;
}
MT1568
#include<bits/stdc++.h>
using namespace std;
struct Student{
int number;
char name[10];
double score[3];
double sum;
};
int main( )
{
//学号整型,姓名字符型,成绩实型
Student stu,max_stu;
for(int i=0;i<3;i++){
scanf("%d %s %lf %lf %lf",
&(stu.number),&(stu.name),&(stu.score[0]),&(stu.score[1]),&(stu.score[2]));
stu.sum=stu.score[0]+stu.score[1]+stu.score[2];
if(i==0)max_stu=stu;
if(max_stu.sum<stu.sum)max_stu=stu;
}
//注意:这个题输出必须用%g,
//%g是啥意思?=>格式化输出
//就是,如果用%lf输出12.3,则输出的是12.300000
//如果用%g,则输出12.3,会抹去不必要的0
//printf("%lf %g\n",12.3,12.3);
//c++中的cout就是自带%g的
//cout<<max_stu.score[0]<<" "<<max_stu.score[1]<<" "<<max_stu.score[2]<<endl;
printf("%d %s %g %g %g",
max_stu.number,
max_stu.name,
max_stu.score[0],
max_stu.score[1],
max_stu.score[2]);
return 0;
}
MT1572
#include<bits/stdc++.h>
using namespace std;
struct Cost
{
int season;
int net;
int phone;
};
int main( )
{
Cost c;
int total=0;
for(int i=0;i<4;i++){
scanf("%d %d %d",&(c.season),&(c.net),&(c.phone));
total += c.phone+c.net;
}
cout<<total;
return 0;
}
MT1573
#include<bits/stdc++.h>
using namespace std;
struct Food{
char name[30];
float weight;
float price;
};
int main( )
{
Food f;
float total =0.0;
for(int i=0;i<3;i++){
scanf("%s %f %f",f.name,&(f.weight),&(f.price));
total+=f.weight*f.price;
}
printf("%f",total);
return 0;
}
MT1574
#include<bits/stdc++.h>
using namespace std;
struct Flower{
char name[30];
int num;
int price;
};
int main( )
{
Flower f;
int total =0;
for(int i=0;i<4;i++){
scanf("%s %d %d",f.name,&(f.num),&(f.price));
total += f.num*f.price;
}
printf("%d",total);
return 0;
}
MT1575
#include<bits/stdc++.h>
using namespace std;
struct Food{
char name[100];
int calorie;
};
int main( )
{
Food f;
int total = 0;
for(int i=0;i<3;i++){
scanf("%s %d",f.name,&(f.calorie));
total+=f.calorie;
}
double round =total/50.0;
printf("%lf",round);
return 0;
}
MT1577
#include<bits/stdc++.h>
using namespace std;
struct Money{
int season;
int value;
};
int main( )
{
Money m;
int surplus=0;
for(int i=0;i<4;i++){
scanf("%d %d",&(m.season),&(m.value));
int temp=m.value+surplus-9000;
if(temp>0)surplus=temp;
else surplus=0;
}
printf("%d",surplus);
return 0;
}
MT1579
#include<bits/stdc++.h>
//看着这个题麻烦,其实并不麻烦,很多代码都是可以复制粘贴,然后改改就OK了
using namespace std;
struct Fraction{
int fenzi,fenmu;
Fraction operator+(const Fraction &a){
Fraction rtn;
rtn.fenzi=this->fenzi * a.fenmu + this->fenmu * a.fenzi;
rtn.fenmu=this->fenmu * a.fenmu;
return rtn;
}
Fraction operator-(const Fraction &a){
Fraction rtn;
rtn.fenzi=this->fenzi * a.fenmu - this->fenmu * a.fenzi;
rtn.fenmu=this->fenmu * a.fenmu;
return rtn;
}
Fraction operator*(const Fraction &a){
Fraction rtn;
rtn.fenzi=this->fenzi * a.fenzi;
rtn.fenmu=this->fenmu * a.fenmu;
return rtn;
}
Fraction operator/(const Fraction &a){
Fraction rtn;
rtn.fenzi=this->fenzi * a.fenmu;
rtn.fenmu=this->fenmu * a.fenzi;
return rtn;
}
};
int gcd(int a,int b){
return b ? gcd( b , a%b) :a;
}
void printFrac(Fraction &a){
//通分
int yue=gcd(a.fenzi,a.fenmu);
a.fenzi/=yue;
a.fenmu/=yue;
if(a.fenmu==1)printf("%d\n",a.fenzi);
else printf("%d/%d\n",a.fenzi,a.fenmu);
}
int main( )
{
Fraction a,b,c;
scanf("%d %d %d %d",
&a.fenzi,&a.fenmu,&b.fenzi,&b.fenmu);
c=a+b;
printFrac(c);
c=a-b;
if(c.fenzi<0){
printf("-");
c.fenzi=-c.fenzi;
}
printFrac(c);
c=a*b;
printFrac(c);
c=a/b;
printFrac(c);
return 0;
}
MT1580
#include<bits/stdc++.h>
using namespace std;
struct Student{
char name[20],number[20],getder[2],classno[20];
int ret;
};
int main( )
{
Student stu;
int n,count=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s%s%s%s%d",
stu.name,
stu.number,
stu.getder,
stu.classno,
&stu.ret);
if(stu.ret<60)count++;
}
printf("%d",count);
return 0;
}
MT1581
#include<bits/stdc++.h>
using namespace std;
struct Person{
char name[20];
char number[20];
char sex;
char zhi_cheng[20];
int money;
};
int main( )
{
int n;
cin>>n;
Person p;
int count=0;
for(int i=0;i<n;i++){
scanf("%s %s %c %s %d",
p.name,
p.number,
&p.sex,
p.zhi_cheng,
&p.money);
if(strcmp(p.zhi_cheng,"jiangshi")==0){//strcmp相等返回0
count++;
//cout<<count<<endl;
}
}
cout<<count;
return 0;
}
MT1582
#include<bits/stdc++.h>
using namespace std;
struct Date{
int month,day;
};
int DateToDay(Date date){
int count=0;
//假设该年是非闰年
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=1;i<=date.month;i++){
count += m[i-1];
}
count += date.day;
return count;
}
int main( )
{
Date date;
scanf("%d %d",&(date.month),&(date.day));
int count=DateToDay(date);
/*
日期 1 2 3 4 5 6 7 8 9
星期 5 6 7 1 2 3 4 5 6
=>对7取余,得1是星期5,的2是星期6,...得0是星期4
*/
int d[]={4,5,6,7,1,2,3};
printf("%d",d[count%7]);
return 0;
}
MT1584
#include<bits/stdc++.h>
using namespace std;
struct Date{
int month,day;
};
int DateToDay(Date date){
int count=0;
//假设该年是非闰年
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=1;i<=date.month;i++){
count += m[i-1];
}
count += date.day;
return count;
}
int main( )
{
Date date1,date2;
scanf("%d %d",&(date1.month),&(date1.day));
scanf("%d %d",&(date2.month),&(date2.day));
printf("%d",DateToDay(date2)-DateToDay(date1));
return 0;
}
MT1585
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
double distance(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+
(a.y-b.y)*(a.y-b.y));
}
int main( )
{
Point a,b;
cin>>a.x>>a.y>>b.x>>b.y;
printf("%.2lf",distance(a,b));
return 0;
}
MT1586
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Rect{
Point lt,rb;
};
int main( )
{
Rect a;
int s1,s2;
scanf("%d%d%d%d",
&a.lt.x,
&a.lt.y,
&a.rb.x,
&a.rb.y);
s1=abs(a.lt.x-a.rb.x);
s2=abs(a.lt.y-a.rb.y);
printf("%d",s1*s2);
return 0;
}
MT1587
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Circle{
Point c;
double r;
};
int main( )
{
Circle a;
scanf("%d%d%lf",&a.c.x,
&a.c.y,
&a.r);
printf("%.2lf",3.1415926 * a.r * a.r);
return 0;
}
MT1588
#include<bits/stdc++.h>
using namespace std;
struct Cube{
int length;
int width;
int height;
};
int main( )
{
Cube cube;
int n;
cin>>n;
int max_id=0;
int max_area=0;
for(int i=0;i<n;i++){
scanf("%d %d %d",&cube.length,
&cube.width,
&cube.height);
int tmp=cube.length*cube.width+
cube.length*cube.length+
cube.width*cube.height;
tmp=tmp*2;
if(tmp>max_area){
max_area=tmp;
max_id=i+1;
}
}
cout<<max_id;
return 0;
}
MT1589
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Circle{
Point c;
double r;
};
int main( )
{
Circle circle;
scanf("%d %d %lf",
&(circle.c.x),
&(circle.c.y),
&(circle.r));
Point point;
scanf("%d %d",&(point.x),&(point.y));
int distance=(point.x-circle.c.x)*(point.x-circle.c.x)
+
(point.y-circle.c.y)*(point.y-circle.c.y);
if(distance==circle.r*circle.r)cout<<"YES";
else cout<<"NO";
return 0;
}
MT1590
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Rect{
Point lt,rb;
};
int main( )
{
Rect rect;
Point point;
scanf("%d %d",&(rect.lt.x),&(rect.lt.y));
scanf("%d %d",&(rect.rb.x),&(rect.rb.y));
scanf("%d %d",&(point.x),&(point.y));
if( (point.x==rect.lt.x || point.x==rect.rb.x) &&
(point.y<=rect.lt.y && point.y>=rect.rb.y)){//在竖着的两条杠上
cout<<"YES";
return 0;
}
if( (point.y==rect.lt.y || point.y==rect.rb.y) &&
(point.x>=rect.lt.x && point.x<=rect.rb.x)){//在横着的两条杠上
cout<<"YES";
return 0;
}
cout<<"NO";
return 0;
}
MT1591
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Circle{
Point c;
double r;
};
int main( )
{
Circle circle;
Point point;
scanf("%d %d %lf",
&(circle.c.x),
&(circle.c.y),
&(circle.r));
scanf("%d %d",&point.x,&point.y);
int distance=(point.x-circle.c.x)*(point.x-circle.c.x)+
(point.y-circle.c.y)*(point.y-circle.c.y);
if(distance < (circle.r*circle.r))cout<<"YES";
else cout<<"NO";
return 0;
}
MT1592
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Rect{
Point lt,rb;
};
int main( )
{
Rect rect;
Point point;
scanf("%d %d",&(rect.lt.x),&(rect.lt.y));
scanf("%d %d",&(rect.rb.x),&(rect.rb.y));
scanf("%d %d",&(point.x),&(point.y));
if((point.x>rect.lt.x && point.x<rect.rb.x)
&&
(point.y>rect.rb.y && point.y<rect.lt.y)){
cout<<"YES";
}else{
cout<<"NO";
}
return 0;
}
MT1593
#include<bits/stdc++.h>
/*
需要知道:
已知三个点,求三角形的面积
S=1/2 * |x1(y2-y3)+x2(y3-y1)+x3(y1-y2)|
S=1/2 * |(x1-x3)(y2-y3)-(x2-x3)(y1-y3)|//这个好记13 23 , 23 13
*/
using namespace std;
struct Point{
int x,y;
};
struct Line{//线
Point s,e;//线的两端
};
int main( )
{
Point p;
Line line;
double s,d,h;
scanf("%d %d",&line.s.x,&line.s.y);
scanf("%d %d",&line.e.x,&line.e.y);
scanf("%d %d",&p.x,&p.y);
s=fabs(
line.s.x*(line.e.y-p.y)+
line.e.x*(p.y-line.s.y)+
p.x*(line.s.y-line.e.y)
)/2;
d=sqrt(
(line.e.x-line.s.x)*(line.e.x-line.s.x)+
(line.e.y-line.s.y)*(line.e.y-line.s.y)
);
h=s*2/d;
printf("%.2lf\n",h);
return 0;
}
MT1594
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Circle{
Point c;
double r;
};
int main( )
{
Circle circle;
scanf("%d %d %lf",&(circle.c.x),
&(circle.c.y),&(circle.r));
Point point;
scanf("%d %d",&point.x,&point.y);
double distance = sqrt(
(point.x-circle.c.x)*(point.x-circle.c.x)
+
(point.y-circle.c.y)*(point.y-circle.c.y)
);
if(distance <= circle.r)cout<<"0";
else printf("%.2lf",distance-circle.r);
return 0;
}
MT1595
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Line{
Point s,e;
};
struct Rect{
Point lt,rb;
};
double pointToPoint(Point a,Point b){
double d=sqrt(
(a.x-b.x)*(a.x-b.x)
+
(a.y-b.y)*(a.y-b.y)
);
return d;
}
double pointToLine(Point p,Line l){
double s=fabs(
l.s.x*(l.e.y-p.y)+l.e.x*(p.y-l.s.y)+
p.x*(l.s.y-l.e.y)
)/2;//三角形的面积
double d=pointToPoint(l.s,l.e);
double h=s*2/d;
return h;
}
int main()
{
Point plt,plb,prt,prb , point;
Line lineLeft,lineRight,lineTop,lineBottom;
Rect rect;
scanf("%d %d",&plt.x,&plt.y);
scanf("%d %d",&prb.x,&prb.y);
scanf("%d %d",&point.x,&point.y);
plb.x=plt.x,plb.y=prb.y;
prt.x=prb.x,prt.y=plt.y;
lineLeft.e=plb,lineLeft.s=plt;
lineRight.e=prb,lineRight.s=prt;
lineBottom.e=plb,lineBottom.s=prb;
lineTop.e=plt,lineTop.s=prt;
rect.lt=plt,rect.rb=prb;
if(plt.x<=point.x && point.x<=prt.x && plb.y<=point.y && point.y<=plt.y){
cout<<"0";
}else{
//矩形x范围内上方或下方
if(plt.x<=point.x && point.x<=prt.x){
double d1 = pointToLine(point,lineTop);
double d2 = pointToLine(point,lineBottom);
printf("%lf",min(d1,d2));
return 0;
}
//矩形y范围内左侧或右侧
if(plb.y<=point.y && point.y<=plt.y){
double d1 = pointToLine(point,lineLeft);
double d2 = pointToLine(point,lineRight);
printf("%lf",min(d1,d2));
return 0;
}
//其他情况,取最近的顶点距离作为距离
double d1=pointToPoint(point,plt);
double d2=pointToPoint(point,plb);
double d3=pointToPoint(point,prt);
double d4=pointToPoint(point,prt);
printf("%lf",min(min(d1,d2),min(d3,d4)));
}
return 0;
}
MT1596
#include<bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
struct Line{
Point s,e;
};
struct Rect{
Point lt,rb;
};
bool intersect(Line ab,Line cd){
Point A=ab.e,B=ab.s;
Point C=cd.e,D=cd.s;
int fc=(C.y-A.y)*(A.x-B.x)-
(C.x-A.x)*(A.y-B.y);
int fd=(D.y-A.y)*(A.x-B.x)-
(D.x-A.x)*(A.y-B.y);
if(fc*fd<=0){
return true;
}else{
return false;
}
}
bool interSegment(Line ab,Line cd){
if(intersect(ab,cd) && intersect(cd,ab)){
return true;
}else{
return false;
}
}
bool inRect(Point point,Rect rect){
if(point.x<=rect.rb.x && point.x>=rect.lt.x
&& point.y <= rect.lt.y && point.x>=rect.rb.y){
return true;
}else{
return false;
}
}
int main( )
{
int x1,y1,x2,y2,x3,y3,x4,y4;
cin>>x1>>y1>>x2>>y2;
cin>>x3>>y3>>x4>>y4;
Point LT{x1,y1},
LB{x1,y2},RT{x2,y1},
RB{x2,y2},
lineA{x3,y3},lineB{x4,y4};
Rect rect{LT,RB};
Line diag1{LT,RB},diag2{LB,RT},
lineAB{lineA,lineB};
bool flag = false;
if(inRect(lineA,rect) || inRect(lineB,rect)){
flag=true;
}
if(interSegment(lineAB,diag1) || interSegment(lineAB,diag2)){
flag=true;
}
if(flag)cout<<"YES";
else cout<<"NO";
return 0;
}
MT1597
#include<bits/stdc++.h>
using namespace std;
struct POINT{
int x,y;
};
struct LINE{
POINT s,e;
};
bool parallel (LINE AB,LINE CD){
POINT A =AB.e,B = AB.s;
POINT C =CD.e,D = CD.s;
if(A.x==B.x||C.x==D.x||A.y==B.y||C.y==D.y)
if(A.x==B.x&&C.x==D.x||A.y==B.y&&C.y==D.y)
return true;
if((B.y-A.y) * (D.x - C.x )==(D.y-C.y)*(B.x-A.x))
return true;
return false;
}
int main( ){
POINT A,B,C,D;
cin>>A.x>>A.y>>B.x>>B.y>>C.x>>C.y>>D.x>>D.y;
bool flag =false;
if(parallel(LINE{A,B},LINE{C,D}))
flag =true;
if(parallel(LINE{A,C},LINE{C,D}))
flag =false;
if(flag)
cout <<"YES";
else cout <<"NO";
return 0;
}
MT1598
#include <bits/stdc++.h>
using namespace std;
struct POINT {
int x, y;
};
struct LINE {
POINT s, e;
};
struct EQUATION{//直线斜截式方程:y=ax+b
LINE line;
double a;
double b;
bool vertical;//是否垂直,即斜率不存在
};
EQUATION get_equa(LINE line){
EQUATION equation;
equation.line = line;
if (line.e.x == line.s.x)
equation.vertical = true;
else {
equation.a =(line.s.y - line.e.y)* 1.0 / (line.s.x - line.e.x);
equation.b = line.s.y - equation.a* line.s.x;
equation.vertical = false;
}
return equation;
}
//判断线的交点是不是在线段两个端点区域内
bool judge_point(LINE l1,LINE l2,double x, double y){
if((x-l1.s.x)* (x-l1.e.x)<= 0 && (y-l1.s.y) *(y-l1.e.y)<= 0
&&(x-l2.s.x)* (x-l2.e.x) <= 0 &&(y-l2.s.y) *(y-l2.e.y)<= 0)
return true;
else
return false;
}
//计算交点:存在一个斜率不存在的时候
void vertTrue(EQUATION vert,EQUATION normal){
double x = vert.line.e.x;
double y = normal.a*x +normal.b;
if (judge_point(vert.line,normal.line,x,y))
printf( "%.2lf %.2lf\n",x,y);
else
printf("-1\n");
}
//斜率都存在的时候
void vertFalse(EQUATION normal1,EQUATION normal2){
double diff_a = normal1.a - normal2.a;
if(diff_a ==0)
printf("-1\n");else {
double x =-(normal1.b - normal2.b)/(normal1.a - normal2.a);double y = normal1.a*x +normal1.b;
if(judge_point(normal1.line,normal2.line,x,y))
printf("%.2lf %.2lf\n", x,y);
else
printf("-1\n");
}
}
int main(){
LINE l1,l2;
scanf("%d%d%d%d",&l1.s.x,&l1.s.y,&l1.e.x,&l1.e.y);
scanf("%d%d%d%d",&l2.s.x,&l2.s.y,&l2.e.x,&l2.e.y);
EQUATION line1 = get_equa(l1);
EQUATION line2 = get_equa(l2);
if (line1.vertical && !line2. vertical)
vertTrue(line1,line2);
else if(!line1.vertical && line2.vertical)
vertTrue(line2,line1);
else if(line1.vertical && line2.vertical)
printf("-1\n");
else
vertFalse(line1,line2);
return 0;
}
MT1599
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
#define MAX_NUM 10010
#define PI 3.1415926
double res;
int ans, n, m, k,len,cnt = 0,minn = MAX_NUM, maxx = 0;
char ch, s1[MAX_NUM] = "", s2[MAX_NUM] = "";
bool flag = false;
int a[MAX_NUM]= {0},b[MAX_NUM]= {0};
double x,s,t;
struct Student{
string name;
int grade;
}student[ MAX_NUM];
bool cmp(Student s1,Student s2) { //cmp true不用交换位置false需要交换位置
if( s1.grade != s2.grade) {
if(s1.grade > s2.grade)
return true;
else
return false;
}else {
if(s1.name > s2.name)
return true;
else
return false;
}
}
int main( ) {
scanf( "%d\n",&n);
for(int i = 0; i < n; i++) {
cin >> student[i].name >> student[i].grade;
}
sort(student, student+n,cmp);for( int i = 0; i < n; i++) {
cout << student[i].name << endl;
}
return 0;
}
MT1600
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
#define MAX_NUM 10010
#define PI 3.1415926
double res;
int ans, n, m, k,len,cnt = 0,minn = MAX_NUM, maxx = 0;
char ch, s1[MAX_NUM] = "", s2[MAX_NUM] = "";
bool flag = false;
int a[MAX_NUM]= {0},b[MAX_NUM]= {0};
double x,s,t;
struct Student{
string name;
int grade;
int X,S,T;
double zongfen;
}student[ MAX_NUM];
bool cmp(Student s1,Student s2) { //cmp true不用交换位置false需要交换位置
if( s1.zongfen != s2.zongfen) {
if(s1.zongfen > s2.zongfen)
return true;
else
return false;
}else {
if(s1.name > s2.name)
return true;
else
return false;
}
}
int main( ) {
scanf( "%d\n",&n);
scanf("%lf %lf %lf\n", &x,&s,&t);for(int i = 0; i < n; i++) {
cin >> student[i ].name >> student[i].X >> student[i].S >>
student[i].T;
student[i].zongfen = x*student[i].X + s*student[i].S +t*student[i].T;
}
sort(student, student+n,cmp);for( int i = 0; i < n; i++) {
cout << student[i ].name << endl;
}
return 0;
}