###### 【九度】题目1491：求1和2的个数

10
11

3
5

2013年王道论坛研究生机试练习赛（三）
【解题思路】

Java AC

import java.util.Scanner;

public class Main {
/*
* 1491
*/
private static int mod = 20123;
private static char array[];
private static int len;
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String input = scanner.next();
array = input.toCharArray();
len = array.length;
System.out.println(modValue(calCount(1) + calCount(2)));
}
}

private static int calCount(int num) {
int k = len - 1;
int factor = 1;
int count = 0;
while (k >= 0) {
int lowNum = 0;
int currNum = 0;
int highNum = 0;
for (int i = 0; i < k; i++) {
highNum = modValue(modValue(highNum * 10) + (array[i] - '0'));
}
for (int i = k+1; i < len; i++) {
lowNum = modValue(modValue(lowNum * 10) + array[i] - '0');
}
currNum = array[k] - '0';
if (currNum < num){
count = modValue(count + highNum * factor);
}else if (currNum == num) {
count = modValue(count + highNum * factor + lowNum + 1);
}else if (currNum > num) {
count = modValue(count + (highNum + 1) * factor);
}

factor = modValue(factor * 10);
k--;
}
return count;
}

private static int modValue(int n) {
return n % mod;
}
}

/**************************************************************
Problem: 1491
User: wzqwsrf
Language: Java
Result: Accepted
Time:130 ms
Memory:18248 kb
****************************************************************/

C++ AC

#include <stdio.h>
#include <string.h>

const int mod = 20123;
int len;
int i;
char array[1000];
int modValue(int n){
return n % mod;
}

int calCount(int num) {
int k = len - 1;
int factor = 1;
int count = 0;
while (k >= 0) {
int lowNum = 0;
int currNum = 0;
int highNum = 0;
for (i = 0; i < k; i++) {
highNum = modValue(modValue(highNum * 10) + (array[i] - '0'));
}
for (i = k+1; i < len; i++) {
lowNum = modValue(modValue(lowNum * 10) + array[i] - '0');
}
currNum = array[k] - '0';
if (currNum < num){
count = modValue(count + highNum * factor);
}else if (currNum == num) {
count = modValue(count + highNum * factor + lowNum + 1);
}else if (currNum > num) {
count = modValue(count + (highNum + 1) * factor);
}

factor = modValue(factor * 10);
k--;
}
return count;
}

int main(){

while(scanf("%s",&array) != EOF){
len = strlen(array);
printf("%d\n",modValue(calCount(1) + calCount(2)));
}
return 0;
}
/**************************************************************
Problem: 1491
User: wzqwsrf
Language: C++
Result: Accepted
Time:10 ms
Memory:1020 kb
****************************************************************/

#### 51NOD 1491 黄金系统 && Codeforces 458 A. Golden System（斐波那契数列 + 找规律）

2016-08-21 11:50:46

#### 九度OJ题目1491：求1和2的个数

2016-08-10 19:54:32

#### 51nod 1491 黄金系统

2017-10-01 20:13:13

#### 11-快排（排序）

2013-06-20 16:07:45

#### 数字处理相关编程题

2012-11-14 21:14:33

#### Eclipse 版本发布规律

2017-12-17 10:51:56

#### bzoj1491

2017-11-06 16:54:49

#### 规律数字方阵

2017-09-12 10:13:44

#### IT行业三大定律

2014-04-27 22:41:04

#### 九度题目1087：约数的个数

2014-05-18 19:53:49

## 不良信息举报

【九度】题目1491：求1和2的个数