首页 > 其他分享 >892.2021

892.2021

时间:2022-12-01 23:46:26浏览次数:31  
标签:892.2021 string int s2 s1 ++ sum

1:

#include <bits/stdc++.h>
using namespace std;

int main(){
    for(int i = 300; i <= 500; i += 2){
    	if(i % 3 == 0 && i % 7 == 0) printf("%d\n", i);
	}
    return 0;
}

  

2:

#include <bits/stdc++.h>
using namespace std;

int main(){
    string s;
    cin >> s;
    int l = s.length();
    int k = 1, ans = 0;
    for(int i = l - 1; i >= 0; i--){
    	int x;
    	if(s[i] >= 'A' && s[i] <= 'F'){
    		x = s[i] - 'A' + 10;
		}else{
			x = s[i] - '0';
		}
		ans += x * k;
		k *= 16;
	}
    cout << ans << endl;
    return 0;
}

  

3:

#include <bits/stdc++.h>
using namespace std;

int compare(string s1, string s2){
	int l1 = s1.length();
	int l2 = s2.length();
	int i;
	for(i = 0; i < min(l1,l2); i++){
		if(s1[i] != s2[i]){
			return s1[i] - s2[i];
		}
	}
	//这个我觉着我多虑了,考虑到两字符串不等长
	if(l1 > i) 		return s1[i] - '\n';
	else if(l2 > i)	return s2[i] - '\n';
	return 0;
}
int main(){
    string s1, s2;
    cin >> s1 >> s2;
    cout << compare(s1, s2) << endl;
    return 0;
}

  

4:

#include <bits/stdc++.h>
using namespace std;

int main(){
    string x, y;
    cin >> x >> y;
    //reverse反转字符串,stoi将字符串转换为整型
    reverse(x.begin(), x.end());
    reverse(y.begin(), y.end());
    string sum = to_string(stoi(x) + stoi(y));
    reverse(sum.begin(), sum.end());
    cout << stoi(sum) << endl;
    return 0;
}

  

5:

#include <bits/stdc++.h>
using namespace std;

//核心:NEXT i = 0表示往右走,i = 1表示往下左走,i = 2表示往上走
//根据DFS,一路走到头再选择换路径,优先 i = 1。。。  
int NEXT[3][2] = {{0,1}, {1,-1}, {-1,0}};
int n, a[1005][1005];
//book数组用于检查下标 book[x][y] x,y这个坐标点是否登记过 
bool book[1005][1005];
void dfs(int x, int y, int k){
	for(int i = 0; i < 3; i++){
		int tx = x + NEXT[i][0];
		int ty = y + NEXT[i][1];
		//注意y的范围!! 
		if(x < 1 || x > n || y < 1 || y > n - x + 1)continue;
		if(!book[tx][ty]){
			book[tx][ty] = 1;
			a[tx][ty] = k;
			dfs(tx, ty, k + 1);
		} 
	}
}
int main() {
	int k = 1;
	cin >> n;
	memset(book, 0, sizeof(book));
	a[1][1] = k;
	book[1][1] = 1;
	dfs(1, 1, k + 1);
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n - i + 1; j++){
			cout << a[i][j] << "\t";
		}
		cout << endl;
	} 
	return 0;
}

  

6:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int a[35][35], b[35][35], c[35][35];
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			cin >> a[i][j];
			b[i][j] = a[i][j];
		}
	}
	for(int k = 1; k < m; k++) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				int sum = 0;
				for(int t = 0; t < n; t++) {
					sum = sum + a[i][t] * b[t][j];
				}
				c[i][j] = sum;
			}
		}
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				a[i][j] = c[i][j];
			}
		}
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			cout << a[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}

  

7:

#include <bits/stdc++.h>
using namespace std;

vector<string> s;

bool cmp(string s1, string s2){
	//将	MM//DD//YYYY 转换成 YYYYMMDD 
	string ss1 = "";
	ss1 = ss1 + s1[6] + s1[7] + s1[8] + s1[9] + s1[0] + s1[1] + s1[3] + s1[4];
	string ss2 = "";
	ss2 = ss2 + s2[6] + s2[7] + s2[8] + s2[9] + s2[0] + s2[1] + s2[3] + s2[4];
	// 16110505 与 17110505 6<7 则ss1 < ss2 s1 < s2 日期上 s1比s2早 需要交换 
	for(int i = 0; i < ss1.length(); i++){
		if(ss1[i] < ss2[i]) 		return 1;
		else if(ss1[i] > ss2[i])	return 0;
	}
	return 0;
}
/*
测试数据 
4
02/02/2002
01/05/2002
05/06/1987
03/03/1000
*/
int main() {
	int n;
	 
	cin >> n;
	for(int i = 0; i < n; i++){
		string x;
		cin >> x;
		s.push_back(x); 
	}
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < n - 1; j++){
			if(cmp(s[j], s[j + 1])){
				swap(s[j], s[j + 1]);
			}
		}
	} 

	cout << endl;
	for(int i = n - 1, t = 0; i >= 0 && t < 3;i--, t++){
		cout << s[i] << endl;
	}
	return 0;
}

  

8:

#include <bits/stdc++.h>
using namespace std;


int mystringSum(char *str){
	int sum = 0;
	string s = str;
	for(int i = 0; i < s.length(); i++){
		sum += s[i];
	}
	return sum;
} 

int main() {
	char s[1005];
	cin >> s;
	
	cout << mystringSum(s);
	
	return 0;
}

9:

 

标签:892.2021,string,int,s2,s1,++,sum
From: https://www.cnblogs.com/rongrongrong/p/16943158.html

相关文章