日常题目训练

2019:5:10


找规律

Problem Description

M今天在给SD的同学们上程序算法课的时候出了一道找规律的题目,题目表述如下
假设:
S1=a
S2=ab
S3=abc
……
S26=abcd…xyz
S27=abcd…xyza
…….
现在M要求上课的同学们把所有的串依次连接起来,于是得到:
S=aababcabcd……
那么你能告诉M在S串中的第N个字母是多少吗?

Input

输入首先是一个数字K,代表有K次询问(1<= K <= 1000)
接下来的K行每行有一个整数N(1<= N <= 10000)

Output

对于每次询问,输出S串中第N个位置对应的字母。

Sample Input

6
1
2
3
4
5
10

Sample Output

a
a
b
a
b
d

#include 
#include 
using namespace std;

char arr[26];

//初始化 
void init(){
	for(int i=97;i<=123;i++){
		arr[i-97] = i;
	}
}

string str="";
void inits(){
	for(int i=0;i<200;i++){
		for(int j=0;j<=i;j++){
			str = str + arr[j%26]; 
		}
	}
}

int main(){
	init();
	inits();
	int n;
	cin >> n;
	while(n--){
		int k,l,j,i;
		cin>>k; 
		cout<

最大值:


Problem Description

You are given a positive integer n.
Let S(x) be sum of digits in base 10 representation of x, for example, S(123) = 1 + 2 + 3 = 6, S(0) = 0. Your task is to find two integers a, b, such that (0 <= a, b <=n, a + b = n) and S(a) + S(b) is the largest possible among all such pairs.

Input

Output

For each test case print largest S(a) + S(b) among all pairs of integers a, b.

Sample Input

2
35
1000000000

Sample Output

17
82

说明
In the first example, you can choose, for example, a = 17 and b = 18, so that S(17) + S(18) = 1 + 7 + 1 + 8 = 17. It can be shown that it is impossible to get a larger answer.
In the second test example, you can choose, for example, a = 500000001 and b = 499999999, with S(500000001) + S(499999999) = 82. It can be shown that it is impossible to get a larger answer

 

#include 
#include 
#define ll long long
using namespace std;

ll fun(ll x){
	ll p =0;
	while(x!=0){
		p = p+x%10;
		x = x /10;
	}
	return p;
}

int main(){
	int n;
	cin>>n;
	while(n--){
		ll m;
		cin>>m;
		ll p,q,k,t=0;
		p = m/2;  //
		while(p/10!=0){
			t++;
			k = p/10;
			p = p/10;
		}
		while(t--){
			k = k*10+9;
		}
		q = m-k;
		cout<

发工资

Problem Description

M今天发工资,想去买吃的。
M来到一家糖果店,糖果店里摆放着X堆糖,假设第i堆糖的数量为a[i],糖堆之间满足关系 a[i] - a[i - 1] = d(d为常数)
现在给出第一堆糖的数量,第二堆糖的数量。
求M要买的第P堆糖的数量

Input

多组输入,每组数据有两行
第一行为第一堆糖的数量和第二堆糖的数量
第二行为P

Output

输出第P堆糖的数量

Sample Input

1 2
3
2 4
3

Sample Output

3
6
#include 
#include 
#define ll long long
using namespace std;

int main(){
	
	int n,m,k;
	while(cin>>n>>m>>k){
		int d = m-n;
		ll t=0;
		for(int i = 3;i<=k;i++){
			t = m + d;
			m = t;
		}
		cout<

寻找素数对

Problem Description

哥德巴赫猜想大家都知道一点吧.我们现在不是想证明这个结论,而是想在程序语言内部能够表示的数集中,任意取出一个偶数,来寻找两个素数,使得其和等于该偶数.
做好了这件实事,就能说明这个猜想是成立的.
由于可以有不同的素数对来表示同一个偶数,所以专门要求所寻找的素数对是两个值最相近的.

Input

输入中是一些偶整数M(5

Output

对于每个偶数,输出两个彼此最接近的素数,其和等于该偶数.

Sample Input

20 30 40

Sample Output

7 13
13 17
17 23
#include 
#include  
using namespace std;

int ss(int x){
	for(int i =2;i<=sqrt(x);i++){
		if(x%i==0)
		return 0;
	}
	return 1;
}
int main(){
	int n;
	while(~scanf("%d",&n)){    
		int i=0,j=0;
		for(i= n/2,j=n-i;i>=0,j<=n;i--,j++){
			if(ss(i)&&ss(j)){
				printf("%d %d\n",i,j);
				break;
			}
		}
	}	
	return 0;
}

A+B for Input-Output Practice (VIII)

 

Problem Description

Your task is to calculate the sum of some integers.

Input

Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.

Output

For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.

Sample Input

3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output

10

15

6
#include 
using namespace std;

//格式问题 
int main(){
	
	int n;
	cin >> n;
	while(n--){
		int m;
		cin>>m;
		int total=0;   
		while(m--){
			int num ;
			cin>>num;
			total = total+num; 
		}
		cout<

 

你可能感兴趣的:(C/C++语言基础题目)