1、hello word
#include<iostream>
using namespace std;
int main ()
{
cout<<"Hello World!"<<endl;
return 0;
}
2、打印沙漏
#include<iostream>
using namespace std;
int main ()
{
int n,row=0;
char c;
cin>>n>>c;
int i=0;
for(i=0;i<n;i++)
if((2*i*(i+2)+1)>n)
{
row=i-1;
break;
}
for(int i=row;i>=1;i--)
{
for(int k=row-i;k>=1;k--) cout<<" ";
for(int j=i*2+1;j>=1;j--) cout<<c;
cout<<endl;
}
for(int i=0;i<row;i++) cout<<" ";
cout<<c<<endl;
for(int i=1;i<=row;i++){
for(int k=row-i;k>=1;k--) cout<<" ";
for(int j=i*2+1;j>=1;j--) cout<<c;
cout<<endl;
}
cout<<(n-(2*row*(row+2)+1));
return 0;
}
3、个位数统计
#include<iostream>
#include<string>
using namespace std;
int a[10];
string str;
int main ()
{
cin>>str;
for (int i = 0; i < str.size(); i++)
{
a[str[i]-48]++;
}
for(int i=0;i<10;i++)
{
if(a[i]!=0)
{
printf("%d:%d\n",i,a[i]);
}
}
return 0;
}
4、计算摄氏温度
#include<iostream>
using namespace std;
int main ()
{
int f;
cin>>f;
printf("Celsius = %d",5*(f-32)/9);
return 0;
}
5、考试座位号
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int m,n;
class Mes{
public:
Mes(string id,int testid,int gameid):m_id(id),m_testid(testid),
m_gameid(gameid){};
string m_id;
int m_testid;
int m_gameid;
};
int main (){
cin>>n;
vector<Mes>mv;
for(int i=1;i<=n;i++)
{
string str;
int testid,gameid;
cin>>str>>testid>>gameid;
Mes ms(str,testid,gameid);
mv.push_back(ms);
}
cin>>m;
int ret;
while(m--){
cin>>ret;
for(int i=0;i<n;i++)
{
if(ret==mv[i].m_testid)
cout<<mv[i].m_id<<" "<<mv[i].m_gameid<<endl;
}
}
return 0;
}
6、连续因子
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
vector<int>a;
vector<int>ans;
bool isprime(int n)
{
if(n<2)
return false;
for(int i=2;i<sqrt(n);i++)
if(n%i==0)
return false;
return true;
}
int main ()
{
cin>>n;
int res=-1;
if(isprime(n)){
cout<<1<<endl<<n;
return 0;
}
for(int i=2;i<=n;i++)
{
int tmp=1;
a.clear();
int cnt=0;
for(int j=i;j<=n;j++)
{
tmp*=j;
if(n%tmp!=0)
break;
a.push_back(j);
cnt++;
}
if(res<cnt)
{
res=cnt;
ans.clear();
for(int i=0;i<a.size();i++)
ans.push_back(a[i]);
}
}
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
{
if(i==0)
cout<<ans[0];
else
cout<<"*"<<ans[i];
}
return 0;
}
7、念数字
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string>s;
s.push_back("ling");
s.push_back("yi");
s.push_back("er");
s.push_back("san");
s.push_back("si");
s.push_back("wu");
s.push_back("liu");
s.push_back("qi");
s.push_back("ba");
s.push_back("jiu");
string n;
cin>>n;
int i=1;
if(n[0]=='-'){
printf("fu");
}
else{
cout<<s[n[0]-'0'];
}
for(i;i<n.size();i++){
cout<<" "<<s[n[i]-'0'];
}
return 0;
}
8、求整数段和
#include<iostream>
using namespace std;
int main ()
{
int a,b;
cin>>a>>b;
int k=0,sum=0;
for(int i=a;i<=b;i++)
{
printf("%5d",i);
k++;
sum+=i;
if(k%5==0)
cout<<endl;
}
if(k%5!=0)
cout<<endl;
printf("Sum = %d\n",sum);
return 0;
}
9、N个数求和
#include<iostream>
using namespace std;
long long int gcd(long long int a, long long int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
long long int N = 0, a = 0, b = 0, sum_a = 0, sum_b = 1, t;
scanf("%lld", &N);
for (int i = 0; i < N; i++)
{
scanf("%lld/%lld", &a, &b);
sum_a *= b;
sum_a += a * sum_b;
sum_b *= b;
t = gcd(sum_a, sum_b);
sum_a /= t;
sum_b /= t;
}
if (sum_a % sum_b == 0)
printf("%lld\n", sum_a / sum_b);
else if (sum_a < sum_b)
printf("%lld/%lld\n", sum_a, sum_b);
else
printf("%lld %lld/%lld\n", sum_a / sum_b, sum_a % sum_b, sum_b);
return 0;
}
10、比较大小
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
int mmin=min(a,b);
mmin=min(mmin,c);
int mmax=max(a,b);
mmax=max(mmax,c);
int mmid;
if(a>mmin&&a<mmax)
mmid=a;
if(b>mmin&&b<mmax)
mmid=b;
if(c>mmin&&c<mmax)
mmid=c;
cout<<mmin<<"->"<<mmid<<"->"<<mmax<<endl;
return 0;
}
11、A-B
#include<stdio.h>
#include<string.h>
int main(){
char a[100000],b[100000];
gets(a);
gets(b);
for(int i=0;i<strlen(a);i++)
{
if(strchr(b,a[i]))
continue;
else
printf("%c",a[i]);
}
return 0;
}
12、计算指数
#include<iostream>
#include<cmath>
using namespace std;
int n;
int main ()
{
cin>>n;
printf("2^%d = %d\n",n,(int)pow(2,n));
return 0;
}
13、计算阶乘和
#include<iostream>
using namespace std;
int func(int i)
{
int sum=1;
for(int j=i;j>=1;j--)
sum*=j;
return sum;
}
int main()
{
int n,sum=0;
cin>>n;
for(int i=1;i<=n;i++)
sum+=func(i);
cout<<sum<<endl;
return 0;
}
14、简单题
#include<iostream>
using namespace std;
int main ()
{
cout<<"This is a simple problem."<<endl;
return 0;
}
15、和奥巴马一起画方块
#include<iostream>
using namespace std;
int main ()
{
int n;
char c;
scanf("%d %c",&n,&c);
if(n%2==0){
for(int i=1;i<=n/2;i++){
for(int j=1;j<=n;j++)
printf("%c",c);
cout<<endl;
}
}
else
for(int i=1;i<=n/2+1;i++){
for(int j=1;j<=n;j++)
printf("%c",c);
cout<<endl;
}
return 0;
}
16、查验身份证
#include<iostream>
#include<string>
using namespace std;
int n;
char arr[]={"10X98765432"};
int brr[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int main ()
{
cin>>n;
bool ret=true;
string str;
while(n--)
{
str.clear();
cin>>str;
int ans=0;
for(int i=0;i<=16;i++)
ans+=(str[i]-'0')*brr[i];
ans%=11;
if(str[17]!=arr[ans]){
cout<<str<<endl;
ret=false;
}
}
if(ret)
cout<<"All passed"<<endl;
return 0;
}
17、到底有多二
#include<iostream>
#include<string>
using namespace std;
int main ()
{
string str;
cin>>str;
int count=0,cnt=0;
bool ret=false;
for(int i=0;i<str.size();i++)
if(str[i]-'0'==2)
count++;
if(str[0]=='-')
ret=true;
float ans=0;
if(ret)
{
cnt=str.size()-1;
ans=((float)count/cnt)*1.5;
if(str[str.size()-1]-'0'==0
||str[str.size()-1]-'0'==2
||str[str.size()-1]-'0'==4
||str[str.size()-1]-'0'==6
||str[str.size()-1]-'0'==8)
ans*=2;
}
else{
cnt=str.size();
ans=((float)count/cnt);
if(str[str.size()-1]-'0'==0
||str[str.size()-1]-'0'==2
||str[str.size()-1]-'0'==4
||str[str.size()-1]-'0'==6
||str[str.size()-1]-'0'==8)
ans*=2;
}
printf("%.2f%%\n",ans*100);
return 0;
}
18、大笨钟
#include<iostream>
using namespace std;
int main ()
{
int hh,mm;
scanf("%d:%d",&hh,&mm);
if(hh>=0&&hh<12)
{
printf("Only %02d:%02d. Too early to Dang.",hh,mm);
return 0;
}
if(hh==12&&mm==0)
{
printf("Only %02d:%02d. Too early to Dang.",hh,mm);
return 0;
}
if(mm==0){
for(int i=1;i<=hh-12;i++)
cout<<"Dang";
}
if(mm!=0)
for(int i=1;i<=hh-11;i++)
cout<<"Dang";
cout<<endl;
return 0;
}
19、谁先倒
#include<iostream>
using namespace std;
int a,b;
int n;
int a_count,b_count;
int main()
{
cin>>a>>b;
cin>>n;
while(n--){
int as,ap,bs,bp;
cin>>as>>ap>>bs>>bp;
if((bp==(as+bs))&&(ap!=(as+bs)))
b_count++;
if((ap==(as+bs))&&(bp!=(as+bs)))
a_count++;
if(a_count==a+1)
{
cout<<"A"<<endl;
cout<<b_count<<endl;
return 0;
}
if(b_count==b+1)
{
cout<<"B"<<endl;
cout<<a_count<<endl;
return 0;
}
}
return 0;
}
20、帅到没朋友
#include <stdio.h>
int main()
{
int a[1000000]={ 0 };
int N,K,M;
int i,n,m,sum=0;
scanf("%d",&N);
while(N--)
{
scanf("%d",&K);
for(i=1;i<=K;i++)
{
scanf("%d",&n);
if(K!=1)
{
a[n]+=K;
}
}
}
scanf("%d",&M);
while(M--)
{
scanf("%d",&m);
if(a[m]==0)
{
if(sum!=0) printf(" ");
printf("%05d",m);
a[m]=1;
sum++;
}
}
if(sum==0) printf("No one is handsome");
return 0;
}
21、重要的事情说三遍
#include<iostream>
using namespace std;
int main ()
{
int n=3;
while(n--){
cout<<"I'm gonna WIN!"<<endl;
}
}
22、奇偶分家
#include<iostream>
using namespace std;
int n,j_sum,o_sum;
int main ()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int ret;
cin>>ret;
if(ret%2==0)
o_sum++;
else
j_sum++;
}
cout<<j_sum<<" "<<o_sum<<endl;
return 0;
}
23、输出GPLT
#include<iostream>
#include<string>
using namespace std;
int arr[5];
int main ()
{
string str;
cin>>str;
for(int i=0;i<str.size();i++)
{
if(str[i]=='G'||str[i]=='g')
arr[1]++;
if(str[i]=='P'||str[i]=='p')
arr[2]++;
if(str[i]=='L'||str[i]=='l')
arr[3]++;
if(str[i]=='T'||str[i]=='t')
arr[4]++;
}
while(arr[1]!=0 || arr[2]!=0 || arr[3]!=0 || arr[4]!=0){
for(int i=1;i<=4;i++)
{
if(arr[1]!=0)
{
cout<<'G';
arr[1]--;
}
if(arr[2]!=0)
{
cout<<'P';
arr[2]--;
}
if(arr[3]!=0)
{
cout<<'L';
arr[3]--;
}
if(arr[4]!=0)
{
cout<<'T';
arr[4]--;
}
}
}
cout<<endl;
return 0;
}
24、后天
#include<iostream>
using namespace std;
int main ()
{
int n;
cin>>n;
if(n==5){
cout<<7<<endl;
return 0;}
cout<<(n+2)%7<<endl;
return 0;
}
25、正整数A+B
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
string a,b;
bool check(string str)
{
for(int i=0;i<str.size();i++)
{
if(str[i]=='-' || str[i]=='.'|| str[i]-'0'<=0 || str[i]-'0'>=9)
return false;
}
int ret;
stringstream ss;
ss<<str;
ss>>ret;
if(ret<1 || ret >1000)
return false;
return true;
}
int main ()
{
cin>>a>>b;
bool bool_a=check(a);
bool bool_b=check(b);
if(bool_a&&bool_b)
{
int aa,bb;
stringstream ss,tt;
ss << a;
ss >> aa;
tt<<b;
tt>>bb;
cout<<a<<" + "<<b<<" = "<<aa+bb<<endl;
}
if(!bool_a&&bool_b)
{
int bb;
stringstream ss;
ss<<b;
ss>>bb;
printf("? + %d = ?\n",bb);
}
if(bool_a&&!bool_b)
{
int aa;
stringstream ss;
ss<<a;
ss>>aa;
printf("%d + ? = ?\n",aa);
}
if(!bool_a&&!bool_b)
{
printf("? + ? = ?\n");
}
return 0;
}
26、I LOVE GPLT
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("I Love GPLT");
for(int i=0;i<str.size();i++)
cout<<str[i]<<endl;
return 0;
}
27、出租
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string str;
vector<int>arr;
int a[10];
int main ()
{
cin>>str;
for(int i=0;i<str.size();i++){
a[str[i]-'0']++;
}
bool flag=false;
printf("int[] arr = new int[]{");
for(int i=9;i>=0;i--){
if(!flag&&a[i]!=0){
printf("%d",i);
flag=true;
arr.push_back(i);
continue;
}
if(flag&&a[i]!=0){
cout<<","<<i;
arr.push_back(i);
}
}
printf("};\n");
flag=false;
printf("int[] index = new int[]{");
for(int i=0;i<11;i++)
{
int ret=str[i]-'0';
for(int j=0;j<arr.size();j++){
if(!flag&&ret==arr[j]){
printf("%d",j);
flag=true;
continue;
}
if(flag&&ret==arr[j])
cout<<","<<j;
}
}
printf("};\n");
return 0;
}
28、判断素数
#include<iostream>
#include<cmath>
using namespace std;
int n,ret;
bool check(int m)
{
int k=(int)sqrt( (double)m );
for(int i=2;i<=k;i++)
if(m%i==0)
return false;
return true;
}
int main ()
{
cin>>n;
while(n--)
{
cin>>ret;
if(ret==1)
{
cout<<"No"<<endl;
continue;
}
if(check(ret))
cout<<"Yes"<<endl;
if(!check(ret))
cout<<"No"<<endl;
}
return 0;
}
29、是不是太胖了
#include<iostream>
#include<cmath>
using namespace std;
int n,ret;
bool check(int m)
{
int k=(int)sqrt( (double)m );
for(int i=2;i<=k;i++)
if(m%i==0)
return false;
return true;
}
int main ()
{
cin>>n;
while(n--)
{
cin>>ret;
if(ret==1)
{
cout<<"No"<<endl;
continue;
}
if(check(ret))
cout<<"Yes"<<endl;
if(!check(ret))
cout<<"No"<<endl;
}
return 0;
}
30、一帮一
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Person
{
public:
Person(int sex,string name):m_sex(sex),m_name(name){};
int m_sex;
string m_name;
bool ret=false;
};
int n;
vector<Person>mv;
int main ()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int sex;
string name;
cin>>sex>>name;
mv.push_back(Person(sex,name));
}
for(int i=0;i<n/2;i++)
{
for(int j=n-1;j>=0;j--)
{
if((mv[i].m_sex + mv[j].m_sex==1)&&(mv[j].ret==false))
{
cout<<mv[i].m_name<<" "<<mv[j].m_name<<endl;
mv[j].ret=true;
break;
}
}
}
return 0;
}
|