【day36】

题目:

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

cpp 复制代码
#include<iostream> 
#include<string>
#include<algorithm>
using namespace std;
int main() {
	int n;
	cin >> n;
	int t = 1;
	while (t <= n) {
		string a, b;
		cin >> a >> b;
		string sum = "";
		reverse(a.begin(),a.end());//反转顺序,方便计算
		reverse(b.begin(), b.end());
		int lena = a.size();
		int lenb = b.size();
		int i = 0, j = 0, ka, kb, add, extra = 0;
		while (i < lena && j < lenb) {
			ka = (int)(a[i] - '0');
			kb = (int)(b[j] - '0');
			add = ka + kb + extra;
			extra = add / 10;//进位
			add = add % 10;
			sum.push_back(add+'0');//!!!加上'0'再加入sum,因为add是int型
			i++;
			j++;
		}
        //处理剩下的a or b
		while (i<lena) {
			ka = (int)(a[i] - '0');
			add = ka + extra;
			extra = add / 10;
			add = add % 10;
			sum.push_back(add + '0');
			i++;
		}
		while (j < lenb) {
			kb = (int)(b[j] - '0');
			add = kb + extra;
			extra = add / 10;
			add = add % 10;
			sum.push_back(add + '0');
			j++;
		}
		if (extra != 0) {//最后一个进位
			sum.push_back(extra + '0');
		}
        //再转回来
		reverse(a.begin(), a.end());
		reverse(b.begin(), b.end());
		reverse(sum.begin(), sum.end());
		cout << "Case " << t << ":" << endl;
		cout << a << " + " << b << " = " << sum << endl;;
		cout << '\n';
		t++;
	}
	return 0;
}

题目:

某天、小晨在路上背着单词,突遇一外星人,外星人对小晨很感兴趣,为了考验小晨的智商,就向小晨提问简单加法,由于外星人使用16进制,所以,小晨必须用16进制回答。

个人总结:

  1. 遇上题解决思路相同
  2. 注意十六进制的加法运算规律
cpp 复制代码
#include<iostream> 
#include<string>
#include<algorithm>
using namespace std;
int backnum(char a) {
	if (a >= '0' && a <= '9')return (int)(a - '0');
	else {
		return (int)(a - 'a' + 10);
	}
}
int main() {
	int n;
	cin >> n;
	char index[6] = { 'a','b','c','d','e','f' };
	while (n > 0) {
		n--;
		string a, b;
		cin >> a >> b;
		reverse(a.begin(), a.end());
		reverse(b.begin(), b.end());
		string sum;
		int lena = a.size();
		int lenb = b.size();
		int i = 0, j = 0, ka, kb, add, extra = 0;
		while (i < lena && j < lenb) {
			ka = backnum(a[i]);
			kb = backnum(b[j]);
			add = ka + kb + extra;
			extra = add / 16;
			add = add % 16;
			if (add >= 10)sum.push_back(index[add % 10]);
			else sum.push_back(add + '0');
			i++;
			j++;
		}
		while (i < lena) {
			ka = backnum(a[i]);
			add = ka + extra;
			extra = add / 16;
			add = add % 16;
			if (add >= 10)sum.push_back(index[add % 10]);
			else sum.push_back(add + '0');
			i++;
		}
		while (j < lenb) {
			kb = backnum(b[j]);
			add = kb + extra;
			extra = add / 16;
			add = add % 16;
			if (add >= 10)sum.push_back(index[add % 10]);
			else sum.push_back(add + '0');
			j++;
		}
		if (extra != 0) {
			sum.push_back(extra + '0');
		}
		reverse(a.begin(), a.end());
		reverse(b.begin(), b.end());
		reverse(sum.begin(), sum.end());
		cout << sum << endl;
	}
	return 0;
}

题目:

明明的爸爸是一位数学家,明明受他爸爸的影响从小就喜欢数学,经常向他爸爸请教数学问题。一天,明明问爸爸什么是素数,爸爸回答说:"首先,素数都是大于1的自然数;其次,素数是只能被1和其本身整除的数。例如'3'这个数,它只能被1和3这两个整数整除,因此'3'就是素数;但是'4'就不是素数,因为4除了能被1和4整除外,也能被2整除,因此'4'就不是一个素数。"

聪明的明明很快就理解了他爸爸的意思,于是又接着问他爸爸:"那么纯粹素数又是什么呢?"明明的爸爸接着回答说:"一个素数,去掉最高位,剩下的数仍为素数,再去掉剩下的数的最高位,余下的数还是素数,这样下去一直到最后剩下的个位数也还是素数,我们把这样的数称为纯粹素数。例如'1013'这个数,它只能被1和1013整除,因此'1013'是一个素数,我们去掉它的最高位,剩下的数是13(其实剩下的应该是013,但是前置0对一个整数来说没有意义,因此0被舍去,就剩下13),13只能被1和13整除,因此13也是个素数,我们再接着去掉它的最高位,剩下的个位数是3,3当然也是素数,因此'1013'就是纯粹素数。更有趣的是,1013是第一个大于1000的纯粹素数,因为:

  • 1000能被1、2、......、1000整除,其本身不是素数;

  • 1001能被1、7、......、1001整除,其本身不是素数;

  • 1002能被1、2、......、1000整除,其本身不是素数;

  • 1003能被1、17、......、1003整除,其本身不是素数;

  • 1004能被1、2、......、1004整除,其本身不是素数;

  • 1005能被1、3、......、1005整除,其本身不是素数;

  • 1006能被1、2、......、1006整除,其本身不是素数;

  • 1007能被1、19、......、1007整除,其本身不是素数;

  • 1008能被1、2、......、1008整除,其本身不是素数;

  • 1009是一个素数,但是9能被1、3、9整除,不是素数;

  • 1010能被1、2、......、1010整除,其本身不是素数;

  • 1011能被1、3、......、1011整除,其本身不是素数;

  • 1012能被1、2、......、1012整除,其本身不是素数;

所以从1000到1012,每个数都不是纯粹素数。"

明明对他爸爸的回答很满意,于是自己动手从1000开始寻找纯粹素数,不一会儿他就找到了20个纯粹素数,调皮的明明开始反过来考爸爸了,问他爸爸能否告诉他第2个大于1000的纯粹素数是哪个?第3个大于1000的纯粹素数是哪个?......明明的爸爸被这些突如其来的问题给难住了,他无法立刻回答出来,于是请求你帮助他回答明明的这些问题。

明明的问题可以归结为:跟据一个正整数n,求出从1,000开始从小到大的第n个纯粹素数。

个人总结:

  1. delfirst函数与之前有道题相同做法
cpp 复制代码
#include<iostream> 
#include<math.h>
using namespace std;
int delfirst108(int n) {
	int i = 0;
	int temp = n;
	while (temp > 0) {
		i++;
		temp /= 10;
	}
	int j = pow(10, i - 1);
	return n % j;
}
bool sushu108(int n) {
	if (n == 1)return false;
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)return false;
	}
	return true;
}
int main() {
	int n;
	while (cin >> n) {
		int i = 1013;
		int t = 0;
		while (t < n) {
			int temp = i;
			int flag = 1;
			while (temp > 0) {
				if (!sushu108(temp)) {
					flag = 0;
					break;
				}
				temp = delfirst108(temp);
			}
			if (flag)t++;
			if (t == n)cout << i << endl;
			i++;
		}
	}
	return 0;
}

Translation:

Computer science can be divided into four main fields: softwaredevelopment, computer architecture (hardware), human-computer interfacing (the design of the most efficient ways forhumans to use computers), and artificial intelligence (theattempt to make computers behave intelligently). Softwaredevelopment is concerned with creating computer programsthat perform efficiently. Computer architecture is concerned withdeveloping optimal hardware for specific computational needs.The areas of artificial intelligence (AI) and human-computerinterfacing often involve the development of both software andhardware to solve specific problems.

计算机科学可分为四个主要领域:软件开发、计算机体系结构(硬件)、人机交互(设计人类使用计算机的最高效方式)以及人工智能(试图让计算机表现出智能行为)。软件开发涉及创建运行高效的计算机程序。计算机体系结构致力于为特定的计算需求开发最优硬件。人工智能(AI)和人机交互领域通常需要同时开发软件和硬件来解决特定问题。

In developing computer software, computer scientists and engineers study various areas and techniques of software design,such as the best types of programming languages andalgorithms to use in specific programs, how to efficiently storeand retrieve information, and the computational limits of certainsoftware-computer combinations. Software designers mustconsider many factors when developing a program. Often,program performance in one area must be sacrificed for the sakeof the general performance of the software. For instance, sincecomputers have only a limited amount of memory, softwaredesigners must limit the number of features they include in aprogram so that it will not require more memory than thesystem it is designed for can supply.

在开发计算机软件时,计算机科学家和工程师会研究软件设计的各个领域和技术,例如在特定程序中使用的最佳编程语言和算法类型、如何高效地存储和检索信息,以及某些软件与计算机组合的计算极限。通常情况下,为了软件的整体性能,必须牺牲某个方面的程序性能。例如,由于计算机的内存量有限,软件设计师必须限制程序中包含的功能数量,以确保程序所需的内存不会超过其设计运行的系统所能提供的内存。

Software engineering is an area of software development inwhich computer scientists and engineers study methods andtools that facilitate the efficient development of correct, reliable,and robust computer programs. Research in this branch ofcomputer science considers all the phases of the software lifecycle, which begins with a formal problem specification, and progresses to the design of a solution, its implementation as aprogram, testing of the program, and program maintenance.Software engineers develop software tools and collections oftools called programming environments to improve thedevelopment process. For example, tools can help to managethe many components of a large program that is being writtenby a team of programmers.

软件工程是软件开发的一个领域,计算机科学家和工程师在此研究有助于高效开发正确、可靠且稳健的计算机程序的方法和工具。计算机科学这一分支的研究涵盖了软件生命周期的所有阶段,该周期始于正式的问题说明,随后依次推进到解决方案的设计、将方案实现为程序、程序测试以及程序维护。软件工程师会开发软件工具以及被称为编程环境的工具集合,以改进开发过程。例如,工具可以帮助管理由一个程序员团队编写的大型程序的众多组件。

  • robust 稳定的

相关推荐
Z9fish1 小时前
sse哈工大C语言编程练习23
c语言·数据结构·算法
山河君1 小时前
四麦克风声源定位实战:基于 GCC-PHAT + 最小二乘法实现 DOA
算法·音视频·语音识别·信号处理·最小二乘法·tdoa
额,不知道写啥。2 小时前
P5354 [Ynoi Easy Round 2017] 由乃的 OJ
java·开发语言·算法
代码无bug抓狂人2 小时前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
im_AMBER2 小时前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
Polaris北2 小时前
第二十九天打卡
算法
CodeJourney_J2 小时前
从“Hello World“ 开始 C++
c语言·c++·学习
样例过了就是过了2 小时前
LeetCode热题100 环形链表 II
数据结构·算法·leetcode·链表