招银网络:笔试题(20190906)

黄金数的个数

设置一个临时变量来存放所有因子之积,当大于当前值cu则可以直接中止。如果两者相等,则累加值count加一。

#include <iostream>

using namespace std;

int gold_number(int num)
{
	if(num < 6)
		return 0;
	int count = 0;
	for(int nu=6; nu <= num; nu++)
	{
		int current = 1;
		for(int cu=2; cu < nu; cu++)
		{
			if(nu%cu == 0)
			{
				current *= cu;
			}
			if(current > cu)
				break;
		}
		if(current == nu)
		{
			count += 1;
		}
	}
	return count;
}

int main()
{
	int num;
	cin >> num;
	int res;
	res = gold_number(num);
	cout << res << endl;
}

随机打乱一个有序序列

原题为一个编程填空题,有点搞不懂思路,也不明白迭代器和索引是提供两种方式还是都是有作用的,但是注意到了题目额外定义了一个idx,但是代码中却没有使用。所以,idx应该就是用来产生随机数的,而条件判断应该考虑到只有一个元素的情况避免死循环,同时要注意在vector中删除一个元素应该使用erase函数。

  • array.erase(array.begin()+idx);:删除第idx个元素。
#include <iostream>
#include <vector>
#include <time.h>
#include <iterator>

using namespace std;

void shuffle(vector<int> array)
{
	int i=0, idx;
	vector<int>::iterator it;

	srand((unsigned)time(NULL));

	while(!array.empty())
	{
		idx = 0 + rand()%(array.size());	// 第一处
		for(i=0, it=array.begin(); it != array.end(); it++, i++)
		{
			if(array[idx] != array[i] || array.size() == 1)		// 第二处
			{
				cout << array[idx] << " ";
				array.erase(array.begin()+idx);		// 第三处
				break;
			}
		}
	}
	cout << endl;
}

int main()
{
	int num;
	vector<int> seq;
	while(cin >> num)
	{
		seq.push_back(num);
	}

	shuffle(seq);

	return 0;
}

交换两个字符串

使用指针变量作为形参来操作,直接交换s和t指向的变量的值,而不是改变形参指针变量s和t的指向。要输入char*类型的值,可采用以下方式:

  • char* s = new char[maxSize]; cin >> s;

  • char* s; string temp; cin>>temp; s = const_cast<char*>(temp);

#include <iostream>
#include <string>

using namespace std;

void swap(char* &s, char* &t);

int main()
{
	char* s = new char[100];
	char* t = new char[100];

	cin >> s;
	cin >> t;

	swap(s, t);

	cout << s << " " << t << endl;

	return 0;
}

void swap(char* &s, char* &t)
{
	char* temp = s;
	s = t;
	t = temp;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
招银网络Java笔试题主要测试对Java语言的理解和应用能力。以下是一些可能出现的题目和答案: 题目一:请用Java语言实现一个简单的计算器,能够进行加减乘除运算。 答案一: ```java import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入第一个运算数:"); double num1 = input.nextDouble(); System.out.print("请输入第二个运算数:"); double num2 = input.nextDouble(); System.out.print("请输入运算符(+、-、*、/):"); char operator = input.next().charAt(0); double result = 0; switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default: System.out.println("请输入正确的运算符!"); } System.out.println("计算结果:" + result); } } ``` 题目二:请写一个Java程序,判断一个字符串是否是回文串。 答案二: ```java import java.util.Scanner; public class Palindrome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入一个字符串:"); String str = input.next(); boolean isPalindrome = true; int length = str.length(); for (int i = 0; i < length / 2; i++) { if (str.charAt(i) != str.charAt(length - i - 1)) { isPalindrome = false; break; } } if (isPalindrome) { System.out.println(str + "是回文串"); } else { System.out.println(str + "不是回文串"); } } } ``` 以上是两道可能出现在招银网络Java笔试中的题目及其解答,供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值