openjudge_noi

1.5编程基础之循环控制

38 计算多项式的导函数

#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;


int main()
{
	int casenum;casenum=1;
	while(casenum--)
	{
		int n;
		cin>>n;
		int a[n+1];
		for(int j=n;j>=0;--j)
		{
			cin>>a[j];
		}
		if(n==0)
		{
			cout<<0<<endl;
			continue;
		}
		for(int j=n;j>=1;--j)
		{
			cout<<a[j]*j;
			if(j!=1)cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}

39

#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;


int main()
{
	int n,s=0;
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		int flag=0;
		if(i%7==0)flag=1;
		int t=i;
		while(t)
		{
			if(t%10==7)flag=1;
			t/=10;
		}
		if(flag==0)
		{
			//cout<<i<<endl;
			s+=i*i;
		}
	}
	cout<<s;
	return 0;
}
 

40

#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;

int fun(int t)
{
	int c=0;
	while(t)
	{
		if(t%10==1)c++;
		t/=10;
	}
	return c;
}

int main()
{
	int n, s=0;
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		s+=fun(i);
	}
	cout<<s;
	return 0;
}

42

#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;


int main()
{
	int height,weight;
	char c;
	int flag;
	cin>>height>>weight>>c>>flag;
	if(flag==1)
	{
		for(int i=0;i<height;++i)
		{
			for(int j=0;j<weight;++j)
			{
				cout<<c;
			}
			cout<<endl;
		}
	}
	else
	{
		for(int i=0;i<height;++i)
		{
			for(int j=0;j<weight;++j)
			{
				if(j==0||j==weight-1||i==0||i==height-1)
				cout<<c;
				else cout<<" ";
			}
			cout<<endl;
		}
	}
	return 0;
}
 

43 质因数分解

#include<iostream>
#include<stdio.h>
#include<string>
#include <math.h>

using namespace std;


int main()
{
	int n;cin>>n;
	for(int i=2;i<sqrt(n);++i)
	{
		if(n%i==0)cout<<n/i;
	}
	return 0;
}

44

#include<iostream>
#include<stdio.h>
#include<string>
#include <math.h>

using namespace std;

bool shushu(int t)
{
	for(int i=2;i<=sqrt(t);++i)
	{
		if(t%i==0)return false;
	}
	return true;
}
int main()
{
	int n;cin>>n;
	int s=0;
	int ans;
	for(int i=2;i!=0;++i)
	{
		if(shushu(i))s++;
		if(s==n)
		{
			ans=i;break;
		}
	}
	cout<<ans;
	return 0;
}

45

#include<iostream>
#include<stdio.h>
#include<string>
#include <math.h>

using namespace std;

int main()
{
	int n;cin>>n;
	int c=1;int t=0;int s=0;
	for(int i=1;i<=n;++i)
	{
		s+=c;
		t++;
		if(t==c)
		{
			t=0;c++;
		}
	}
	cout<<s;
	return 0;
}
 

1.6编程基础之一维数组

3

#include<iostream>
#include<stdio.h>
#include<string>
#include <math.h>

using namespace std;

int main()
{
	int a[10];
	for(int i=0;i<10;++i)cin>>a[i];
	double s=0;
	s=
	a[0]*28.9+
	a[1]*32.7+
	a[2]*45.6+
	a[3]*78+
	a[4]*35+
	a[5]*86.2+
	a[6]*27.8+
	a[7]*43+
	a[8]*56+
	a[9]*65;
	printf("%.1f",s);
	return 0;
}
 

4

#include<iostream>
#include<stdio.h>
#include<string>
#include <math.h>

using namespace std;

int main()
{
	int n;cin>>n;
	int a[n];
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i)cout<<a[n-1-i]<<" ";
	return 0;
}

5

#include<iostream>
#include<stdio.h>
#include<string>
#include <math.h>
#include <string.h>

using namespace std;

int main()
{
	int n;cin>>n;
	int a[4];
	memset(a,0,sizeof(a));
	for(int i=0;i<n;++i)
	{
		int t;cin>>t;
		if(t>=0&&t<=18)
			a[0]++;
		else if(t>18&&t<=35)
			a[1]++;
		else if(t>35&&t<=60)
			a[2]++;
		else if(t>=61)
		a[3]++;
	}
	for(int i=0;i<4;++i)
	{
		a[i]*=100;
		double ans=(double)a[i]/n;
		printf("%.2f%%\n",ans);
	}
	
	return 0;
}
 

6

#include<iostream>
#include<stdio.h>
#include<string>
#include <math.h>
#include <string.h>

using namespace std;

int main()
{
	int l,n;
	cin>>l>>n;
	int a[l+1];
	memset(a,0,sizeof(a));
	for(int i=0;i<n;++i)
	{
		int c,d;
		cin>>c>>d;
		for(int j=c;j<=d;++j)a[j]=1;
	}	
	int s=0;
	for(int i=0;i<=l;++i)
	{
		if(a[i]==0)s++;
	}
	cout<<s;
	return 0;
}
 

7

#include<iostream>
#include<stdio.h>
#include<string>
#include <math.h>
#include <string.h>
#include<stdlib.h>

using namespace std;

int main()
{
	int a[3005];
	memset(a,0,sizeof(a));
	int n;cin>>n;
	int t,t1;cin>>t;
	for(int i=0;i<n-1;++i)
	{
		cin>>t1;
		int t2=abs(t1-t);
		a[t2]=1;
		t=t1;
	}
	bool flag=true;
	for(int i=1;i<=n-1;++i)
	{
		if(a[i]==0)flag=false;
	}
	if(flag==false)
	cout<<"Not jolly";
	else
	cout<<"Jolly";
	return 0;
}
 

8

#include <iostream>

using namespace std;

int main()
{
	int a[200],b[200];
	int n;cin>>n;
	int t1,t2;cin>>t1>>t2;
	int c[105],d[105];
	for(int i=0;i<t1;++i)cin>>c[i];
	for(int i=0;i<t2;++i)cin>>d[i];
	int av=0,bv=0;
	for(int i=0;i<n;++i)
	{
		a[i]=c[i%t1];
		b[i]=d[i%t2];
		if(a[i]!=b[i])
		{
			if(a[i]==0&&b[i]==2)av++;
			else if(a[i]==2&&b[i]==5)av++;
			else if(a[i]==5&&b[i]==0)av++;
			else bv++;
		}
	}
	if(av==bv)cout<<"draw";
	else if(av>bv)cout<<"A";
	else cout<<"B";
	return 0;
}
 

9

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	int n;cin>>n;
	int a[n],b[n];int s=0;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i)
	{
		cin>>b[i];
		s+=a[i]*b[i];
	}
	cout<<s;
	
	return 0;
}
 

10 高精度加法

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main(){
	int a[205],b[205],c[205];
	memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));
	char s[205];
	int lenm;
	scanf("%s",s);int len=strlen(s);lenm=len;for(int i=0;i<len;++i)a[i]=s[len-1-i]-'0';
	scanf("%s",s);len=strlen(s);if(len>lenm)lenm=len;for(int i=0;i<len;++i)b[i]=s[len-1-i]-'0';
	int t=0;
	for(int i=0;i<lenm;++i)
	{
		t+=(a[i]+b[i]);
		c[i]=t%10;
		t/=10;
	}
	if(t>0)c[lenm]=1;
	int flag=true;
	for(int i=lenm;i>=0;--i)
	{
		if(flag==true&&i==0)cout<<0;
		if(c[i]==0&&flag==true)continue;
		
		flag=false;
		cout<<c[i];
	}
	return 0;
}
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        BigInteger b1,b2;
        Scanner in=new Scanner(System.in);
        b1=in.nextBigInteger();b2=in.nextBigInteger();
        System.out.println(b1.add(b2));
    }
}

11 高精度减法

#include<iostream>
#include<string> 
using namespace std;

int A,B,x[999],y[999],z[999],k,m;

int main(){
	string a,b;
	cin>>a>>b;A=a.size();B=b.size();
	for(int i=0;i<A;++i)x[i]=a[A-1-i]-'0';
	for(int i=0;i<B;++i)y[i]=b[B-1-i]-'0';
	while(k<A||k<B)
	{
		if(x[k]<y[k])x[k]+=10,x[k+1]--;
		x[k]-=y[k];
		k++;
	}
	while(x[k]==0&&k>0)k--;
	while(k>=0){
		cout<<x[k--];
	}
	
	return 0;
}
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        BigInteger b1,b2;
        Scanner in=new Scanner(System.in);
        b1=in.nextBigInteger();b2=in.nextBigInteger();
        System.out.println(b1.subtract(b2));
    }
}

12

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        BigInteger b1;
        int n=in.nextInt();
        b1=BigInteger.valueOf(1);
        for(int i=1;i<=n;++i)
        {
            b1=b1.multiply(BigInteger.valueOf(2));
        }
        System.out.println(b1);
    }
}

13

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        BigInteger b;b=in.nextBigInteger();
        int flag=1;
        for(int i=2;i<=9;++i)
        {
            if(b.mod(BigInteger.valueOf(i)).intValue()==0){
                flag=0;
                System.out.print(i+" ");
            }
        }
        if(flag==1) System.out.println("none");
    }
}

14

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        BigInteger b;b=BigInteger.valueOf(1);
        int n=in.nextInt();
        for(int i=1;i<=n;++i)
        {
            b=b.multiply(BigInteger.valueOf(i));
        }
        System.out.println(b);
    }
}

15

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        BigInteger b,s;b=BigInteger.valueOf(1);
        s=BigInteger.valueOf(0);
        int n=in.nextInt();
        for(int i=1;i<=n;++i)
        {
            b=b.multiply(BigInteger.valueOf(i));
            s=s.add(b);
        }
        System.out.println(s);
    }
}

1.7编程基础之字符串

01:统计数字字符个数

#include <bits/stdc++.h>

using namespace std;

int ans;

int main()
{
	string s;
	getline(cin,s);
	for(int i=0;i<s.size();++i)
	{
		if(isdigit(s[i]))ans++;
	}
	cout<<ans;
	return 0;
} 
import java.util.*;

public class Main
{	
	static String s;
	static int ans;
	public static void main(String []args)
	{
		Scanner sc=new Scanner(System.in);
		s=sc.nextLine();
		for(int i=0;i<s.length();++i)
		{
			if(s.charAt(i)>='0'&&s.charAt(i)<='9')ans++;
		}
		System.out.println(ans);
	}
	
}

02:找第一个只出现一次的字符

#include <bits/stdc++.h>

using namespace std;

int a[200];

int main()
{
	string s;
	cin>>s;
	for(int i=0;i<s.size();++i)
	{
		a[s[i]]++;
	}
	for(int i=0;i<s.size();++i)
	{
		if(a[s[i]]==1)
		{
			cout<<s[i];
			return 0;
		}
	}
	cout<<"no"<<endl;
	return 0;
} 
import java.util.*;

public class Main
{	
	static String s;
	static int a[]=new int [200];
	public static void main(String []args)
	{
		Scanner sc=new Scanner(System.in);
		s=sc.next();
		for(int i=0;i<s.length();++i)
		{
			a[s.charAt(i)]++;
		}
		for(int i=0;i<s.length();++i)
		{
			if(a[s.charAt(i)]==1)
			{
				System.out.println(s.charAt(i));
				return ;
			}
		}
		System.out.println("no");
	}
	
}



04

#include<iostream>
#include<string> 
using namespace std;

int main(){
	int n;cin>>n;
	string a,b;
	while(n--)
	{
		cin>>a>>b;
		if(a=="Rock")
		{
			if(b=="Rock")cout<<"Tie";
			if(b=="Scissors")cout<<"Player1";
			if(b=="Paper")cout<<"Player2";
		}
		if(a=="Scissors")
		{
			if(b=="Rock")cout<<"Player2";
			if(b=="Scissors")cout<<"Tie";
			if(b=="Paper")cout<<"Player1";
		}
		if(a=="Paper")
		{
			if(b=="Rock")cout<<"Player1";
			if(b=="Scissors")cout<<"Player2";
			if(b=="Paper")cout<<"Tie";
		}
		cout<<endl;
	}
	
	return 0;
}

05

#include<iostream>
#include<string> 
#include <cstdio>
#include<string.h>
using namespace std;

int main(){
	char s[200];gets(s);
	int S=strlen(s);
	for(int i=0;i<S;++i)
	{
		putchar(s[i]+s[(i+1)%S]);
	}
	return 0;
}

06

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	bool flag=true;
	string s;cin>>s;
	if(isdigit(s[0]))flag=false;
	for(int i=0;i<s.length();++i)
	{
		if(!(isdigit(s[i])||isalpha(s[i])||s[i]=='_'))flag=false;
	}
	if(flag==false)cout<<"no";
	else cout<<"yes";
	return 0;
}

07

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	string s;cin>>s;
	for(int i=0;i<s.size();++i){
		if(s[i]=='A')cout<<"T";
		if(s[i]=='G')cout<<"C";
		if(s[i]=='T')cout<<"A";
		if(s[i]=='C')cout<<"G";
	}
	return 0;
}

08

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	string s;getline(cin,s);
	for(int i=0;i<s.size();++i){
		if(isalpha(s[i]))
		{
			if(s[i]=='z'){
				s[i]='a';continue;
			}
			if(s[i]=='Z'){
				s[i]='A';continue;
			}
			s[i]++;
		}
	}
	cout<<s;
	return 0;
}

09

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	string s;getline(cin,s);
	for(int i=0;i<s.size();++i){
		if(s[i]>='A'&&s[i]<='Z')
		{
			s[i]-=5;
			if(s[i]<'A'){
				s[i]+=('Z'-'A'+1);
			}
		}
	}
	cout<<s;
	return 0;
}

10

#include<iostream>
#include<cstdio>

using namespace std;
char v[50],V[50];
int main(){
	for(int i=0;i<26;++i)v[i]='\0',V[i]='\0';
	string mi,min,now;
	cin>>mi>>min>>now;
	int M=mi.size();
	bool flag=true;
	for(int i=0;i<M;++i){
		int t=mi[i]-'A';
		if(v[t]!='\0'&&v[t]!=min[i])flag=false;//判断是否一对多 
		v[t]=min[i];
		V[min[i]-'A']=mi[i];
	} 
	for(int i=0;i<26;++i)if(v[i]=='\0'||V[i]=='\0')flag=false;//判断mi是否都有对应的min
	if(flag==false){
		cout<<"Failed";
	}
	else{
		for(int i=0;i<now.size();++i)cout<<v[now[i]-'A'];
	}
	return 0;
}

11

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	string s;cin>>s;
	for(int i=0;i<s.size();++i){
		if(s[i]<='Z')s[i]+=('a'-'A');
		else s[i]+=('A'-'a');
		s[i]+=3;
		if(s[i]>'Z'&&s[i]<'a')s[i]+=('A'-'Z'-1);
		if(s[i]>'z')s[i]+=('a'-'z'-1);
	}
	for(int i=s.size()-1;i>=0;--i)cout<<s[i];
	return 0;
}

13

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	string s;getline(cin,s);
	for(int i=0;i<s.size();++i)s[i]=toupper(s[i]);
	cout<<s;
	return 0;
}

14

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	string s;getline(cin,s);
	for(int i=0;i<s.size();++i){
		if(s[i]<='z'&&s[i]>='a')s[i]=toupper(s[i]);
		else
		if(s[i]<='Z'&&s[i]>='A')s[i]=tolower(s[i]);
	}
	cout<<s;
	return 0;
}

15

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	int n;cin>>n;
	string s;
	while(n--)
	{
		cin>>s;
		if(isalpha(s[0]))s[0]=toupper(s[0]);
		for(int i=1;i<s.size();++i){
			if(isalpha(s[i]))s[i]=tolower(s[i]);
		}
		cout<<s<<endl;
	} 
	return 0;
}

16

#include<iostream>
#include<cstdio>
#include<string.h>

using namespace std;

int main(){
	string a,b;
	getline(cin,a);getline(cin,b);
	char A[100],B[100];
	strcpy(A,a.c_str());strcpy(B,b.c_str());
	for(int i=0;i<100;++i){
		A[i]=tolower(A[i]);
		B[i]=tolower(B[i]);
	}
	int t=strcmp(A,B);
	if(t<0)
	cout<<"<";
	else if(t==0)
	cout<<"=";
	else 
	cout<<">";
	
	return 0;
}

17

#include<iostream>
#include<cstdio>
#include<string.h>
char A[100],B[100];

using namespace std; 
int main(){
	string a,b;
	getline(cin,a);
	getline(cin,b);
	
	int ai=0,bi=0;
	for(int i=0;i<1000;++i)
	{
		if(b[i]!=' '&&i<b.size())B[bi++]=b[i];
		if(a[i]!=' '&&i<a.size())A[ai++]=a[i];
	}
	for(int i=0;i<1000;++i){
		A[i]=tolower(A[i]);
		B[i]=tolower(B[i]);
	}
	int t=strcmp(A,B);
	if(t==0)
	cout<<"YES"<<endl;
	else 
	cout<<"NO"<<endl;
	return 0;
}

18 字符串匹配

模拟暴力

#include<iostream>

using namespace std; 

int main(){
	string a,b;
	cin>>a>>b;
	bool subf=false;
	if(a.size()<b.size())swap(a,b);
	for(int i=0;i<a.size()-b.size()+1;++i)
	{
		int j;
		for(j=0;j<b.size();++j){
			if(b[j]!=a[i+j])break;
		}
		if(j==b.size())subf=true;
	}
	if(subf)
		cout<<b<<" is substring of "<<a;
	else
		cout<<"No substring";
	return 0;
}

string.find函数

#include<iostream>

using namespace std; 

int main(){
	string a,b;
	cin>>a>>b;
	if(a.size()<b.size())swap(a,b);
	if(a.find(b)!=a.npos)cout<<b<<" is substring of "<<a;
	else cout<<"No substring";
	return 0;
}

19循环移位字符串+匹配子串

#include<iostream>

using namespace std; 

int main(){
	string a,b;
	cin>>a>>b;
	bool flag=false;
	if(a.size()<b.size())swap(a,b);
	for(int i=0;i<a.size();++i){
		if(a.find(b)!=a.npos){
			flag=true;
		}
		a=a.substr(1,a.size()-1)+a[0];
	} 
	if(flag)cout<<"true";
	else cout<<"false";
	return 0;
}

20

#include<iostream>

using namespace std; 

int main(){
	string s,a,aa; 
	cin>>s;
	if(s.size()<=3)
	{
		if(s.size()<3){
			cout<<s;return 0;
		}
		if(s.substr(1,2)=="er"||s.substr(1,2)=="ly"){
			cout<<s[0];return 0;
		}
	}
	a=s.substr(s.size()-2,2); 
	aa=s.substr(s.size()-3,3);
	if(a.find("er")!=a.npos||a.find("ly")!=a.npos)
	{
		s=s.substr(0,s.size()-2);
	}
	else if(aa.find("ing")!=aa.npos){
		s=s.substr(0,s.size()-3);
	}
	cout<<s;
	return 0;
}

22

#include<iostream>

using namespace std; 

int main(){
	string ans,u,m,e;
	bool flag=true;
	cin>>ans;
	int n;cin>>n; 
	while(n--)
	{
		cin>>u>>m>>e;
		if(e==ans)
		{
			flag=false;
			for(int i=0;i<m.size();++i)
			{
				if(m[i]>='a'&&m[i]<='z')m[i]=toupper(m[i]);
				else
				if(m[i]>='A'&&m[i]<='Z')m[i]=tolower(m[i]);
			} 
			cout<<u<<" "<<m<<endl;
		}
	}
	if(flag)cout<<"empty";
	return 0;
}

23

#include<iostream>

using namespace std; 

int main(){
	string s;
	cin>>s;cout<<s;
	while(cin>>s)
	{
		cout<<" "<<s;
	}
	return 0;
}


24

#include<iostream>

using namespace std; 

int main(){
	string s;
	cin>>s;cout<<s.size();
	while(cin>>s)cout<<","<<s.size();
}


25

#include<iostream>

using namespace std; 

int main(){
	string s,a,b;
	cin>>s;a=s;b=s;
	while(cin>>s){
		if(s.size()>a.size())a=s;
		if(s.size()<b.size())b=s;
	}
	cout<<a<<endl<<b;
}


26

#include<iostream>
#include <sstream>
#include<stdlib.h>
#include<stdio.h>

using namespace std; 
string s,a,b;
int main(){
	getline(cin,s,',');
	getline(cin,a,',');
	cin>>b;
	
	int pa=s.find(a);int pb=s.rfind(b);
	//int pla=s.find_last_of(a);int plb=s.find_first_of(b);
	if(pa==-1||pb==-1||pb<pa)cout<<"-1";
	
	else cout<<pb-pa-(int)a.size();
	return 0;
}

posted @ 2022-11-17 23:04  林动  阅读(30)  评论(0编辑  收藏  举报