给定一个正整数,确定它是否可以表示为两个平方数的级联。连在一起的数字不能以0开头(0除外)。输入中的任何前导零都应该被忽略。
示例
11 -> true // because 1 and 1 are squares
90 -> true // because 9 and 0 are squares
1001 -> true // because 100 and 1 are squares
144100 -> true // because 144 and 100 are squares
151296561 -> true // because 15
项目欧拉问题1说:
找出低于1000的3或5倍数之和。
我已经编写了一个程序,它将返回任何正整数列表的倍数之和。例如,如果我想知道每一个整数的之和,即小于10的3和5的倍数,我会将3,5,6和9相加,得到23。
我的函数可以对任意数量的正整数这样做,在从1开始的任何范围内。
# Function to return sum of multiples that are within a certain range
def MultiplesSum(multiple, end, start=1):
mSum = 0
naturals = range(start, end)
我需要验证字符串,如果它包含字母数字值和至少6个字符,如abcd。
我尝试使用下面的代码,但没有找到如何验证字符串是否以char开头,并且至少包含其中的6个。字符串可以有一个或几个数字,但不能以一个开始。
var regex = new Regex("^[a-zA-Z0-9]*$");
if (!string.IsNullOrEmpty(_item ) && !regex.IsMatch(_item))
#include <stdio.h>
#include <cs50.h>
int GetPositiveInt();
int main (void)
{
int min; /*variable to hold minutes*/
printf ("How many minutes does it take you to use a shower?");
scanf ("%d", &min);
int numbtl = min * 12; /*computes number of bottles*/
我试图写一个函数,以一个正整数和一个素数作为输入,如果给定的正整数的素数因子小于或等于给定素数的,则返回true。
给定素数应该是给定正整数的素数。
hasLessPrimeFactor( 20 ,5)应该返回true,因为一个数的素数因子:20=2X2X5
Up to now I have completed 3 test cases out of 4 test cases
返回类型应该是string.hasLessPrimeFactor(20,5),truehasLessPrimeFactor(20,7)应该返回,falseThe答案对任何给定的输入都是有效的。
我到目前为止所做的事:
以下问题说明:
“编写一个从标准输入中读取正整数的循环,当它读取一个非正整数时终止。在循环结束后,它会在一行上打印出所读取的所有偶数整数的和。声明任何需要的变量。假设引用与标准输入关联的Scanner对象的变量stdin可用”。
答案如下:
int even = 0;
int i = 1;
while (i > 0){
i = stdin.nextInt();
if ((i % 2)==0 && (i > 0)){
even += i;
}
}
System.out.print(even);
为什么要将int变量"i“设置为代码要工作的数字值?我好像搞不懂为