/*自己写的,有些问题*/
#include
#include
using namespace std;
int main()
{
int n;
cin>>n;
int flagA=0,flagB=0,flagC=0,others=0;
int Tot,tot;
while(n!=-1)
{
if(n==1)
flagA++;
else if(n==2)
flagB++;
else if(n==3)
flagC++;
else
others++;
cin>>n;
}
Tot=flagA+flagB+flagC+others;
tot=flagA+flagB+flagC;
cout<<"A="<<flagA<<endl;
cout<<"B="<<flagB<<endl;
cout<<"C="<<flagC<<endl;
cout<<"Tot="<<tot<<endl;
if(flagA>(Tot/2))
cout<<"A-yes"<<endl;
else if(flagB>(Tot/2))
cout<<"B-yes"<<endl;
else if(flagC>(Tot/2))
cout<<"C-yes"<<endl;
else
cout<<"all-No"<<endl;
return 0;
}
#include
#include
#include
#include
using namespace std;
int a[100] = {},tp,sum = 0;
string s;
int main()
{
scanf("%d",&tp);
while(tp != -1)
{
a[tp] ++;
scanf("%d",&tp);
}
sum = a[1] + a[2] + a[3];
printf("A=%d\nB=%d\nC=%d\nTot=%d\n",a[1],a[2],a[3],sum);
sum ++;
if(max(a[1],max(a[2],a[3])) > sum / 2)
{
if(a[1] > max(a[2],a[3]))s = "A-yes";
else if(a[2] > max(a[1],a[3]))s = "B-yes";
else if(a[3] > max(a[2],a[1]))s = "C-yes";
cout << s << endl;
}
else if(max(a[1],max(a[2],a[3])) <= sum / 2)
{
printf("all-NO\n");
}
return 0;
}
总结:C++中有max函数可以进行两数相比较,头文件algorithm。
(2)统计字符
int main()
{
char a;
int Letters = 0, Digits = 0, Others = 0;
while ((a = getchar()) != '?')
{
if ((a >= '0') && (a <= '9'))
Digits++;
else if ((a >= 'a') && (a <= 'z'))
Letters++;
else if ((a >= 'A') && (a <= 'Z'))
Letters++;
else
Others++;
}
printf("Letters=%d\n", Letters);
printf("Digits=%d\n", Digits);
printf("Others=%d\n", Others);
return 0;
}
不能直接写(a-Z)会报错。
using namespace std;
int main()
{
char s;
int n=0,l=0,o=0;
while(s=getchar(),s!='?')
{
if ( isalpha(s) )
l++ ;
else
if( isdigit(s))
n++;
else o++;
}
cout<<"Letters="<<l<<endl;
cout<<"Digits="<<n<<endl;
cout<<"Others="<<o<<endl;
return 0;
}
现成的函数isalpha 和 isdigit
(3)超半的数
#include
using namespace std;
int main()
{
int n;
cin>>n;
long long a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
cout<<a[n/2];
return 0;
}
1、long long int 可以扩大精度
2、sort函数对vector和arrray进行快速排序,但是不能将array写为会出错,n的部分得是一个常数。
(4)字符数量
#include
using namespace std;
int main()
{
int a[26]{};
char c;
while(scanf("%c",&c)==1)
{
if(c<='z'&&c>='a')
a[c-'a']++;
}
for(int i=0;i<26;++i)
{
if(a[i]!=0)
cout<<char(i+97)<<":"<<a[i]<<endl;
}
}
cin遇到空格就会停下,scanf啥都能读,返回数字为读取的个数,若想要读取包含空格的字符串,用getline(cin,s)
#include
#include
using namespace std;
int main(){
string s;
map<char, int> mp;
while(getline(cin, s)){
for(const auto& x : s){
if(x >= 'a' && x <= 'z') ++mp[x];
}
}
for(const auto& x : mp){
cout << x.first << ':' << x.second << endl;
}
return 0;
}
(5)质数数量
#include
#include
#define N 1000001
bool is_prime(int n);
using namespace std;
int main()
{
int T, n;
int a[N];
a[0] = 0;
for(int i = 1; i < N; i++)
{
a[i] = a[i - 1];
if(is_prime(i))
{
a[i]++;
}
}
cin >> T;
while(T > 0)
{
cin >> n;
cout << a[n] << endl;
T--;
}
return 0;
}
bool is_prime(int n)
{
bool res = true;
if(n < 2)
{
return false;
}
for(int i = 2; i <= sqrt(n); i++)
{
if(n % i == 0)
{
res = false;
break;
}
}
return res;
}
本题会有超时的情况出现,可以用一个数组进行存储,有另一个优化算法
#include
#define N 1000001
#define NSQRT 1000
using namespace std;
int main()
{
int T, n;
int a[N]={0}; /*注意初始化,不然结果每次运行都会不同*/
for(int i = 2; i <= NSQRT; i++)
{
if(a[i] == 0)
{
for(int j = i*i; j <= N; j += i)
{
a[j] = 1;
}
}
}
for(int i = 2; i < N; i++)
{
if(a[i] == 0)
{
a[i] = a[i - 1] + 1;
}
else
{
a[i] = a[i - 1];
}
}
cin >> T;
while(T > 0)
{
cin >> n;
cout << a[n] << endl;
T--;
}
return 0;
}
(6)约瑟夫环
#include
int main()
{
int n,k,m,i,j=0,a[100],b=0,c=0;
scanf("%d%d%d",&n,&k,&m);
for(i=0;i<n;i++)
a[i]=1;
for(i=k;i<n;i++)
{
j++;
if(a[i]!=0)
{
b++;
if(b==m)
{
b=0;
a[i]=0;
c++;
}
}
if(i==n-1)//使用i=-1使得下一回++i变成0
i=-1;
if(c==(n-1))
break;
}
for(i=0;i<n;i++)
if(a[i]!=0)
printf("%d",i);
return 0;
}
本题在于如何处理临界的时候的下标问题
#include
#include
using namespace std;
int main()
{
vector< int>table;//建立一个数组table
int i,n,k,m,pos;
cin>>n>>k>>m;
pos=k;
for(i= 0;i<n;i++){
table.push_back(i);//初始化 等价于table[i]=i;
}
for(i= 0;i<n-1;i++){
pos=(pos+m- 1)% table.size();//删除元素下标
table.erase(table.begin()+pos);//删除元素
}
cout<< table[0]<<endl;
return 0;
}
(7)删除一个元素
#include
#include
using namespace std;
int main()
{
int n, e;
vector<int> a;
while (cin >> n)
{
for (int i = 0; i < n; ++i)
{
cin >> e;
a.push_back(e);
}
int m;
int flag = 1;
cin >> m;
for (int i = 0; i < n; ++i)
{
if (a[i] == m && flag == 1)
{
a.erase(a.begin() + i);
flag = 0;
break;
}
}
if (flag)
{
for (int i = 0; i < n; ++i)
cout << a[i] << " ";
cout << endl;
}
else
{
for (int i = 0; i < n - 1; ++i)
cout << a[i] << " ";
cout << endl;
}
}
}
这是自己写的,答案没问题,就是通过不了
#include
using namespace std;
int main()
{
vector<long long> q;
long long n,m;
while(cin>>n)
{
long long a[100]={};
bool s[100]={false};
q.clear();
for(long long i=0;i<n;i++) cin>>a[i];
cin>>m;
for(long long i=0;i<n;i++)
{
if(a[i]==m)
{
s[i]=true;
break;
}
}
for(long long i=0;i<n;i++)
{
if(s[i]==false) q.push_back(a[i]);
}
vector<long long>::iterator it=q.begin();
cout<<*it;
it=q.begin()+1;
for(it;it!=q.end();it++) cout<<" "<<*it;
cout<<endl;
}
return 0;
}