当前位置:网站首页>Skills of CCF question 2
Skills of CCF question 2
2022-06-25 20:05:00 【Orange cedar】
1. Then sell vegetables : Store... In two arrays
scanf("%d", &n);
for(i = 1; i <= n * 2; i++) {
scanf("%d%d", &ai, &bi);
for(j = ai; j < bi; j++)
cnt[j]++;
}
2. Take a jump : Basic points + Extra points Set two variables if(a==1)plaus=0 else
while(scanf("%d", &a) != EOF && a) {
sum += a;
if(a == 1)
plus = 0;
else if(a == 2) {
sum += plus;
plus += 2;
}
}
3. Colliding balls : Outer layer t loop , Inner layer n A small ball cycle , Location pos[n] , Direction step[n] simulation
/* CCF201803-2 Colliding balls */
#include <iostream>
using namespace std;
const int L = 1000;
int pos[L + 1], step[L + 1];
int main()
{
int n, l, t;
cin >> n >> l >> t;
for(int i = 0; i < n; i++) {
cin >> pos[i];
// Start right , Reach both ends and turn back
step[i] = 1;
if(pos[i] == l || pos[i] == 0)
step[i] = -step[i];
}
for(int i = 0; i < t; i++) {
// Take a step
for(int j = 0; j < n; j++) {
pos[j] += step[j];
// Reach both ends and turn back
if(pos[j] == l || pos[j] == 0)
step[j] = -step[j];
}
// Judge whether to meet , When we meet, we turn around ( Avoid repeated comparisons )
for(int j = 0; j < n; j++)
for(int k = j + 1; k < n; k++)
if(pos[k] == pos[j])
step[k] = -step[k], step[j] = -step[j];
}
for(int i = 0; i < n; i++)
cout << pos[i] << " ";
cout << endl;
return 0;
}
4. Minimum difference :sort Sort
sort(a, a + n);
// Calculate the minimum value of the difference
int min = N2;
for(int i=1; i<n; i++) {
int d = a[i] - a[i - 1];
if(d < min)
min = d;
}5. Divide the cake , The data packet , Edge input , Side record ,count++ after sub Set as 0, Consider the last piece of cake alone
/* CCF201703-1 Divide the cake */
cin >> n >> k;
for(int i=1; i<=n; i++) {
cin >> val;
if((sub += val) >= k) {
count++;
sub = 0;
}
}
if(sub > 0)
count++;
6. The students lined up : Good at using vector Of insert and erase( Move elements ),STL find() Return iterator ,distance()
// Simulate the moving process
for(int i=1; i<=m; i++) {
cin >> p >> q;
// Student number , Adjustment operation
vector <int> :: iterator it =find(v.begin(),v.end(),p);
// Find the operation element location
int pos=distance(v.begin(), it) ;
// Mobile is realized by inserting and deleting
v.insert(v.begin() + pos + (q > 0 ? q + 1 : q), p);
v.erase(v.begin() + pos + (q < 0 ? 1 : 0));
} 7. The middle number : Good at using lower_bound(),upper_bound()
// Sort
sort(val, val+n);
// Find the middle number
int mid = val[n / 2];
int lb = lower_bound(val, val + n, mid) - val;
int ub = upper_bound(val, val + n, mid) - val;
// Output results
if(n - ub == lb)
cout << mid << endl;
else
cout << -1 << endl;8. Wage calculation ( pre-tax / After tax / tax rate / Three table mapping data range conversion )
/* CCF201612-2 Wage calculation */
int salaryrange[] = {3500, 3500+1500, 3500+4500, 3500+9000, 3500+35000, 3500+55000, 3500+80000 };
int taxrate[] = {3, 10, 20, 25, 30, 35, 45};
const int SIZE = sizeof(salaryrange) / sizeof(int);
int range[SIZE];
int main()
{
int t, s;
// Seek various income ranges
range[0] = salaryrange[0];
for(int i=1; i<SIZE; i++) {
range[i] = range[i-1] + (salaryrange[i] - salaryrange[i-1])
- (salaryrange[i] - salaryrange[i-1]) * taxrate[i-1] / 100;
}
// Income range
int i;
for(int i=0; i<SIZE; i++)
if(t <= range[i])
break;
// Instead, ask for pre tax salary
if(i == 0)
s = t;
else {
s = salaryrange[i-1] + (t - range[i-1]) * 100 / (100 - taxrate[i-1]);
}
// Function abstraction
// pre-tax ( Pay attention to the wage expression ) tax rate After tax Three forms Range judgment Wage conversion
int X[]={3500,3500+1500,3500+4500,3500+9000,3500+35000,3500+80000};
int K[]={3,10,20,25,30,35,45};
const int SIZE=sizeof(X)/sizeof(int);// Number of salary range grades
int Y[SIZE];
int main(){
// Recurrence , initialization Y form ;
Y[0]=X[0];
for(int i=0;i<SIZE;i++){
Y[i]=Y[i-1]+(X[i]-X[i-1])-(X[i]-X[i-1])* K[i-1] / 100;
}
// Input
int x,y,i;
scanf("%d",&y);
// find y The scope of the
for( i=0;i<SIZE;i++){
if(y<=Y[i])break;
}
// Calculate original salary , Output x
if(i==0) x=y;
else{
x=X[i-1]+(y-Y[i-1])*100/(100-K[i-1]);
}
9. The sum of numbers
while(n) {
sum += n % 10;
n /= 10;
}
// String version
string a;
for(int i=0;i!=a.size();i++)
sum+=a[i]-'0';
// A character array
char num[50];
scanf("%s",num);
for(int i=0;i<strlen(num);i++)
sum+=num[i]-'0';
边栏推荐
- Web container basic configuration
- 1、 Hikaricp source code analysis of connection acquisition process I
- Life cycle function of composite API
- Alicloud centos8.0 installing mysql8
- <C>. String comparison
- PHP FPM, workman, spoole, golang simple performance test
- PAT B1056
- From now on, I will blog my code
- Dependency injection in PHP reflection implementation framework
- 2020-12-09 laravel . Env file loading mechanism process
猜你喜欢

Huawei HMS core launched a new member conversion & retention prediction model

LNMP compilation and installation

Wechat applet swiper simple local picture display appears large blank

Now meditation: crash service and performance service help improve application quality
![[harmonyos] [arkui] how can Hongmeng ETS call pa](/img/19/9d2c68be48417e0aaa0d27068a67ce.jpg)
[harmonyos] [arkui] how can Hongmeng ETS call pa

Automatic fitting when the applet reaches the top

rmi-registry-bind-deserialization

PostgreSQL user role permissions

Impact of Huawei application transfer and application claim on user identification

Principles of MySQL clustered index and non clustered index
随机推荐
wooyun-2014-065513
Bindgetuserinfo will not pop up
Hdoj topic 2005 day
2020-12-09 laravel . Env file loading mechanism process
2.5 find the sum of the first n terms of the square root sequence
Installing or uninstalling VMware could not find the network location
2.2 step tariff
Processing method for uniapp or applet onload not receiving parameters
C language PTA -- continuity factor
Convert word to PDF through libreoffice
Is it safe to open a new bond account
JS asynchronism (I. asynchronous concept, basic use of web worker)
Impact of Huawei application transfer and application claim on user identification
Panda weekly -2022/02/18
ActiveMQ--CVE-2016-3088
二、HikariCP获取连接流程源码分析二
Applet Click to return to the top 2 methods
The functions in the applet page are better than those in app JS first execution solution
Arduino read temperature
String since I can perform performance tuning, I can call an expert directly